@coze-editor/extension-links 0.1.0-alpha.0fd19e

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.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 coze-dev
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,82 @@
1
+ // src/index.ts
2
+ import {
3
+ Decoration,
4
+ EditorView,
5
+ ViewPlugin
6
+ } from "@codemirror/view";
7
+ import { Prec } from "@codemirror/state";
8
+ async function decorateURL(view2, linksProvider) {
9
+ const links2 = await linksProvider(view2);
10
+ let set = Decoration.none;
11
+ set = set.update({
12
+ add: links2.map(
13
+ ({ target, range }) => Decoration.mark({
14
+ class: "cm-link",
15
+ attributes: {
16
+ "data-link": target
17
+ }
18
+ }).range(range.from, range.to)
19
+ )
20
+ });
21
+ return set;
22
+ }
23
+ var view = (linksProvider, delay) => [
24
+ ViewPlugin.fromClass(
25
+ class {
26
+ controller = null;
27
+ decorations = Decoration.none;
28
+ constructor(view2) {
29
+ this.schedule(view2);
30
+ }
31
+ schedule(view2) {
32
+ if (this.controller) {
33
+ this.controller.abort();
34
+ this.controller = null;
35
+ }
36
+ const controller = new AbortController();
37
+ const { signal } = controller;
38
+ this.controller = controller;
39
+ setTimeout(() => {
40
+ if (signal.aborted) {
41
+ return;
42
+ }
43
+ decorateURL(view2, linksProvider).then((decorations) => {
44
+ if (signal.aborted) {
45
+ return;
46
+ }
47
+ this.decorations = decorations;
48
+ view2.dispatch({
49
+ userEvent: "sdk.links.update"
50
+ });
51
+ this.controller = null;
52
+ });
53
+ }, delay);
54
+ }
55
+ update(update) {
56
+ if (update.docChanged) {
57
+ this.decorations = this.decorations.map(update.changes);
58
+ this.schedule(update.view);
59
+ }
60
+ }
61
+ },
62
+ {
63
+ provide(plugin) {
64
+ return Prec.highest(
65
+ EditorView.decorations.of(
66
+ (view2) => {
67
+ var _a;
68
+ return ((_a = view2.plugin(plugin)) == null ? void 0 : _a.decorations) ?? Decoration.none;
69
+ }
70
+ )
71
+ );
72
+ }
73
+ }
74
+ )
75
+ ];
76
+ function links(linksProvider, delay = 500) {
77
+ return [view(linksProvider, delay)];
78
+ }
79
+ export {
80
+ links
81
+ };
82
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/index.ts"],"sourcesContent":["// Copyright (c) 2025 coze-dev\n// SPDX-License-Identifier: MIT\n\nimport {\n Decoration,\n type DecorationSet,\n EditorView,\n ViewPlugin,\n type ViewUpdate,\n} from '@codemirror/view';\nimport { type Extension, Prec } from '@codemirror/state';\n\nexport interface Link {\n target: string;\n range: {\n from: number;\n to: number;\n };\n}\nexport type LinksProvider = (view: EditorView) => Promise<Link[]>;\n\nasync function decorateURL(\n view: EditorView,\n linksProvider: LinksProvider,\n): Promise<DecorationSet> {\n const links = await linksProvider(view);\n\n let set = Decoration.none;\n set = set.update({\n add: links.map(({ target, range }) =>\n Decoration.mark({\n class: 'cm-link',\n attributes: {\n 'data-link': target,\n },\n }).range(range.from, range.to),\n ),\n });\n\n return set;\n}\n\nconst view = (linksProvider: LinksProvider, delay: number): Extension => [\n ViewPlugin.fromClass(\n class {\n private controller: AbortController | null = null;\n public decorations: DecorationSet = Decoration.none;\n\n constructor(view: EditorView) {\n this.schedule(view);\n }\n\n private schedule(view: EditorView) {\n if (this.controller) {\n this.controller.abort();\n this.controller = null;\n }\n\n const controller = new AbortController();\n const { signal } = controller;\n this.controller = controller;\n\n setTimeout(() => {\n if (signal.aborted) {\n return;\n }\n\n decorateURL(view, linksProvider).then(decorations => {\n if (signal.aborted) {\n return;\n }\n\n this.decorations = decorations;\n\n view.dispatch({\n userEvent: 'sdk.links.update',\n });\n\n this.controller = null;\n });\n }, delay);\n }\n\n update(update: ViewUpdate) {\n if (update.docChanged) {\n this.decorations = this.decorations.map(update.changes);\n this.schedule(update.view);\n }\n }\n },\n {\n provide(plugin) {\n // 优先级设置为最高,decoration 出现在最里面\n return Prec.highest(\n EditorView.decorations.of(\n view => view.plugin(plugin)?.decorations ?? Decoration.none,\n ),\n );\n },\n },\n ),\n];\n\nfunction links(linksProvider: LinksProvider, delay = 500) {\n return [view(linksProvider, delay)];\n}\n\nexport { links };\n"],"mappings":";AAGA;AAAA,EACE;AAAA,EAEA;AAAA,EACA;AAAA,OAEK;AACP,SAAyB,YAAY;AAWrC,eAAe,YACbA,OACA,eACwB;AACxB,QAAMC,SAAQ,MAAM,cAAcD,KAAI;AAEtC,MAAI,MAAM,WAAW;AACrB,QAAM,IAAI,OAAO;AAAA,IACf,KAAKC,OAAM;AAAA,MAAI,CAAC,EAAE,QAAQ,MAAM,MAC9B,WAAW,KAAK;AAAA,QACd,OAAO;AAAA,QACP,YAAY;AAAA,UACV,aAAa;AAAA,QACf;AAAA,MACF,CAAC,EAAE,MAAM,MAAM,MAAM,MAAM,EAAE;AAAA,IAC/B;AAAA,EACF,C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
2
+ import { Extension } from '@codemirror/state';
3
+
4
+ interface Link {
5
+ target: string;
6
+ range: {
7
+ from: number;
8
+ to: number;
9
+ };
10
+ }
11
+ type LinksProvider = (view: EditorView) => Promise<Link[]>;
12
+ declare function links(linksProvider: LinksProvider, delay?: number): Extension[];
13
+
14
+ export { type Link, type LinksProvider, links };
@@ -0,0 +1,14 @@
1
+ import { EditorView } from '@codemirror/view';
2
+ import { Extension } from '@codemirror/state';
3
+
4
+ interface Link {
5
+ target: string;
6
+ range: {
7
+ from: number;
8
+ to: number;
9
+ };
10
+ }
11
+ type LinksProvider = (view: EditorView) => Promise<Link[]>;
12
+ declare function links(linksProvider: LinksProvider, delay?: number): Extension[];
13
+
14
+ export { type Link, type LinksProvider, links };
package/dist/index.js ADDED
@@ -0,0 +1,102 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __export = (target, all) => {
6
+ for (var name in all)
7
+ __defProp(target, name, { get: all[name], enumerable: true });
8
+ };
9
+ var __copyProps = (to, from, except, desc) => {
10
+ if (from && typeof from === "object" || typeof from === "function") {
11
+ for (let key of __getOwnPropNames(from))
12
+ if (!__hasOwnProp.call(to, key) && key !== except)
13
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
14
+ }
15
+ return to;
16
+ };
17
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18
+
19
+ // src/index.ts
20
+ var index_exports = {};
21
+ __export(index_exports, {
22
+ links: () => links
23
+ });
24
+ module.exports = __toCommonJS(index_exports);
25
+ var import_view = require("@codemirror/view");
26
+ var import_state = require("@codemirror/state");
27
+ async function decorateURL(view2, linksProvider) {
28
+ const links2 = await linksProvider(view2);
29
+ let set = import_view.Decoration.none;
30
+ set = set.update({
31
+ add: links2.map(
32
+ ({ target, range }) => import_view.Decoration.mark({
33
+ class: "cm-link",
34
+ attributes: {
35
+ "data-link": target
36
+ }
37
+ }).range(range.from, range.to)
38
+ )
39
+ });
40
+ return set;
41
+ }
42
+ var view = (linksProvider, delay) => [
43
+ import_view.ViewPlugin.fromClass(
44
+ class {
45
+ controller = null;
46
+ decorations = import_view.Decoration.none;
47
+ constructor(view2) {
48
+ this.schedule(view2);
49
+ }
50
+ schedule(view2) {
51
+ if (this.controller) {
52
+ this.controller.abort();
53
+ this.controller = null;
54
+ }
55
+ const controller = new AbortController();
56
+ const { signal } = controller;
57
+ this.controller = controller;
58
+ setTimeout(() => {
59
+ if (signal.aborted) {
60
+ return;
61
+ }
62
+ decorateURL(view2, linksProvider).then((decorations) => {
63
+ if (signal.aborted) {
64
+ return;
65
+ }
66
+ this.decorations = decorations;
67
+ view2.dispatch({
68
+ userEvent: "sdk.links.update"
69
+ });
70
+ this.controller = null;
71
+ });
72
+ }, delay);
73
+ }
74
+ update(update) {
75
+ if (update.docChanged) {
76
+ this.decorations = this.decorations.map(update.changes);
77
+ this.schedule(update.view);
78
+ }
79
+ }
80
+ },
81
+ {
82
+ provide(plugin) {
83
+ return import_state.Prec.highest(
84
+ import_view.EditorView.decorations.of(
85
+ (view2) => {
86
+ var _a;
87
+ return ((_a = view2.plugin(plugin)) == null ? void 0 : _a.decorations) ?? import_view.Decoration.none;
88
+ }
89
+ )
90
+ );
91
+ }
92
+ }
93
+ )
94
+ ];
95
+ function links(linksProvider, delay = 500) {
96
+ return [view(linksProvider, delay)];
97
+ }
98
+ // Annotate the CommonJS export names for ESM import in node:
99
+ 0 && (module.exports = {
100
+ links
101
+ });
102
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/index.ts"],"sourcesContent":["// Copyright (c) 2025 coze-dev\n// SPDX-License-Identifier: MIT\n\nimport {\n Decoration,\n type DecorationSet,\n EditorView,\n ViewPlugin,\n type ViewUpdate,\n} from '@codemirror/view';\nimport { type Extension, Prec } from '@codemirror/state';\n\nexport interface Link {\n target: string;\n range: {\n from: number;\n to: number;\n };\n}\nexport type LinksProvider = (view: EditorView) => Promise<Link[]>;\n\nasync function decorateURL(\n view: EditorView,\n linksProvider: LinksProvider,\n): Promise<DecorationSet> {\n const links = await linksProvider(view);\n\n let set = Decoration.none;\n set = set.update({\n add: links.map(({ target, range }) =>\n Decoration.mark({\n class: 'cm-link',\n attributes: {\n 'data-link': target,\n },\n }).range(range.from, range.to),\n ),\n });\n\n return set;\n}\n\nconst view = (linksProvider: LinksProvider, delay: number): Extension => [\n ViewPlugin.fromClass(\n class {\n private controller: AbortController | null = null;\n public decorations: DecorationSet = Decoration.none;\n\n constructor(view: EditorView) {\n this.schedule(view);\n }\n\n private schedule(view: EditorView) {\n if (this.controller) {\n this.controller.abort();\n this.controller = null;\n }\n\n const controller = new AbortController();\n const { signal } = controller;\n this.controller = controller;\n\n setTimeout(() => {\n if (signal.aborted) {\n return;\n }\n\n decorateURL(view, linksProvider).then(decorations => {\n if (signal.aborted) {\n return;\n }\n\n this.decorations = decorations;\n\n view.dispatch({\n userEvent: 'sdk.links.update',\n });\n\n this.controller = null;\n });\n }, delay);\n }\n\n update(update: ViewUpdate) {\n if (update.docChanged) {\n this.decorations = this.decorations.map(update.changes);\n this.schedule(update.view);\n }\n }\n },\n {\n provide(plugin) {\n // 优先级设置为最高,decoration 出现在最里面\n return Prec.highest(\n EditorView.decorations.of(\n view => view.plugin(plugin)?.decorations ?? Decoration.none,\n ),\n );\n },\n },\n ),\n];\n\nfunction links(linksProvider: LinksProvider, delay = 500) {\n return [view(linksProvider, delay)];\n}\n\nexport { links };\n"],"mappings":";;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAGA,kBAMO;AACP,mBAAqC;AAWrC,eAAe,YACbA,OACA,eACwB;AACxB,QAAMC,SAAQ,MAAM,cAAcD,KAAI;AAEtC,MAAI,MAAM,uBAAW;AACrB,QAAM,IA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lB,mBAAqB,gBAAe,uBAAW;AAAA;AAAA,UACzD;AAAA,QACF;AAAA,MACF;AAAA,IACF;AAAA,EACF;AACF;AAEA,SAAS,MAAM,eAA8B,QAAQ,KAAK;AACxD,SAAO,CAAC,KAAK,eAAe,KAAK,CAAC;AACpC;","names":["view","links"]}
package/package.json ADDED
@@ -0,0 +1,43 @@
1
+ {
2
+ "name": "@coze-editor/extension-links",
3
+ "version": "0.1.0-alpha.0fd19e",
4
+ "description": "extension-links",
5
+ "license": "MIT",
6
+ "author": "fengzilong",
7
+ "maintainers": [],
8
+ "sideEffects": [
9
+ "**/*.css",
10
+ "**/*.less",
11
+ "**/*.sass",
12
+ "**/*.scss"
13
+ ],
14
+ "main": "./dist/esm/index.js",
15
+ "module": "./dist/esm/index.js",
16
+ "types": "./dist/index.d.ts",
17
+ "files": [
18
+ "dist"
19
+ ],
20
+ "scripts": {
21
+ "build": "tsup",
22
+ "lint": "eslint && tsc --noEmit"
23
+ },
24
+ "devDependencies": {
25
+ "@codemirror/state": "^6.4.1",
26
+ "@codemirror/view": "^6.26.1",
27
+ "@coze-arch/ts-config": "workspace:*",
28
+ "@coze-editor/eslint-config": "workspace:*",
29
+ "@types/node": "^22",
30
+ "eslint": "9.14.0",
31
+ "tsup": "^8.0.1",
32
+ "typescript": "^5.8.2"
33
+ },
34
+ "peerDependencies": {
35
+ "@codemirror/state": "^6.4.1",
36
+ "@codemirror/view": "^6.26.1"
37
+ },
38
+ "publishConfig": {
39
+ "access": "public",
40
+ "registry": "https://registry.npmjs.org"
41
+ },
42
+ "test:main": "./src/index.ts"
43
+ }