mirror of
https://github.com/OrcaSlicer/OrcaSlicer.git
synced 2026-08-31 22:06:59 +00:00
Share one fuzzy matcher across both dialogs
Plugins dialog and the Speed Dial popup each carried a private copy of the same fuzzy matcher and had already drifted. Move the pure functions into a shared module loaded by both pages, and add a unit test beside it.
This commit is contained in:
@@ -17,6 +17,7 @@
|
|||||||
<script type="text/javascript" src="../js/common.js"></script>
|
<script type="text/javascript" src="../js/common.js"></script>
|
||||||
<script src="./index.js"></script>
|
<script src="./index.js"></script>
|
||||||
<script src="./plugin-sort.js"></script>
|
<script src="./plugin-sort.js"></script>
|
||||||
|
<script src="../js/fuzzy-search.js"></script>
|
||||||
<script src="./plugin-search.js"></script>
|
<script src="./plugin-search.js"></script>
|
||||||
</head>
|
</head>
|
||||||
<body onLoad="OnInit()">
|
<body onLoad="OnInit()">
|
||||||
|
|||||||
@@ -4,60 +4,14 @@ function PluginSearchActive() {
|
|||||||
return pluginSearch.query.length > 0;
|
return pluginSearch.query.length > 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
// --- matcher: fold per-character on the fly so matched offsets stay in ORIGINAL coordinates ---
|
// why: matcher (FoldChar/Norm/FuzzyRanges/WholeWordRanges) lives in shared ../js/fuzzy-search.js,
|
||||||
// why: highlighting marks slices of the original string; a separate folded string would desync offsets.
|
// loaded before this script - it is shared with the Speed Dial popup. Cc = pluginSearch.caseSensitive.
|
||||||
function FoldChar(ch) {
|
|
||||||
return ch.normalize("NFD").replace(/\p{Diacritic}/gu, ""); // accents always folded (both Cc states)
|
|
||||||
}
|
|
||||||
function Norm(ch, caseSensitive) {
|
|
||||||
const folded = FoldChar(ch);
|
|
||||||
return caseSensitive ? folded : folded.toLowerCase(); // Cc controls case only
|
|
||||||
}
|
|
||||||
function EscapeRegExp(value) {
|
|
||||||
return value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
||||||
}
|
|
||||||
|
|
||||||
function MatchText(text, query) {
|
function MatchText(text, query) {
|
||||||
if (!query)
|
if (!query)
|
||||||
return [];
|
return [];
|
||||||
return pluginSearch.wholeWord ? WholeWordRanges(text, query) : FuzzyRanges(text, query);
|
return pluginSearch.wholeWord
|
||||||
}
|
? WholeWordRanges(text, query, pluginSearch.caseSensitive)
|
||||||
|
: FuzzyRanges(text, query, pluginSearch.caseSensitive);
|
||||||
// Fuzzy: ordered subsequence. Builds ranges in original coordinates, merging adjacent runs on the fly.
|
|
||||||
function FuzzyRanges(text, query) {
|
|
||||||
const caseSensitive = pluginSearch.caseSensitive;
|
|
||||||
const needle = Array.from(query).map((ch) => Norm(ch, caseSensitive)).join("");
|
|
||||||
const ranges = [];
|
|
||||||
let qi = 0;
|
|
||||||
for (let i = 0; i < text.length && qi < needle.length; i++) {
|
|
||||||
if (Norm(text[i], caseSensitive) === needle[qi]) {
|
|
||||||
const last = ranges[ranges.length - 1];
|
|
||||||
if (last && last[1] === i)
|
|
||||||
last[1] = i + 1;
|
|
||||||
else
|
|
||||||
ranges.push([i, i + 1]);
|
|
||||||
qi++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return qi === needle.length ? ranges : null;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Whole word: literal \b-bounded match that bypasses fuzzy; Cc still applies. The per-char fold keeps the
|
|
||||||
// haystack length-aligned to the original text, so regex indices map straight back to original offsets.
|
|
||||||
// note: one-to-many folds (ligatures, eszett) shift offsets by a char; rare in plugin names, cosmetic only.
|
|
||||||
function WholeWordRanges(text, query) {
|
|
||||||
const caseSensitive = pluginSearch.caseSensitive;
|
|
||||||
const haystack = Array.from(text).map((ch) => Norm(ch, caseSensitive)).join("");
|
|
||||||
const needle = Array.from(query).map((ch) => Norm(ch, caseSensitive)).join("");
|
|
||||||
if (!needle)
|
|
||||||
return null;
|
|
||||||
const re = new RegExp(`\\b${EscapeRegExp(needle)}\\b`, "g");
|
|
||||||
const ranges = [];
|
|
||||||
let match;
|
|
||||||
// why: needle is non-empty, so \b-bounded matches are never zero-length - no empty-match guard needed.
|
|
||||||
while ((match = re.exec(haystack)) !== null)
|
|
||||||
ranges.push([match.index, match.index + match[0].length]);
|
|
||||||
return ranges.length > 0 ? ranges : null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Per-plugin evaluator consumed by RenderPlugins. The name text mirrors LabelCell's pluginLabelText so
|
// Per-plugin evaluator consumed by RenderPlugins. The name text mirrors LabelCell's pluginLabelText so
|
||||||
|
|||||||
@@ -7,6 +7,7 @@
|
|||||||
<link rel="stylesheet" href="./style.css" />
|
<link rel="stylesheet" href="./style.css" />
|
||||||
<link rel="stylesheet" type="text/css" href="../css/theme.css" />
|
<link rel="stylesheet" type="text/css" href="../css/theme.css" />
|
||||||
<script type="text/javascript" src="../../include/globalapi.js"></script>
|
<script type="text/javascript" src="../../include/globalapi.js"></script>
|
||||||
|
<script src="../../js/fuzzy-search.js"></script>
|
||||||
<script src="./speeddial.js"></script>
|
<script src="./speeddial.js"></script>
|
||||||
</head>
|
</head>
|
||||||
<body onload="OnInit()">
|
<body onload="OnInit()">
|
||||||
|
|||||||
@@ -9,38 +9,13 @@ var sel = { zone: "list", i: 0 }; // zone: 'list' | 'fav'
|
|||||||
var lastResizeHeight = 0;
|
var lastResizeHeight = 0;
|
||||||
var matchIndex = {};
|
var matchIndex = {};
|
||||||
|
|
||||||
function FoldChar(ch) {
|
// why: fuzzy matcher (FoldChar/Norm/FuzzyRanges) lives in shared ../../js/fuzzy-search.js, loaded before
|
||||||
return ch.normalize("NFD").replace(/\p{Diacritic}/gu, "");
|
// this script - it is shared with the Plugins dialog. Speed dial search is always case-insensitive.
|
||||||
}
|
|
||||||
|
|
||||||
function NormChar(ch) {
|
|
||||||
return FoldChar(ch).toLowerCase();
|
|
||||||
}
|
|
||||||
|
|
||||||
// element handles, assigned in OnInit (kept null so load-time touches no DOM)
|
// element handles, assigned in OnInit (kept null so load-time touches no DOM)
|
||||||
var qEl = null, listEl = null, favEl = null, clearEl = null, eyeEl = null, countEl = null;
|
var qEl = null, listEl = null, favEl = null, clearEl = null, eyeEl = null, countEl = null;
|
||||||
|
|
||||||
// ---- pure helpers (no DOM; unit-tested) -------------------------------------
|
// ---- pure helpers (no DOM; unit-tested) -------------------------------------
|
||||||
function fuzzyRanges(text, query) {
|
|
||||||
var t = text || "";
|
|
||||||
var q = query || "";
|
|
||||||
if (!q)
|
|
||||||
return null;
|
|
||||||
var needle = Array.from(q).map(function (c) { return NormChar(c); }).join("");
|
|
||||||
var ranges = [];
|
|
||||||
var qi = 0;
|
|
||||||
for (var i = 0; i < t.length && qi < needle.length; i++) {
|
|
||||||
if (NormChar(t[i]) === needle[qi]) {
|
|
||||||
if (ranges.length && ranges[ranges.length - 1][1] === i)
|
|
||||||
ranges[ranges.length - 1][1] = i + 1;
|
|
||||||
else
|
|
||||||
ranges.push([i, i + 1]);
|
|
||||||
qi++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return qi === needle.length ? ranges : null;
|
|
||||||
}
|
|
||||||
|
|
||||||
function filterActions(actions, query) {
|
function filterActions(actions, query) {
|
||||||
var q = (query || "").trim();
|
var q = (query || "").trim();
|
||||||
var list = actions || [];
|
var list = actions || [];
|
||||||
@@ -51,8 +26,8 @@ function filterActions(actions, query) {
|
|||||||
var out = [];
|
var out = [];
|
||||||
for (var i = 0; i < list.length; i++) {
|
for (var i = 0; i < list.length; i++) {
|
||||||
var a = list[i];
|
var a = list[i];
|
||||||
var titleMatch = fuzzyRanges(a.title, q);
|
var titleMatch = FuzzyRanges(a.title, q, false);
|
||||||
var pkgMatch = fuzzyRanges(a.pkg, q);
|
var pkgMatch = FuzzyRanges(a.pkg, q, false);
|
||||||
if (!titleMatch && !pkgMatch)
|
if (!titleMatch && !pkgMatch)
|
||||||
continue;
|
continue;
|
||||||
matchIndex[a.id] = { title: titleMatch, pkg: pkgMatch, useTitle: !!titleMatch };
|
matchIndex[a.id] = { title: titleMatch, pkg: pkgMatch, useTitle: !!titleMatch };
|
||||||
|
|||||||
58
resources/web/js/fuzzy-search.js
Normal file
58
resources/web/js/fuzzy-search.js
Normal file
@@ -0,0 +1,58 @@
|
|||||||
|
// Shared fuzzy-search core for the webview dialogs (Plugins dialog, Speed Dial popup).
|
||||||
|
// why: both pages carried their own copy of this matcher and had already drifted; one source of truth.
|
||||||
|
// note: keep this DOM-free and plain global-scope (no export/module) - it is loaded by <script src> in
|
||||||
|
// each page AND by a node vm.runInContext in the speed-dial logic test. It MUST be loaded before
|
||||||
|
// the page script that calls it.
|
||||||
|
|
||||||
|
// Fold per-character so matched offsets stay in ORIGINAL string coordinates (highlighting slices the
|
||||||
|
// original text; a separately-folded string would desync offsets).
|
||||||
|
function FoldChar(ch) {
|
||||||
|
return ch.normalize("NFD").replace(/\p{Diacritic}/gu, ""); // accents always folded
|
||||||
|
}
|
||||||
|
|
||||||
|
function Norm(ch, caseSensitive) {
|
||||||
|
const folded = FoldChar(ch);
|
||||||
|
return caseSensitive ? folded : folded.toLowerCase(); // case-sensitivity is the only toggle
|
||||||
|
}
|
||||||
|
|
||||||
|
function EscapeRegExp(value) {
|
||||||
|
return value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fuzzy: ordered subsequence. Builds ranges in original coordinates, merging adjacent runs on the fly.
|
||||||
|
function FuzzyRanges(text, query, caseSensitive) {
|
||||||
|
const t = text || "";
|
||||||
|
const needle = Array.from(query || "").map((ch) => Norm(ch, caseSensitive)).join("");
|
||||||
|
if (!needle)
|
||||||
|
return null;
|
||||||
|
const ranges = [];
|
||||||
|
let qi = 0;
|
||||||
|
for (let i = 0; i < t.length && qi < needle.length; i++) {
|
||||||
|
if (Norm(t[i], caseSensitive) === needle[qi]) {
|
||||||
|
const last = ranges[ranges.length - 1];
|
||||||
|
if (last && last[1] === i)
|
||||||
|
last[1] = i + 1;
|
||||||
|
else
|
||||||
|
ranges.push([i, i + 1]);
|
||||||
|
qi++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return qi === needle.length ? ranges : null;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Whole word: literal \b-bounded match that bypasses fuzzy. The per-char fold keeps the haystack
|
||||||
|
// length-aligned to the original text, so regex indices map straight back to original offsets.
|
||||||
|
// note: one-to-many folds (ligatures, eszett) shift offsets by a char; rare in names, cosmetic only.
|
||||||
|
function WholeWordRanges(text, query, caseSensitive) {
|
||||||
|
const haystack = Array.from(text || "").map((ch) => Norm(ch, caseSensitive)).join("");
|
||||||
|
const needle = Array.from(query || "").map((ch) => Norm(ch, caseSensitive)).join("");
|
||||||
|
if (!needle)
|
||||||
|
return null;
|
||||||
|
const re = new RegExp(`\\b${EscapeRegExp(needle)}\\b`, "g");
|
||||||
|
const ranges = [];
|
||||||
|
let match;
|
||||||
|
// why: needle is non-empty, so \b-bounded matches are never zero-length - no empty-match guard needed.
|
||||||
|
while ((match = re.exec(haystack)) !== null)
|
||||||
|
ranges.push([match.index, match.index + match[0].length]);
|
||||||
|
return ranges.length > 0 ? ranges : null;
|
||||||
|
}
|
||||||
37
resources/web/js/fuzzy-search.test.js
Normal file
37
resources/web/js/fuzzy-search.test.js
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
// Unit test for the shared fuzzy matcher. Run: node resources/web/js/fuzzy-search.test.js
|
||||||
|
// why: fuzzy-search.js is plain global-scope (no exports, so a browser <script src> works) - load it into
|
||||||
|
// a vm context the same way the page and the speed-dial test do, then assert against the globals.
|
||||||
|
const vm = require("vm"), assert = require("assert"), fs = require("fs");
|
||||||
|
const ctx = {};
|
||||||
|
vm.createContext(ctx);
|
||||||
|
vm.runInContext(fs.readFileSync(__dirname + "/fuzzy-search.js", "utf8"), ctx);
|
||||||
|
const { FoldChar, Norm, EscapeRegExp, FuzzyRanges, WholeWordRanges } = ctx;
|
||||||
|
|
||||||
|
// FoldChar / Norm: accents fold, case only folds when case-insensitive.
|
||||||
|
assert.equal(FoldChar("é"), "e");
|
||||||
|
assert.equal(Norm("É", false), "e");
|
||||||
|
assert.equal(Norm("É", true), "E");
|
||||||
|
|
||||||
|
// FuzzyRanges: ordered subsequence, ranges in ORIGINAL coordinates, adjacent runs merged.
|
||||||
|
assert.deepEqual(FuzzyRanges("Auto Arrange", "aa", false), [[0, 1], [5, 6]]);
|
||||||
|
assert.deepEqual(FuzzyRanges("Measure", "eas", false), [[1, 4]]); // contiguous run merges to one range
|
||||||
|
assert.equal(FuzzyRanges("Measure", "xyz", false), null); // no subsequence -> null
|
||||||
|
assert.equal(FuzzyRanges("Measure", "", false), null); // empty query -> null
|
||||||
|
|
||||||
|
// Accent-insensitive matching, offsets stay in the original (accented) string.
|
||||||
|
assert.deepEqual(FuzzyRanges("Café", "cafe", false), [[0, 4]]);
|
||||||
|
|
||||||
|
// Case sensitivity is the only toggle.
|
||||||
|
assert.equal(FuzzyRanges("Measure", "MEAS", true), null); // case-sensitive: no match
|
||||||
|
assert.deepEqual(FuzzyRanges("Measure", "Meas", true), [[0, 4]]); // case-sensitive: matches exact case
|
||||||
|
|
||||||
|
// WholeWordRanges: \b-bounded literal, bypasses fuzzy. Substring inside a word does NOT match.
|
||||||
|
assert.deepEqual(WholeWordRanges("Auto Arrange", "arrange", false), [[5, 12]]);
|
||||||
|
assert.equal(WholeWordRanges("Rearrange", "arrange", false), null); // not on a word boundary
|
||||||
|
assert.deepEqual(WholeWordRanges("a.b", "a", false), [[0, 1]]); // '.' is a boundary
|
||||||
|
|
||||||
|
// EscapeRegExp: regex metachars in the query are treated literally by whole-word.
|
||||||
|
assert.equal(EscapeRegExp("a.b*"), "a\\.b\\*");
|
||||||
|
assert.deepEqual(WholeWordRanges("c++ tool", "c", false), [[0, 1]]); // '+' would be a regex error unescaped
|
||||||
|
|
||||||
|
console.log("ok");
|
||||||
Reference in New Issue
Block a user