@minnalstack/editor-plugin-find 0.0.0-stage → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,31 @@
1
+ import type { DeltaInsertJson, DocumentReader, TextPosition, TextRange } from '@minnalstack/editor-core';
2
+ /** The most matches one search finds. It bounds the work after each change, and what is painted, so a search for "e" in a long document stays quick. */
3
+ export declare const matchLimit = 1000;
4
+ /** How text has to compare to count as a match. */
5
+ export interface SearchOptions {
6
+ /** Upper and lower case differ. */
7
+ readonly matchCase: boolean;
8
+ /** The match is not part of a longer word: no letter, digit or underscore touches it. */
9
+ readonly wholeWord: boolean;
10
+ }
11
+ /** What a search found, in the order of the document. */
12
+ export interface Found {
13
+ readonly matches: readonly TextRange[];
14
+ /** The search stopped at `matchLimit`, and there are more. */
15
+ readonly hasMore: boolean;
16
+ /** The first match at or after the position it was asked about, or the first of all if none is after it, or -1 if there are none. */
17
+ readonly first: number;
18
+ }
19
+ /**
20
+ * The expression that finds `query`, or null when nothing can match. The `u` flag compares case by Unicode's rules without changing the length of the text, so an offset in
21
+ * the text is an offset in the block.
22
+ */
23
+ export declare function patternOf(query: string, options: SearchOptions): RegExp | null;
24
+ /**
25
+ * Every match in the document, block by block, so that none crosses from one block to the next. It reads blocks in order and stops at `matchLimit`, so its cost is the
26
+ * text up to that match. `from` is where the search starts for `first`, such as the caret.
27
+ */
28
+ export declare function searchDocument(document: DocumentReader, pattern: RegExp, from: TextPosition | null): Found;
29
+ /** The formats of the character at `offset` in a block's content, for text that takes its place. A format with no value is left out, because it means none. */
30
+ export declare function formatsAt(inline: readonly DeltaInsertJson[], offset: number): DeltaInsertJson['attributes'];
31
+ //# sourceMappingURL=search.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"search.d.ts","sourceRoot":"","sources":["../src/search.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,eAAe,EAAE,cAAc,EAAE,YAAY,EAAE,SAAS,EAAE,MAAM,0BAA0B,CAAC;AAEzG,wJAAwJ;AACxJ,eAAO,MAAM,UAAU,OAAO,CAAC;AAE/B,mDAAmD;AACnD,MAAM,WAAW,aAAa;IAC5B,mCAAmC;IACnC,QAAQ,CAAC,SAAS,EAAE,OAAO,CAAC;IAC5B,yFAAyF;IACzF,QAAQ,CAAC,SAAS,EAAE,OAAO,CAAC;CAC7B;AAED,yDAAyD;AACzD,MAAM,WAAW,KAAK;IACpB,QAAQ,CAAC,OAAO,EAAE,SAAS,SAAS,EAAE,CAAC;IACvC,8DAA8D;IAC9D,QAAQ,CAAC,OAAO,EAAE,OAAO,CAAC;IAC1B,qIAAqI;IACrI,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;CACxB;AASD;;;GAGG;AACH,wBAAgB,SAAS,CAAC,KAAK,EAAE,MAAM,EAAE,OAAO,EAAE,aAAa,GAAG,MAAM,GAAG,IAAI,CAK9E;AAED;;;GAGG;AACH,wBAAgB,cAAc,CAAC,QAAQ,EAAE,cAAc,EAAE,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,YAAY,GAAG,IAAI,GAAG,KAAK,CAgB1G;AAED,+JAA+J;AAC/J,wBAAgB,SAAS,CAAC,MAAM,EAAE,SAAS,eAAe,EAAE,EAAE,MAAM,EAAE,MAAM,GAAG,eAAe,CAAC,YAAY,CAAC,CAW3G"}
package/dist/search.js ADDED
@@ -0,0 +1,57 @@
1
+ /** The most matches one search finds. It bounds the work after each change, and what is painted, so a search for "e" in a long document stays quick. */
2
+ export const matchLimit = 1000;
3
+ /** How `Block.text` shows an embedded item, such as an image in the text. It is not text, so nothing matches it. */
4
+ const embeddedItem = '';
5
+ /** What a regular expression would read as more than a character. Only these are escaped, because the `u` flag refuses an escape of anything else. */
6
+ const special = /[\\^$.*+?()[\]{}|/]/g;
7
+ /** What a word is made of, in any script. */
8
+ const wordCharacter = '[\\p{L}\\p{M}\\p{N}_]';
9
+ /**
10
+ * The expression that finds `query`, or null when nothing can match. The `u` flag compares case by Unicode's rules without changing the length of the text, so an offset in
11
+ * the text is an offset in the block.
12
+ */
13
+ export function patternOf(query, options) {
14
+ if (query === '' || query.includes(embeddedItem))
15
+ return null;
16
+ const text = query.replace(special, '\\$&');
17
+ const source = options.wholeWord ? `(?<!${wordCharacter})${text}(?!${wordCharacter})` : text;
18
+ return new RegExp(source, options.matchCase ? 'gu' : 'giu');
19
+ }
20
+ /**
21
+ * Every match in the document, block by block, so that none crosses from one block to the next. It reads blocks in order and stops at `matchLimit`, so its cost is the
22
+ * text up to that match. `from` is where the search starts for `first`, such as the caret.
23
+ */
24
+ export function searchDocument(document, pattern, from) {
25
+ const matches = [];
26
+ let first = -1;
27
+ let isPast = false;
28
+ for (const block of document.blocks()) {
29
+ const startsAt = from !== null && block.id === from.block ? from.offset : null;
30
+ const text = block.text;
31
+ pattern.lastIndex = 0;
32
+ for (let found = pattern.exec(text); found !== null; found = pattern.exec(text)) {
33
+ if (matches.length === matchLimit)
34
+ return { matches, hasMore: true, first: Math.max(first, 0) };
35
+ matches.push({ block: block.id, from: found.index, to: found.index + found[0].length });
36
+ if (first < 0 && (isPast || (startsAt !== null && found.index >= startsAt)))
37
+ first = matches.length - 1;
38
+ }
39
+ if (startsAt !== null)
40
+ isPast = true;
41
+ }
42
+ return { matches, hasMore: false, first: first >= 0 || matches.length === 0 ? first : 0 };
43
+ }
44
+ /** The formats of the character at `offset` in a block's content, for text that takes its place. A format with no value is left out, because it means none. */
45
+ export function formatsAt(inline, offset) {
46
+ let at = 0;
47
+ for (const op of inline) {
48
+ const length = typeof op.insert === 'string' ? op.insert.length : 1;
49
+ if (offset < at + length) {
50
+ const formats = Object.entries(op.attributes ?? {}).filter(([, value]) => value !== null);
51
+ return formats.length === 0 ? undefined : Object.fromEntries(formats);
52
+ }
53
+ at += length;
54
+ }
55
+ return undefined;
56
+ }
57
+ //# sourceMappingURL=search.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"search.js","sourceRoot":"","sources":["../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AC,CAAC,CAAC,EAAE,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC;QACpE,IAAI,MAAM,GAAG,EAAE,GAAG,MAAM,EAAE,CAAC;YACzB,MAAM,OAAO,GAAG,MAAM,CAAC,OAAO,CAAC,EAAE,CAAC,UAAU,IAAI,EAAE,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,KAAK,CAAC,EAAE,EAAE,CAAC,KAAK,KAAK,IAAI,CAAC,CAAC;YAC1F,OAAO,OAAO,CAAC,MAAM,KAAK,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,MAAM,CAAC,WAAW,CAAC,OAAO,CAAC,CAAC;QACxE,CAAC;QACD,EAAE,IAAI,MAAM,CAAC;IACf,CAAC;IACD,OAAO,SAAS,CAAC;AACnB,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,34 @@
1
1
  {
2
2
  "name": "@minnalstack/editor-plugin-find",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.3.0",
4
+ "license": "MIT",
5
+ "description": "Find and replace, in a panel of Aranghat's components beside the editor, with matches painted by the CSS Custom Highlight API.",
6
+ "type": "module",
7
+ "exports": {
8
+ ".": {
9
+ "types": "./dist/index.d.ts",
10
+ "default": "./dist/index.js"
11
+ }
12
+ },
13
+ "types": "./dist/index.d.ts",
14
+ "files": [
15
+ "dist"
16
+ ],
17
+ "sideEffects": false,
18
+ "scripts": {
19
+ "build": "tsc -p tsconfig.json"
20
+ },
21
+ "peerDependencies": {
22
+ "@aranghat/base": "^3.1.0",
23
+ "@minnalstack/icons": "^3.1.0",
24
+ "@minnalstack/editor-core": "^0.3.0"
25
+ },
26
+ "devDependencies": {
27
+ "@aranghat/base": "3.1.0",
28
+ "@minnalstack/icons": "3.1.0"
29
+ },
30
+ "publishConfig": {
31
+ "access": "public",
32
+ "registry": "https://registry.npmjs.org/"
33
+ }
34
+ }