feat: md-table weighted random
This commit is contained in:
@@ -3,6 +3,7 @@ import { createSignal, For, Show, createEffect, createMemo, createResource } fro
|
||||
import { marked } from '../markdown';
|
||||
import { loadCSV, CSV, processVariables, isCSV } from './utils/csv-loader';
|
||||
import { resolvePath } from './utils/path';
|
||||
import { areAllLabelsNumeric, weightedRandomIndex } from './utils/weighted-random';
|
||||
|
||||
export interface TableProps {
|
||||
roll?: boolean;
|
||||
@@ -108,8 +109,23 @@ customElement('md-table', { roll: false, remix: false }, (props, { element }) =>
|
||||
|
||||
// 随机切换 tab
|
||||
const handleRoll = () => {
|
||||
const randomIndex = Math.floor(Math.random() * filteredRows().length);
|
||||
setActiveTab(randomIndex);
|
||||
const filtered = filteredRows();
|
||||
if (filtered.length === 0) return;
|
||||
|
||||
// 检查所有 label 是否都是整数/整数范围格式
|
||||
const labels = filtered.map(row => row.label);
|
||||
if (areAllLabelsNumeric(labels)) {
|
||||
// 使用加权随机
|
||||
const randomIndex = weightedRandomIndex(labels);
|
||||
if (randomIndex !== -1) {
|
||||
setActiveTab(randomIndex);
|
||||
}
|
||||
} else {
|
||||
// 使用简单随机
|
||||
const randomIndex = Math.floor(Math.random() * filtered.length);
|
||||
setActiveTab(randomIndex);
|
||||
}
|
||||
|
||||
// 滚动到可视区域
|
||||
setTimeout(() => {
|
||||
const activeButton = tabsContainer?.querySelector('.border-blue-600');
|
||||
|
||||
Reference in New Issue
Block a user