@coze-editor/extension-placeholder 0.1.0-alpha.09ffeb

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,178 @@
1
+ // src/placeholder.ts
2
+ import { FacetCombineStrategy } from "@coze-editor/utils";
3
+ import {
4
+ Decoration,
5
+ ViewPlugin,
6
+ WidgetType
7
+ } from "@codemirror/view";
8
+ import { Facet } from "@codemirror/state";
9
+
10
+ // src/dom.ts
11
+ var scratchRange;
12
+ function textRange(node, from, to = from) {
13
+ const range = scratchRange || (scratchRange = document.createRange());
14
+ range.setEnd(node, to);
15
+ range.setStart(node, from);
16
+ return range;
17
+ }
18
+ function flattenRect(rect, left) {
19
+ const x = left ? rect.left : rect.right;
20
+ return { left: x, right: x, top: rect.top, bottom: rect.bottom };
21
+ }
22
+ function clientRectsFor(dom) {
23
+ if (dom.nodeType == 3) {
24
+ return textRange(dom, 0, dom.nodeValue.length).getClientRects();
25
+ } else if (dom.nodeType == 1) {
26
+ return dom.getClientRects();
27
+ } else {
28
+ return [];
29
+ }
30
+ }
31
+
32
+ // src/placeholder.ts
33
+ function createPlaceholderWidgetClass() {
34
+ return class Placeholder extends WidgetType {
35
+ constructor(content) {
36
+ super();
37
+ this.content = content;
38
+ }
39
+ toDOM(view) {
40
+ const wrap = document.createElement("span");
41
+ wrap.className = "cm-placeholder";
42
+ wrap.style.cssText = `
43
+ height: 0;
44
+ `;
45
+ wrap.appendChild(
46
+ typeof this.content === "string" ? document.createTextNode(this.content) : typeof this.content === "function" ? this.content(view) : this.content
47
+ );
48
+ if (typeof this.content === "string") {
49
+ wrap.setAttribute("aria-label", `placeholder ${this.content}`);
50
+ } else {
51
+ wrap.setAttribute("aria-hidden", "true");
52
+ }
53
+ return wrap;
54
+ }
55
+ coordsAt(dom) {
56
+ const rects = dom.firstChild ? clientRectsFor(dom.firstChild) : [];
57
+ if (!rects.length) {
58
+ return null;
59
+ }
60
+ const style = window.getComputedStyle(dom.parentNode);
61
+ const rect = flattenRect(rects[0], style.direction != "rtl");
62
+ const lineHeight = parseInt(style.lineHeight);
63
+ if (rect.bottom - rect.top > lineHeight * 1.5) {
64
+ return {
65
+ left: rect.left,
66
+ right: rect.right,
67
+ top: rect.top,
68
+ bottom: rect.top + lineHeight
69
+ };
70
+ }
71
+ return rect;
72
+ }
73
+ ignoreEvent() {
74
+ return false;
75
+ }
76
+ };
77
+ }
78
+ var Placeholder = createPlaceholderWidgetClass();
79
+ var contentFacet = Facet.define({
80
+ combine: FacetCombineStrategy.Last
81
+ });
82
+ var placeholderView = ViewPlugin.fromClass(
83
+ class PlaceholderView {
84
+ constructor(view) {
85
+ this.view = view;
86
+ const content = view.state.facet(contentFacet);
87
+ this.placeholder = this.getPlaceholder(content);
88
+ }
89
+ placeholder;
90
+ content;
91
+ update(update) {
92
+ const content = update.view.state.facet(contentFacet);
93
+ if (this.content !== content) {
94
+ this.placeholder = this.getPlaceholder(content);
95
+ this.content = content;
96
+ }
97
+ }
98
+ getPlaceholder(content) {
99
+ return content ? Decoration.set([
100
+ Decoration.widget({
101
+ widget: new Placeholder(content),
102
+ side: 1
103
+ }).range(0)
104
+ ]) : Decoration.none;
105
+ }
106
+ get decorations() {
107
+ return this.view.state.doc.length ? Decoration.none : this.placeholder;
108
+ }
109
+ },
110
+ {
111
+ decorations: (v) => v.decorations
112
+ }
113
+ );
114
+ function placeholder(content) {
115
+ return [placeholderView, contentFacet.of(content)];
116
+ }
117
+
118
+ // src/active-line-placeholder.ts
119
+ import { FacetCombineStrategy as FacetCombineStrategy2 } from "@coze-editor/utils";
120
+ import {
121
+ Decoration as Decoration2,
122
+ ViewPlugin as ViewPlugin2
123
+ } from "@codemirror/view";
124
+ import { Facet as Facet2 } from "@codemirror/state";
125
+ var Placeholder2 = createPlaceholderWidgetClass();
126
+ var contentFacet2 = Facet2.define({
127
+ combine: FacetCombineStrategy2.Last
128
+ });
129
+ var activeLinePlaceholderView = ViewPlugin2.fromClass(
130
+ class {
131
+ decorations = Decoration2.none;
132
+ constructor(view) {
133
+ if (view.hasFocus) {
134
+ this.decorations = getPlaceholderDecoration(view.state);
135
+ } else {
136
+ this.decorations = Decoration2.none;
137
+ }
138
+ }
139
+ update(update) {
140
+ if (!update.view.hasFocus) {
141
+ this.decorations = Decoration2.none;
142
+ return;
143
+ }
144
+ if (update.selectionSet || update.docChanged) {
145
+ this.decorations = getPlaceholderDecoration(update.state);
146
+ }
147
+ }
148
+ },
149
+ {
150
+ decorations(plugin) {
151
+ return plugin.decorations;
152
+ }
153
+ }
154
+ );
155
+ function getPlaceholderDecoration(state) {
156
+ const pos = state.selection.main.head;
157
+ if (state.selection.main.empty) {
158
+ const line = state.doc.lineAt(pos);
159
+ if (line && line.length === 0) {
160
+ const content = state.facet(contentFacet2);
161
+ return Decoration2.set([
162
+ Decoration2.widget({
163
+ widget: new Placeholder2(content),
164
+ side: 1
165
+ }).range(line.from)
166
+ ]);
167
+ }
168
+ }
169
+ return Decoration2.none;
170
+ }
171
+ var activeLinePlaceholder = function(content) {
172
+ return [activeLinePlaceholderView, contentFacet2.of(content)];
173
+ };
174
+ export {
175
+ activeLinePlaceholder,
176
+ placeholder
177
+ };
178
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/placeholder.ts","../../src/dom.ts","../../src/active-line-placeholder.ts"],"sourcesContent":["// Copyright (c) 2025 coze-dev\n// SPDX-License-Identifier: MIT\n\n// modified from @codemirror/view\n// updates:\n// - support handle user events(remove pointerEvents = \"none\", remove cloneNode)\n// - use single view\n// - support multiple content sources(use last one)\n\nimport { FacetCombineStrategy } from '@coze-editor/utils';\nimport {\n Decoration,\n type DecorationSet,\n type EditorView,\n ViewPlugin,\n type ViewUpdate,\n WidgetType,\n} from '@codemirror/view';\nimport { type Extension, Facet } from '@codemirror/state';\n\nimport { clientRectsFor, flattenRect } from './dom';\n\nfunction createPlaceholderWidgetClass() {\n return class Placeholder extends WidgetType {\n constructor(\n readonly content:\n | string\n | HTMLElement\n | ((view: EditorView) => HTMLElement),\n ) {\n super();\n }\n\n toDOM(view: EditorView) {\n const wrap = document.createElement('span');\n wrap.className = 'cm-placeholder';\n // solved the cursor height problem(eg. cursor double height with two lines)\n wrap.style.cssText = `\n height: 0;\n `;\n // wrap.style.pointerEvents = \"none\"\n wrap.appendChild(\n typeof this.content === 'string'\n ? document.createTextNode(this.content)\n : typeof this.content === 'function'\n ? this.content(view)\n : this.content,\n );\n if (typeof this.content === 'string') {\n wrap.setAttribute('aria-label', `placeholder ${this.content}`);\n } else {\n wrap.setAttribute('aria-hidden', 'true');\n }\n return wrap;\n }\n\n coordsAt(dom: HTMLElement) {\n const rects = dom.firstChild ? clientRectsFor(dom.firstChild) : [];\n if (!rects.length) {\n return null;\n }\n const style = window.getComputedStyle(dom.parentNode as HTMLElement);\n const rect = flattenRect(rects[0], style.direction != 'rtl');\n const lineHeight = parseInt(style.lineHeight);\n if (rect.bottom - rect.top > lineHeight * 1.5) {\n return {\n left: rect.left,\n right: rect.right,\n top: rect.top,\n bottom: rect.top + lineHeight,\n };\n }\n return rect;\n }\n\n ignoreEvent() {\n return false;\n }\n };\n}\n\nconst Placeholder = createPlaceholderWidgetClass();\n\ntype Content = string | HTMLElement | ((view: EditorView) => HTMLElement);\nconst contentFacet = Facet.define({\n combine: FacetCombineStrategy.Last<Content>,\n});\n\nconst placeholderView = ViewPlugin.fromClass(\n class PlaceholderView {\n private placeholder: DecorationSet;\n private content: Content | undefined;\n\n constructor(private readonly view: EditorView) {\n const content = view.state.facet(contentFacet);\n this.placeholder = this.getPlaceholder(content);\n }\n\n update(update: ViewUpdate) {\n const content = update.view.state.facet(contentFacet);\n\n if (this.content !== content) {\n this.placeholder = this.getPlaceholder(content);\n this.content = content;\n }\n }\n\n getPlaceholder(content: Content) {\n return content\n ? Decoration.set([\n Decoration.widget({\n widget: new Placeholder(content),\n side: 1,\n }).range(0),\n ])\n : Decoration.none;\n }\n\n get decorations() {\n return this.view.state.doc.length ? Decoration.none : this.placeholder;\n }\n },\n {\n decorations: v => v.decorations,\n },\n);\n\nfunction placeholder(content: Content): Extension {\n return [placeholderView, contentFacet.of(content)];\n}\n\nexport { placeholder, createPlaceholderWidgetClass };\n","// Copyright (c) 2025 coze-dev\n// SPDX-License-Identifier: MIT\n\n/// Basic rectangle type.\ninterface Rect {\n readonly left: number;\n readonly right: number;\n readonly top: number;\n readonly bottom: number;\n}\n\nlet scratchRange: Range | null;\n\nfunction textRange(node: Text, from: number, to = from) {\n const range = scratchRange || (scratchRange = document.createRange());\n range.setEnd(node, to);\n range.setStart(node, from);\n return range;\n}\n\nfunction flattenRect(rect: Rect, left: boolean) {\n const x = left ? rect.left : rect.right;\n return { left: x, right: x, top: rect.top, bottom: rect.bottom };\n}\n\nfunction clientRectsFor(dom: Node) {\n if (dom.nodeType == 3) {\n return textRange(dom as Text, 0, dom.nodeValue!.length).getClientRects();\n } else if (dom.nodeType == 1) {\n return (dom as HTMLElement).getClientRects();\n } else {\n return [] as any as DOMRectList;\n }\n}\n\nexport { flattenRect, clientRectsFor };\n\nexport type { Rect };\n","// Copyright (c) 2025 coze-dev\n// SPDX-License-Identifier: MIT\n\nimport { FacetCombineStrategy } from '@coze-editor/utils';\nimport {\n Decoration,\n type EditorView,\n ViewPlugin,\n type ViewUpdate,\n} from '@codemirror/view';\nimport { type EditorState, type Extension, Facet } from '@codemirror/state';\n\nimport { createPlaceholderWidgetClass } from './placeholder';\n\n// isolate Widget class with Placeholder to avoid widget reusable compare when using Placeholder and ActiveLinePlaceholder together\nconst Placeholder = createPlaceholderWidgetClass();\n\ntype Content = string | HTMLElement | ((view: EditorView) => HTMLElement);\nconst contentFacet = Facet.define({\n combine: FacetCombineStrategy.Last<Content>,\n});\n\nconst activeLinePlaceholderView = ViewPlugin.fromClass(\n class {\n public decorations = Decoration.none;\n\n constructor(view: EditorView) {\n if (view.hasFocus) {\n this.decorations = getPlaceholderDecoration(view.state);\n } else {\n this.decorations = Decoration.none;\n }\n }\n\n update(update: ViewUpdate) {\n if (!update.view.hasFocus) {\n this.decorations = Decoration.none;\n return;\n }\n\n // docChanged case: focusing at first empty line, and hit cmd + x(non-empty second line comes in), widget should disappear\n if (update.selectionSet || update.docChanged) {\n this.decorations = getPlaceholderDecoration(update.state);\n }\n }\n },\n {\n decorations(plugin) {\n return plugin.decorations;\n },\n },\n);\n\nfunction getPlaceholderDecoration(state: EditorState) {\n const pos = state.selection.main.head;\n\n if (state.selection.main.empty) {\n const line = state.doc.lineAt(pos);\n\n if (line && line.length === 0) {\n const content = state.facet(contentFacet);\n return Decoration.set([\n Decoration.widget({\n widget: new Placeholder(content),\n side: 1,\n }).range(line.from),\n ]);\n }\n }\n\n return Decoration.none;\n}\n\nconst activeLinePlaceholder = function (content: Content): Ext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AM,MAAMJ,aAAY;AACxC,aAAOI,YAAW,IAAI;AAAA,QACpBA,YAAW,OAAO;AAAA,UAChB,QAAQ,IAAIL,aAAY,OAAO;AAAA,UAC/B,MAAM;AAAA,QACR,CAAC,EAAE,MAAM,KAAK,IAAI;AAAA,MACpB,CAAC;AAAA,IACH;AAAA,EACF;AAEA,SAAOK,YAAW;AACpB;AAEA,IAAM,wBAAwB,SAAU,SAA6B;AACnE,SAAO,CAAC,2BAA2BJ,cAAa,GAAG,OAAO,CAAC;AAC7D;","names":["FacetCombineStrategy","Decoration","ViewPlugin","Facet","Placeholder","contentFacet","Facet","FacetCombineStrategy","ViewPlugin","Decoration"]}
@@ -0,0 +1,10 @@
1
+ import { EditorView } from '@codemirror/view';
2
+ import { Extension } from '@codemirror/state';
3
+
4
+ type Content$1 = string | HTMLElement | ((view: EditorView) => HTMLElement);
5
+ declare function placeholder(content: Content$1): Extension;
6
+
7
+ type Content = string | HTMLElement | ((view: EditorView) => HTMLElement);
8
+ declare const activeLinePlaceholder: (content: Content) => Extension;
9
+
10
+ export { activeLinePlaceholder, placeholder };
@@ -0,0 +1,10 @@
1
+ import { EditorView } from '@codemirror/view';
2
+ import { Extension } from '@codemirror/state';
3
+
4
+ type Content$1 = string | HTMLElement | ((view: EditorView) => HTMLElement);
5
+ declare function placeholder(content: Content$1): Extension;
6
+
7
+ type Content = string | HTMLElement | ((view: EditorView) => HTMLElement);
8
+ declare const activeLinePlaceholder: (content: Content) => Extension;
9
+
10
+ export { activeLinePlaceholder, placeholder };
package/dist/index.js ADDED
@@ -0,0 +1,198 @@
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
+ activeLinePlaceholder: () => activeLinePlaceholder,
23
+ placeholder: () => placeholder
24
+ });
25
+ module.exports = __toCommonJS(index_exports);
26
+
27
+ // src/placeholder.ts
28
+ var import_utils = require("@coze-editor/utils");
29
+ var import_view = require("@codemirror/view");
30
+ var import_state = require("@codemirror/state");
31
+
32
+ // src/dom.ts
33
+ var scratchRange;
34
+ function textRange(node, from, to = from) {
35
+ const range = scratchRange || (scratchRange = document.createRange());
36
+ range.setEnd(node, to);
37
+ range.setStart(node, from);
38
+ return range;
39
+ }
40
+ function flattenRect(rect, left) {
41
+ const x = left ? rect.left : rect.right;
42
+ return { left: x, right: x, top: rect.top, bottom: rect.bottom };
43
+ }
44
+ function clientRectsFor(dom) {
45
+ if (dom.nodeType == 3) {
46
+ return textRange(dom, 0, dom.nodeValue.length).getClientRects();
47
+ } else if (dom.nodeType == 1) {
48
+ return dom.getClientRects();
49
+ } else {
50
+ return [];
51
+ }
52
+ }
53
+
54
+ // src/placeholder.ts
55
+ function createPlaceholderWidgetClass() {
56
+ return class Placeholder extends import_view.WidgetType {
57
+ constructor(content) {
58
+ super();
59
+ this.content = content;
60
+ }
61
+ toDOM(view) {
62
+ const wrap = document.createElement("span");
63
+ wrap.className = "cm-placeholder";
64
+ wrap.style.cssText = `
65
+ height: 0;
66
+ `;
67
+ wrap.appendChild(
68
+ typeof this.content === "string" ? document.createTextNode(this.content) : typeof this.content === "function" ? this.content(view) : this.content
69
+ );
70
+ if (typeof this.content === "string") {
71
+ wrap.setAttribute("aria-label", `placeholder ${this.content}`);
72
+ } else {
73
+ wrap.setAttribute("aria-hidden", "true");
74
+ }
75
+ return wrap;
76
+ }
77
+ coordsAt(dom) {
78
+ const rects = dom.firstChild ? clientRectsFor(dom.firstChild) : [];
79
+ if (!rects.length) {
80
+ return null;
81
+ }
82
+ const style = window.getComputedStyle(dom.parentNode);
83
+ const rect = flattenRect(rects[0], style.direction != "rtl");
84
+ const lineHeight = parseInt(style.lineHeight);
85
+ if (rect.bottom - rect.top > lineHeight * 1.5) {
86
+ return {
87
+ left: rect.left,
88
+ right: rect.right,
89
+ top: rect.top,
90
+ bottom: rect.top + lineHeight
91
+ };
92
+ }
93
+ return rect;
94
+ }
95
+ ignoreEvent() {
96
+ return false;
97
+ }
98
+ };
99
+ }
100
+ var Placeholder = createPlaceholderWidgetClass();
101
+ var contentFacet = import_state.Facet.define({
102
+ combine: import_utils.FacetCombineStrategy.Last
103
+ });
104
+ var placeholderView = import_view.ViewPlugin.fromClass(
105
+ class PlaceholderView {
106
+ constructor(view) {
107
+ this.view = view;
108
+ const content = view.state.facet(contentFacet);
109
+ this.placeholder = this.getPlaceholder(content);
110
+ }
111
+ placeholder;
112
+ content;
113
+ update(update) {
114
+ const content = update.view.state.facet(contentFacet);
115
+ if (this.content !== content) {
116
+ this.placeholder = this.getPlaceholder(content);
117
+ this.content = content;
118
+ }
119
+ }
120
+ getPlaceholder(content) {
121
+ return content ? import_view.Decoration.set([
122
+ import_view.Decoration.widget({
123
+ widget: new Placeholder(content),
124
+ side: 1
125
+ }).range(0)
126
+ ]) : import_view.Decoration.none;
127
+ }
128
+ get decorations() {
129
+ return this.view.state.doc.length ? import_view.Decoration.none : this.placeholder;
130
+ }
131
+ },
132
+ {
133
+ decorations: (v) => v.decorations
134
+ }
135
+ );
136
+ function placeholder(content) {
137
+ return [placeholderView, contentFacet.of(content)];
138
+ }
139
+
140
+ // src/active-line-placeholder.ts
141
+ var import_utils2 = require("@coze-editor/utils");
142
+ var import_view2 = require("@codemirror/view");
143
+ var import_state2 = require("@codemirror/state");
144
+ var Placeholder2 = createPlaceholderWidgetClass();
145
+ var contentFacet2 = import_state2.Facet.define({
146
+ combine: import_utils2.FacetCombineStrategy.Last
147
+ });
148
+ var activeLinePlaceholderView = import_view2.ViewPlugin.fromClass(
149
+ class {
150
+ decorations = import_view2.Decoration.none;
151
+ constructor(view) {
152
+ if (view.hasFocus) {
153
+ this.decorations = getPlaceholderDecoration(view.state);
154
+ } else {
155
+ this.decorations = import_view2.Decoration.none;
156
+ }
157
+ }
158
+ update(update) {
159
+ if (!update.view.hasFocus) {
160
+ this.decorations = import_view2.Decoration.none;
161
+ return;
162
+ }
163
+ if (update.selectionSet || update.docChanged) {
164
+ this.decorations = getPlaceholderDecoration(update.state);
165
+ }
166
+ }
167
+ },
168
+ {
169
+ decorations(plugin) {
170
+ return plugin.decorations;
171
+ }
172
+ }
173
+ );
174
+ function getPlaceholderDecoration(state) {
175
+ const pos = state.selection.main.head;
176
+ if (state.selection.main.empty) {
177
+ const line = state.doc.lineAt(pos);
178
+ if (line && line.length === 0) {
179
+ const content = state.facet(contentFacet2);
180
+ return import_view2.Decoration.set([
181
+ import_view2.Decoration.widget({
182
+ widget: new Placeholder2(content),
183
+ side: 1
184
+ }).range(line.from)
185
+ ]);
186
+ }
187
+ }
188
+ return import_view2.Decoration.none;
189
+ }
190
+ var activeLinePlaceholder = function(content) {
191
+ return [activeLinePlaceholderView, contentFacet2.of(content)];
192
+ };
193
+ // Annotate the CommonJS export names for ESM import in node:
194
+ 0 && (module.exports = {
195
+ activeLinePlaceholder,
196
+ placeholder
197
+ });
198
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/index.ts","../src/placeholder.ts","../src/dom.ts","../src/active-line-placeholder.ts"],"sourcesContent":["// Copyright (c) 2025 coze-dev\n// SPDX-License-Identifier: MIT\n\nexport { placeholder } from './placeholder';\n\nexport { activeLinePlaceholder } from './active-line-placeholder';\n","// Copyright (c) 2025 coze-dev\n// SPDX-License-Identifier: MIT\n\n// modified from @codemirror/view\n// updates:\n// - support handle user events(remove pointerEvents = \"none\", remove cloneNode)\n// - use single view\n// - support multiple content sources(use last one)\n\nimport { FacetCombineStrategy } from '@coze-editor/utils';\nimport {\n Decoration,\n type DecorationSet,\n type EditorView,\n ViewPlugin,\n type ViewUpdate,\n WidgetType,\n} from '@codemirror/view';\nimport { type Extension, Facet } from '@codemirror/state';\n\nimport { clientRectsFor, flattenRect } from './dom';\n\nfunction createPlaceholderWidgetClass() {\n return class Placeholder extends WidgetType {\n constructor(\n readonly content:\n | string\n | HTMLElement\n | ((view: EditorView) => HTMLElement),\n ) {\n super();\n }\n\n toDOM(view: EditorView) {\n const wrap = document.createElement('span');\n wrap.className = 'cm-placeholder';\n // solved the cursor height problem(eg. cursor double height with two lines)\n wrap.style.cssText = `\n height: 0;\n `;\n // wrap.style.pointerEvents = \"none\"\n wrap.appendChild(\n typeof this.content === 'string'\n ? document.createTextNode(this.content)\n : typeof this.content === 'function'\n ? this.content(view)\n : this.content,\n );\n if (typeof this.content === 'string') {\n wrap.setAttribute('aria-label', `placeholder ${this.content}`);\n } else {\n wrap.setAttribute('aria-hidden', 'true');\n }\n return wrap;\n }\n\n coordsAt(dom: HTMLElement) {\n const rects = dom.firstChild ? clientRectsFor(dom.firstChild) : [];\n if (!rects.length) {\n return null;\n }\n const style = window.getComputedStyle(dom.parentNode as HTMLElement);\n const rect = flattenRect(rects[0], style.direction != 'rtl');\n const lineHeight = parseInt(style.lineHeight);\n if (rect.bottom - rect.top > lineHeight * 1.5) {\n return {\n left: rect.left,\n right: rect.right,\n top: rect.top,\n bottom: rect.top + lineHeight,\n };\n }\n return rect;\n }\n\n ignoreEvent() {\n return false;\n }\n };\n}\n\nconst Placeholder = createPlaceholderWidgetClass();\n\ntype Content = string | HTMLElement | ((view: EditorView) => HTMLElement);\nconst contentFacet = Facet.define({\n combine: FacetCombineStrategy.Last<Content>,\n});\n\nconst placeholderView = ViewPlugin.fromClass(\n class PlaceholderView {\n private placeholder: DecorationSet;\n private content: Content | undefined;\n\n constructor(private readonly view: EditorView) {\n const content = view.state.facet(contentFacet);\n this.placeholder = this.getPlaceholder(content);\n }\n\n update(update: ViewUpdate) {\n const content = update.view.state.facet(contentFacet);\n\n if (this.content !== content) {\n this.placeholder = this.getPlaceholder(content);\n this.content = content;\n }\n }\n\n getPlaceholder(content: Content) {\n return content\n ? Decoration.set([\n Decoration.widget({\n widget: new Placeholder(content),\n side: 1,\n }).range(0),\n ])\n : Decoration.none;\n }\n\n get decorations() {\n return this.view.state.doc.length ? Decoration.none : this.placeholder;\n }\n },\n {\n decorations: v => v.decorations,\n },\n);\n\nfunction placeholder(content: Content): Extension {\n return [placeholderView, contentFacet.of(content)];\n}\n\nexport { placeholder, createPlaceholderWidgetClass };\n","// Copyright (c) 2025 coze-dev\n// SPDX-License-Identifier: MIT\n\n/// Basic rectangle type.\ninterface Rect {\n readonly left: number;\n readonly right: number;\n readonly top: number;\n readonly bottom: number;\n}\n\nlet scratchRange: Range | null;\n\nfunction textRange(node: Text, from: number, to = from) {\n const range = scratchRange || (scratchRange = document.createRange());\n range.setEnd(node, to);\n range.setStart(node, from);\n return range;\n}\n\nfunction flattenRect(rect: Rect, left: boolean) {\n const x = left ? rect.left : rect.right;\n return { left: x, right: x, top: rect.top, bottom: rect.bottom };\n}\n\nfunction clientRectsFor(dom: Node) {\n if (dom.nodeType == 3) {\n return textRange(dom as Text, 0, dom.nodeValue!.length).getClientRects();\n } else if (dom.nodeType == 1) {\n return (dom as HTMLElement).getClientRects();\n } else {\n return [] as any as DOMRectList;\n }\n}\n\nexport { flattenRect, clientRectsFor };\n\nexport type { Rect };\n","// Copyright (c) 2025 coze-dev\n// SPDX-License-Identifier: MIT\n\nimport { FacetCombineStrategy } from '@coze-editor/utils';\nimport {\n Decoration,\n type EditorView,\n ViewPlugin,\n type ViewUpdate,\n} from '@codemirror/view';\nimport { type EditorState, type Extension, Facet } from '@codemirror/state';\n\nimport { createPlaceholderWidgetClass } from './placeholder';\n\n// isolate Widget class with Placeholder to avoid widget reusable compare when using Placeholder and ActiveLinePlaceholder together\nconst Placeholder = createPlaceholderWidgetClass();\n\ntype Content = string | HTMLElement | ((view: EditorView) => HTMLElement);\nconst contentFacet = Facet.define({\n combine: FacetCombineStrategy.Last<Content>,\n});\n\nconst activeLinePlaceholderView = ViewPlugin.fromClass(\n class {\n public decorations = Decoration.none;\n\n constructor(view: EditorView) {\n if (view.hasFocus) {\n this.decorations = getPlaceholderDecoration(view.state);\n } else {\n this.decorations = Decoration.none;\n }\n }\n\n update(update: ViewUpdate) {\n if (!update.view.hasFocus) {\n this.decorations = Decoration.none;\n return;\n }\n\n // docChanged case: focusing at first empty line, and hit cmd + x(non-empty second line comes in), widget should disappear\n if (update.selectionSet || update.docChanged) {\n this.decorations = getPlaceholderDecoration(update.state);\n }\n }\n },\n {\n decorations(plugin) {\n return plugin.decorations;\n },\n },\n);\n\nfunction getPlaceholderDecoration(state: EditorState) {\n const pos = state.selection.main.head;\n\n if (state.selection.main.empty) {\n const line = state.doc.lineAt(pos);\n\n if (line && line.length === 0) {\n const content = state.facet(contentFacet);\n return Decoration.set([\n Decoration.widget({\n widget: new Placeholder(content),\n side: 1,\n }).range(line.from),\n ]);\n }\n }\n\n return Decoration.none;\n}\n\nconst activeLinePlaceholder = function (content: Content): Extension {\n return [activeLinePlaceholderView, contentFacet.of(content)];\n};\n\nexport { activeLinePlaceholder };\n"],"mappings":";;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACSA,mBAAqC;AACrC,kBAOO;AACP,mBAAsC;;;ACPtC,IAAI;AAEJ,SAAS,UAAU,MAAY,MAAc,KAAK,MAAM;AACtD,QAAM,QAAQ,iBAAiB,eAAe,SAAS,YAAY;AACnE,QAAM,OAAO,MAAM,EAAE;AACrB,QAAM,SAAS,MAAM,IAAI;AACzB,SAAO;AACT;AAEA,SAAS,YAAY,MAAY,MAAe;AAC9C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Ac,6BAA6B;AAGjD,IAAMC,gBAAe,oBAAM,OAAO;AAAA,EAChC,SAAS,mCAAqB;AAChC,CAAC;AAED,IAAM,4BAA4B,wBAAW;AAAA,EAC3C,MAAM;AAAA,IACG,cAAc,wBAAW;AAAA,IAEhC,YAAY,MAAkB;AAC5B,UAAI,KAAK,UAAU;AACjB,aAAK,cAAc,yBAAyB,KAAK,KAAK;AAAA,MACxD,OAAO;AACL,aAAK,cAAc,wBAAW;AAAA,MAChC;AAAA,IACF;AAAA,IAEA,OAAO,QAAoB;AACzB,UAAI,CAAC,OAAO,KAAK,UAAU;AACzB,aAAK,cAAc,wBAAW;AAC9B;AAAA,MACF;AAGA,UAAI,OAAO,gBAAgB,OAAO,YAAY;AAC5C,aAAK,cAAc,yBAAyB,OAAO,KAAK;AAAA,MAC1D;AAAA,IACF;AAAA,EACF;AAAA,EACA;AAAA,IACE,YAAY,QAAQ;AAClB,aAAO,OAAO;AAAA,IAChB;AAAA,EACF;AACF;AAEA,SAAS,yBAAyB,OAAoB;AACpD,QAAM,MAAM,MAAM,UAAU,KAAK;AAEjC,MAAI,MAAM,UAAU,KAAK,OAAO;AAC9B,UAAM,OAAO,MAAM,IAAI,OAAO,GAAG;AAEjC,QAAI,QAAQ,KAAK,WAAW,GAAG;AAC7B,YAAM,UAAU,MAAM,MAAMA,aAAY;AACxC,aAAO,wBAAW,IAAI;AAAA,QACpB,wBAAW,OAAO;AAAA,UAChB,QAAQ,IAAID,aAAY,OAAO;AAAA,UAC/B,MAAM;AAAA,QACR,CAAC,EAAE,MAAM,KAAK,IAAI;AAAA,MACpB,CAAC;AAAA,IACH;AAAA,EACF;AAEA,SAAO,wBAAW;AACpB;AAEA,IAAM,wBAAwB,SAAU,SAA6B;AACnE,SAAO,CAAC,2BAA2BC,cAAa,GAAG,OAAO,CAAC;AAC7D;","names":["import_utils","import_view","import_state","Placeholder","contentFacet"]}
package/package.json ADDED
@@ -0,0 +1,46 @@
1
+ {
2
+ "name": "@coze-editor/extension-placeholder",
3
+ "version": "0.1.0-alpha.09ffeb",
4
+ "description": "extension-placeholder",
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
+ "dependencies": {
25
+ "@coze-editor/utils": "0.1.0-alpha.09ffeb"
26
+ },
27
+ "devDependencies": {
28
+ "@codemirror/state": "^6.4.1",
29
+ "@codemirror/view": "^6.26.1",
30
+ "@coze-arch/ts-config": "workspace:*",
31
+ "@coze-editor/eslint-config": "workspace:*",
32
+ "@types/node": "^22",
33
+ "eslint": "9.14.0",
34
+ "tsup": "^8.0.1",
35
+ "typescript": "^5.8.2"
36
+ },
37
+ "peerDependencies": {
38
+ "@codemirror/state": "^6.4.1",
39
+ "@codemirror/view": "^6.26.1"
40
+ },
41
+ "publishConfig": {
42
+ "access": "public",
43
+ "registry": "https://registry.npmjs.org"
44
+ },
45
+ "test:main": "./src/index.ts"
46
+ }