@coze-editor/preset-chat 0.1.0-alpha.02803c
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 +21 -0
- package/dist/esm/index.js +607 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/index.d.mts +65 -0
- package/dist/index.d.ts +65 -0
- package/dist/index.js +631 -0
- package/dist/index.js.map +1 -0
- package/package.json +61 -0
package/dist/index.js
ADDED
|
@@ -0,0 +1,631 @@
|
|
|
1
|
+
var __create = Object.create;
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __export = (target, all) => {
|
|
8
|
+
for (var name in all)
|
|
9
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
10
|
+
};
|
|
11
|
+
var __copyProps = (to, from, except, desc) => {
|
|
12
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
13
|
+
for (let key of __getOwnPropNames(from))
|
|
14
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
15
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
16
|
+
}
|
|
17
|
+
return to;
|
|
18
|
+
};
|
|
19
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
20
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
21
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
22
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
23
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
24
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
25
|
+
mod
|
|
26
|
+
));
|
|
27
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
28
|
+
|
|
29
|
+
// src/index.ts
|
|
30
|
+
var index_exports = {};
|
|
31
|
+
__export(index_exports, {
|
|
32
|
+
CUSTOM_CLIPBOARD_MIMETYPE: () => CUSTOM_CLIPBOARD_MIMETYPE,
|
|
33
|
+
default: () => index_default,
|
|
34
|
+
schemaUtils: () => schemaUtils,
|
|
35
|
+
useCurrentElement: () => useCurrentElement
|
|
36
|
+
});
|
|
37
|
+
module.exports = __toCommonJS(index_exports);
|
|
38
|
+
var import_preset_universal = __toESM(require("@coze-editor/preset-universal"));
|
|
39
|
+
var import_core2 = require("@coze-editor/core");
|
|
40
|
+
var import_view4 = require("@codemirror/view");
|
|
41
|
+
var import_commands = require("@codemirror/commands");
|
|
42
|
+
|
|
43
|
+
// src/utils.ts
|
|
44
|
+
function extractElementData(node, text) {
|
|
45
|
+
const openTag = node.firstChild;
|
|
46
|
+
let tagName = null;
|
|
47
|
+
const attributes = {};
|
|
48
|
+
if (openTag && openTag.name === "OpenTag" && openTag.firstChild) {
|
|
49
|
+
let sibling = openTag.firstChild.nextSibling;
|
|
50
|
+
while (true) {
|
|
51
|
+
if (!sibling) {
|
|
52
|
+
break;
|
|
53
|
+
}
|
|
54
|
+
if (sibling.name === "TagName") {
|
|
55
|
+
const { from, to } = sibling;
|
|
56
|
+
tagName = text.slice(from, to);
|
|
57
|
+
} else if (sibling.name === "Attribute") {
|
|
58
|
+
const nameNode = sibling.firstChild;
|
|
59
|
+
const isNode = nameNode == null ? void 0 : nameNode.nextSibling;
|
|
60
|
+
const valueNode = isNode == null ? void 0 : isNode.nextSibling;
|
|
61
|
+
if ((nameNode == null ? void 0 : nameNode.name) === "AttributeName" && (isNode == null ? void 0 : isNode.name) === "Is" && ((valueNode == null ? void 0 : valueNode.name) === "AttributeValue" || (valueNode == null ? void 0 : valueNode.name) === "UnquotedAttributeValue")) {
|
|
62
|
+
const name = text.slice(nameNode.from, nameNode.to);
|
|
63
|
+
const fullValue = text.slice(valueNode.from, valueNode.to);
|
|
64
|
+
let value = fullValue;
|
|
65
|
+
if (valueNode.name === "AttributeValue") {
|
|
66
|
+
value = fullValue.slice(1, -1);
|
|
67
|
+
}
|
|
68
|
+
try {
|
|
69
|
+
attributes[name] = JSON.parse(decodeURIComponent(value));
|
|
70
|
+
} catch (e) {
|
|
71
|
+
}
|
|
72
|
+
} else if ((nameNode == null ? void 0 : nameNode.name) === "AttributeName" && !isNode && !valueNode) {
|
|
73
|
+
const name = text.slice(nameNode.from, nameNode.to);
|
|
74
|
+
attributes[name] = true;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
sibling = sibling.nextSibling;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
if (tagName) {
|
|
81
|
+
return {
|
|
82
|
+
tagName,
|
|
83
|
+
attributes
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
// src/language.ts
|
|
89
|
+
var import_html = require("@lezer/html");
|
|
90
|
+
var import_language = require("@codemirror/language");
|
|
91
|
+
var htmlParser = import_html.parser.configure({
|
|
92
|
+
dialect: "noMatch"
|
|
93
|
+
});
|
|
94
|
+
function parse(text) {
|
|
95
|
+
return htmlParser.parse(text);
|
|
96
|
+
}
|
|
97
|
+
var htmlLanguage = import_language.LRLanguage.define({
|
|
98
|
+
parser: htmlParser
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
// src/schema.ts
|
|
102
|
+
function isElementRegistered(tagName, validTagNames) {
|
|
103
|
+
if (!Array.isArray(validTagNames)) {
|
|
104
|
+
return false;
|
|
105
|
+
}
|
|
106
|
+
if (validTagNames.includes(tagName)) {
|
|
107
|
+
return true;
|
|
108
|
+
}
|
|
109
|
+
return false;
|
|
110
|
+
}
|
|
111
|
+
var schemaUtils = {
|
|
112
|
+
toJSON(text, options) {
|
|
113
|
+
if (text === "") {
|
|
114
|
+
return [];
|
|
115
|
+
}
|
|
116
|
+
const tree = parse(text);
|
|
117
|
+
const tags = [];
|
|
118
|
+
tree.iterate({
|
|
119
|
+
enter(node) {
|
|
120
|
+
if (node.name === "Element") {
|
|
121
|
+
const data = extractElementData(node.node, text);
|
|
122
|
+
if (data && isElementRegistered(data.tagName, options == null ? void 0 : options.validTagNames)) {
|
|
123
|
+
tags.push({
|
|
124
|
+
from: node.from,
|
|
125
|
+
to: node.to,
|
|
126
|
+
raw: text.slice(node.from, node.to),
|
|
127
|
+
...data
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
});
|
|
133
|
+
let pos = 0;
|
|
134
|
+
const elements = [];
|
|
135
|
+
for (const tag of tags) {
|
|
136
|
+
const { from, to, tagName, attributes, raw } = tag;
|
|
137
|
+
const { cmid, ...restAttributes } = attributes ?? {};
|
|
138
|
+
if (from < pos) {
|
|
139
|
+
continue;
|
|
140
|
+
}
|
|
141
|
+
if (from > pos) {
|
|
142
|
+
elements.push({
|
|
143
|
+
type: "text",
|
|
144
|
+
value: text.slice(pos, from)
|
|
145
|
+
});
|
|
146
|
+
}
|
|
147
|
+
elements.push({
|
|
148
|
+
type: "element",
|
|
149
|
+
tagName,
|
|
150
|
+
attributes: restAttributes,
|
|
151
|
+
raw
|
|
152
|
+
});
|
|
153
|
+
pos = to;
|
|
154
|
+
}
|
|
155
|
+
if (pos < text.length) {
|
|
156
|
+
elements.push({
|
|
157
|
+
type: "text",
|
|
158
|
+
value: text.slice(pos)
|
|
159
|
+
});
|
|
160
|
+
}
|
|
161
|
+
return elements;
|
|
162
|
+
},
|
|
163
|
+
fromJSON(elements) {
|
|
164
|
+
return elements.map((el) => {
|
|
165
|
+
if (el.type === "element") {
|
|
166
|
+
return toElementString(el);
|
|
167
|
+
} else if (el.type === "text") {
|
|
168
|
+
return el.value;
|
|
169
|
+
}
|
|
170
|
+
return "";
|
|
171
|
+
}).join("");
|
|
172
|
+
}
|
|
173
|
+
};
|
|
174
|
+
function toElementString(element) {
|
|
175
|
+
const attrsString = attributesToString(element.attributes);
|
|
176
|
+
return `<${element.tagName}${attrsString ? ` ${attrsString}` : ""}></${element.tagName}>`;
|
|
177
|
+
}
|
|
178
|
+
var INTERNAL_ID = "cmid";
|
|
179
|
+
function uniqueId() {
|
|
180
|
+
return `e${Math.random()}`;
|
|
181
|
+
}
|
|
182
|
+
function attributesToString(attributes) {
|
|
183
|
+
const array = [];
|
|
184
|
+
let hasId = false;
|
|
185
|
+
Object.keys(attributes).forEach((key) => {
|
|
186
|
+
if (key === INTERNAL_ID) {
|
|
187
|
+
hasId = true;
|
|
188
|
+
}
|
|
189
|
+
const value = attributes[key];
|
|
190
|
+
if (value === true) {
|
|
191
|
+
array.push(key);
|
|
192
|
+
} else if (typeof value !== "undefined") {
|
|
193
|
+
array.push(`${key}="${encodeURIComponent(JSON.stringify(value))}"`);
|
|
194
|
+
}
|
|
195
|
+
});
|
|
196
|
+
if (!hasId) {
|
|
197
|
+
array.unshift(
|
|
198
|
+
`${INTERNAL_ID}="${encodeURIComponent(JSON.stringify(uniqueId()))}"`
|
|
199
|
+
);
|
|
200
|
+
}
|
|
201
|
+
return array.join(" ");
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
// src/plugins.ts
|
|
205
|
+
var import_core = require("@coze-editor/core");
|
|
206
|
+
var import_view2 = require("@codemirror/view");
|
|
207
|
+
var import_state2 = require("@codemirror/state");
|
|
208
|
+
|
|
209
|
+
// src/extension.ts
|
|
210
|
+
var import_react_dom = require("react-dom");
|
|
211
|
+
var import_react2 = require("react");
|
|
212
|
+
var import_client = require("react-dom/client");
|
|
213
|
+
var import_utils2 = require("@coze-editor/utils");
|
|
214
|
+
var import_react3 = require("@coze-editor/react");
|
|
215
|
+
var import_view = require("@codemirror/view");
|
|
216
|
+
var import_state = require("@codemirror/state");
|
|
217
|
+
var import_language3 = require("@codemirror/language");
|
|
218
|
+
|
|
219
|
+
// src/context.tsx
|
|
220
|
+
var import_react = __toESM(require("react"));
|
|
221
|
+
var Context = (0, import_react.createContext)("");
|
|
222
|
+
function ElementProvider({
|
|
223
|
+
internalId,
|
|
224
|
+
children
|
|
225
|
+
}) {
|
|
226
|
+
return /* @__PURE__ */ import_react.default.createElement(Context.Provider, { value: internalId }, children);
|
|
227
|
+
}
|
|
228
|
+
function useElementId() {
|
|
229
|
+
const id = (0, import_react.useContext)(Context);
|
|
230
|
+
return id;
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
// src/extension.ts
|
|
234
|
+
var ElementWidget = class extends import_view.WidgetType {
|
|
235
|
+
constructor(definition, id, props) {
|
|
236
|
+
super();
|
|
237
|
+
this.definition = definition;
|
|
238
|
+
this.id = id;
|
|
239
|
+
this.props = props;
|
|
240
|
+
const element = document.createElement("span");
|
|
241
|
+
this.element = element;
|
|
242
|
+
this.root = (0, import_client.createRoot)(element);
|
|
243
|
+
}
|
|
244
|
+
$$type = "element";
|
|
245
|
+
root;
|
|
246
|
+
element;
|
|
247
|
+
view = null;
|
|
248
|
+
get elementId() {
|
|
249
|
+
return `element-${this.id}`;
|
|
250
|
+
}
|
|
251
|
+
toDOM(view) {
|
|
252
|
+
this.view = view;
|
|
253
|
+
const c = view.state.facet(import_react3.connector);
|
|
254
|
+
queueMicrotask(() => {
|
|
255
|
+
(0, import_react_dom.flushSync)(() => {
|
|
256
|
+
const jsxElement = (0, import_react2.createElement)(ElementProvider, {
|
|
257
|
+
internalId: this.id,
|
|
258
|
+
children: (0, import_react2.createElement)(this.definition.render, this.props)
|
|
259
|
+
});
|
|
260
|
+
c.connect(this.elementId, (0, import_react_dom.createPortal)(jsxElement, this.element));
|
|
261
|
+
});
|
|
262
|
+
});
|
|
263
|
+
return this.element;
|
|
264
|
+
}
|
|
265
|
+
eq(other) {
|
|
266
|
+
return this.id === other.id && this.props && other.props && JSON.stringify(this.props) === JSON.stringify(other.props);
|
|
267
|
+
}
|
|
268
|
+
destroy() {
|
|
269
|
+
if (this.view) {
|
|
270
|
+
const c = this.view.state.facet(import_react3.connector);
|
|
271
|
+
c.disconnect(this.elementId);
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
ignoreEvent(event) {
|
|
275
|
+
return false;
|
|
276
|
+
}
|
|
277
|
+
};
|
|
278
|
+
var elementsFacet = import_state.Facet.define({
|
|
279
|
+
combine: import_utils2.FacetCombineStrategy.Last
|
|
280
|
+
});
|
|
281
|
+
var field = import_state.StateField.define({
|
|
282
|
+
create(state) {
|
|
283
|
+
return build(state);
|
|
284
|
+
},
|
|
285
|
+
update(value, tr) {
|
|
286
|
+
if (tr.docChanged) {
|
|
287
|
+
return build(tr.state);
|
|
288
|
+
}
|
|
289
|
+
return value;
|
|
290
|
+
},
|
|
291
|
+
provide(f) {
|
|
292
|
+
return [
|
|
293
|
+
import_view.EditorView.decorations.of((view) => view.state.field(f)),
|
|
294
|
+
import_view.EditorView.atomicRanges.of((view) => view.state.field(f))
|
|
295
|
+
];
|
|
296
|
+
}
|
|
297
|
+
});
|
|
298
|
+
function build(state) {
|
|
299
|
+
const allElements = state.facet(elementsFacet);
|
|
300
|
+
if (!allElements) {
|
|
301
|
+
return import_view.Decoration.none;
|
|
302
|
+
}
|
|
303
|
+
const tree = (0, import_language3.ensureSyntaxTree)(state, state.doc.length) ?? (0, import_language3.syntaxTree)(state);
|
|
304
|
+
const builder = new import_state.RangeSetBuilder();
|
|
305
|
+
tree.iterate({
|
|
306
|
+
enter(node) {
|
|
307
|
+
const data = extract(node.node, state);
|
|
308
|
+
if (data) {
|
|
309
|
+
const definition = allElements[data.tagName];
|
|
310
|
+
if (definition) {
|
|
311
|
+
builder.add(
|
|
312
|
+
node.from,
|
|
313
|
+
node.to,
|
|
314
|
+
import_view.Decoration.replace({
|
|
315
|
+
widget: new ElementWidget(
|
|
316
|
+
definition,
|
|
317
|
+
data.internalId,
|
|
318
|
+
data.props
|
|
319
|
+
)
|
|
320
|
+
})
|
|
321
|
+
);
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
return true;
|
|
325
|
+
}
|
|
326
|
+
});
|
|
327
|
+
const decorations = builder.finish();
|
|
328
|
+
return decorations;
|
|
329
|
+
}
|
|
330
|
+
function extract(node, state) {
|
|
331
|
+
if (node.name === "Element") {
|
|
332
|
+
const elementData = extractElementData(node.node, state.doc.toString());
|
|
333
|
+
if (elementData) {
|
|
334
|
+
const { tagName, attributes } = elementData;
|
|
335
|
+
const { [INTERNAL_ID]: internalId, ...props } = attributes ?? {};
|
|
336
|
+
return {
|
|
337
|
+
tagName,
|
|
338
|
+
internalId,
|
|
339
|
+
props
|
|
340
|
+
};
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
var CUSTOM_CLIPBOARD_MIMETYPE = "application/x-with-elements";
|
|
345
|
+
var copyPasteHandler = import_view.EditorView.domEventHandlers({
|
|
346
|
+
paste(event, view) {
|
|
347
|
+
var _a;
|
|
348
|
+
try {
|
|
349
|
+
let text = (_a = event.clipboardData) == null ? void 0 : _a.getData(CUSTOM_CLIPBOARD_MIMETYPE);
|
|
350
|
+
if (!text) {
|
|
351
|
+
return false;
|
|
352
|
+
}
|
|
353
|
+
text = text.replace(/\r\n/g, "\n");
|
|
354
|
+
const definitions = view.state.facet(elementsFacet);
|
|
355
|
+
const nodes = schemaUtils.toJSON(text, {
|
|
356
|
+
validTagNames: definitions ? Object.keys(definitions) : void 0
|
|
357
|
+
});
|
|
358
|
+
const newText = schemaUtils.fromJSON(
|
|
359
|
+
nodes.map((node) => {
|
|
360
|
+
if (node.type === "text") {
|
|
361
|
+
return node;
|
|
362
|
+
}
|
|
363
|
+
if (node.type === "element") {
|
|
364
|
+
return {
|
|
365
|
+
...node,
|
|
366
|
+
attributes: {
|
|
367
|
+
...node.attributes ?? {},
|
|
368
|
+
cmid: `e${Math.random()}`
|
|
369
|
+
}
|
|
370
|
+
};
|
|
371
|
+
}
|
|
372
|
+
}).filter((v) => isEditorNode(v))
|
|
373
|
+
);
|
|
374
|
+
view.dispatch({
|
|
375
|
+
changes: {
|
|
376
|
+
from: view.state.selection.main.from,
|
|
377
|
+
to: view.state.selection.main.to,
|
|
378
|
+
insert: newText
|
|
379
|
+
},
|
|
380
|
+
selection: import_state.EditorSelection.cursor(
|
|
381
|
+
view.state.selection.main.from + newText.length
|
|
382
|
+
)
|
|
383
|
+
});
|
|
384
|
+
return true;
|
|
385
|
+
} catch (e) {
|
|
386
|
+
return false;
|
|
387
|
+
}
|
|
388
|
+
},
|
|
389
|
+
copy(event, view) {
|
|
390
|
+
var _a, _b;
|
|
391
|
+
const definitions = view.state.facet(elementsFacet);
|
|
392
|
+
if (!definitions) {
|
|
393
|
+
return false;
|
|
394
|
+
}
|
|
395
|
+
try {
|
|
396
|
+
const { from, to } = view.state.selection.main;
|
|
397
|
+
const slice = view.state.doc.sliceString(from, to);
|
|
398
|
+
const nodes = schemaUtils.toJSON(slice, {
|
|
399
|
+
validTagNames: definitions ? Object.keys(definitions) : void 0
|
|
400
|
+
});
|
|
401
|
+
const plainText = nodes.map((node) => {
|
|
402
|
+
if (node.type === "text") {
|
|
403
|
+
return node.value;
|
|
404
|
+
}
|
|
405
|
+
if (node.type === "element") {
|
|
406
|
+
const definition = definitions[node.tagName];
|
|
407
|
+
const toString = definition == null ? void 0 : definition.toString;
|
|
408
|
+
if (!definition) {
|
|
409
|
+
return node.raw ?? "";
|
|
410
|
+
}
|
|
411
|
+
if (Object.prototype.hasOwnProperty.call(definition, "toString") && typeof toString === "function") {
|
|
412
|
+
return toString(node);
|
|
413
|
+
}
|
|
414
|
+
return `[${node.tagName}]`;
|
|
415
|
+
}
|
|
416
|
+
return "";
|
|
417
|
+
}).join("");
|
|
418
|
+
(_a = event.clipboardData) == null ? void 0 : _a.setData("text/plain", plainText);
|
|
419
|
+
(_b = event.clipboardData) == null ? void 0 : _b.setData(CUSTOM_CLIPBOARD_MIMETYPE, slice);
|
|
420
|
+
return true;
|
|
421
|
+
} catch (e) {
|
|
422
|
+
return false;
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
});
|
|
426
|
+
function isEditorNode(v) {
|
|
427
|
+
return Boolean(v);
|
|
428
|
+
}
|
|
429
|
+
function chatExtension() {
|
|
430
|
+
return [
|
|
431
|
+
field,
|
|
432
|
+
copyPasteHandler
|
|
433
|
+
// selectionEnlarger.of(state => {
|
|
434
|
+
// const decorations = state.field(field);
|
|
435
|
+
// const cursor = decorations.iter();
|
|
436
|
+
// const array = [];
|
|
437
|
+
// while (cursor.value) {
|
|
438
|
+
// const widget = cursor.value.spec?.widget;
|
|
439
|
+
// const { from, to } = cursor;
|
|
440
|
+
// if (isElementWidget(widget)) {
|
|
441
|
+
// array.push({
|
|
442
|
+
// source: { from, to },
|
|
443
|
+
// target: { from, to },
|
|
444
|
+
// });
|
|
445
|
+
// }
|
|
446
|
+
// cursor.next();
|
|
447
|
+
// }
|
|
448
|
+
// return array;
|
|
449
|
+
// }),
|
|
450
|
+
];
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
// src/plugins.ts
|
|
454
|
+
var focusedEffect = import_state2.StateEffect.define();
|
|
455
|
+
var focusedField = import_state2.StateField.define({
|
|
456
|
+
create() {
|
|
457
|
+
return false;
|
|
458
|
+
},
|
|
459
|
+
update(value, tr) {
|
|
460
|
+
for (const effect of tr.effects) {
|
|
461
|
+
if (effect.is(focusedEffect)) {
|
|
462
|
+
return true;
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
return value;
|
|
466
|
+
}
|
|
467
|
+
});
|
|
468
|
+
function insertElement({ view }) {
|
|
469
|
+
return (element) => {
|
|
470
|
+
let selection = view.state.selection.main;
|
|
471
|
+
const hasFocused = view.state.field(focusedField, false);
|
|
472
|
+
if (hasFocused === false) {
|
|
473
|
+
selection = import_state2.EditorSelection.cursor(view.state.doc.length);
|
|
474
|
+
}
|
|
475
|
+
const insert = toElementString(element);
|
|
476
|
+
view.dispatch({
|
|
477
|
+
changes: {
|
|
478
|
+
from: selection.from,
|
|
479
|
+
to: selection.to,
|
|
480
|
+
insert
|
|
481
|
+
},
|
|
482
|
+
selection: import_state2.EditorSelection.cursor(selection.from + insert.length)
|
|
483
|
+
});
|
|
484
|
+
};
|
|
485
|
+
}
|
|
486
|
+
var plugins = [
|
|
487
|
+
(0, import_core.extension)([
|
|
488
|
+
focusedField,
|
|
489
|
+
import_view2.EditorView.domEventObservers({
|
|
490
|
+
click(e, view) {
|
|
491
|
+
view.dispatch({
|
|
492
|
+
effects: focusedEffect.of(null)
|
|
493
|
+
});
|
|
494
|
+
}
|
|
495
|
+
}),
|
|
496
|
+
chatExtension()
|
|
497
|
+
]),
|
|
498
|
+
(0, import_core.api)("insertElement", insertElement),
|
|
499
|
+
(0, import_core.option)(
|
|
500
|
+
"elements",
|
|
501
|
+
(elements) => elementsFacet.of(elements)
|
|
502
|
+
)
|
|
503
|
+
];
|
|
504
|
+
var plugins_default = plugins;
|
|
505
|
+
|
|
506
|
+
// src/hooks.tsx
|
|
507
|
+
var import_react4 = require("react");
|
|
508
|
+
var import_react5 = require("@coze-editor/react");
|
|
509
|
+
var import_view3 = require("@codemirror/view");
|
|
510
|
+
var import_state3 = require("@codemirror/state");
|
|
511
|
+
function useCurrentElement() {
|
|
512
|
+
const editor = (0, import_react5.useEditor)();
|
|
513
|
+
const elementId = useElementId();
|
|
514
|
+
const injector = (0, import_react5.useInjector)();
|
|
515
|
+
const [isSelected, setIsSelected] = (0, import_react4.useState)(false);
|
|
516
|
+
const elementIdRef = (0, import_react4.useRef)(elementId);
|
|
517
|
+
elementIdRef.current = elementId;
|
|
518
|
+
function select() {
|
|
519
|
+
var _a;
|
|
520
|
+
if (!editor) {
|
|
521
|
+
return;
|
|
522
|
+
}
|
|
523
|
+
const view = editor.$view;
|
|
524
|
+
const decorations = view.state.field(field);
|
|
525
|
+
const cursor = decorations.iter();
|
|
526
|
+
while (cursor.value) {
|
|
527
|
+
const { spec } = cursor.value;
|
|
528
|
+
if (((_a = spec == null ? void 0 : spec.widget) == null ? void 0 : _a.id) === elementId) {
|
|
529
|
+
view.dispatch({
|
|
530
|
+
selection: import_state3.EditorSelection.range(cursor.from, cursor.to)
|
|
531
|
+
});
|
|
532
|
+
break;
|
|
533
|
+
}
|
|
534
|
+
cursor.next();
|
|
535
|
+
}
|
|
536
|
+
}
|
|
537
|
+
function remove() {
|
|
538
|
+
if (!editor) {
|
|
539
|
+
return;
|
|
540
|
+
}
|
|
541
|
+
const view = editor.$view;
|
|
542
|
+
if (view.state.readOnly) {
|
|
543
|
+
return;
|
|
544
|
+
}
|
|
545
|
+
const range = findElement(view.state, elementId);
|
|
546
|
+
if (range) {
|
|
547
|
+
view.dispatch({
|
|
548
|
+
changes: {
|
|
549
|
+
from: range.from,
|
|
550
|
+
to: range.to,
|
|
551
|
+
insert: ""
|
|
552
|
+
},
|
|
553
|
+
selection: import_state3.EditorSelection.cursor(range.from)
|
|
554
|
+
});
|
|
555
|
+
}
|
|
556
|
+
}
|
|
557
|
+
(0, import_react4.useLayoutEffect)(() => {
|
|
558
|
+
function selectionContainsCurrentElement(state) {
|
|
559
|
+
const range = findElement(state, elementId);
|
|
560
|
+
const { ranges } = state.selection;
|
|
561
|
+
if (!range) {
|
|
562
|
+
return false;
|
|
563
|
+
}
|
|
564
|
+
for (const r of ranges) {
|
|
565
|
+
if (r.from <= range.from && r.to >= range.to) {
|
|
566
|
+
return true;
|
|
567
|
+
}
|
|
568
|
+
}
|
|
569
|
+
return false;
|
|
570
|
+
}
|
|
571
|
+
const plugin = import_view3.ViewPlugin.fromClass(
|
|
572
|
+
class {
|
|
573
|
+
constructor(view) {
|
|
574
|
+
const contains = selectionContainsCurrentElement(view.state);
|
|
575
|
+
setIsSelected(contains);
|
|
576
|
+
}
|
|
577
|
+
update(update) {
|
|
578
|
+
if (update.selectionSet) {
|
|
579
|
+
const contains = selectionContainsCurrentElement(update.state);
|
|
580
|
+
setIsSelected(contains);
|
|
581
|
+
}
|
|
582
|
+
}
|
|
583
|
+
}
|
|
584
|
+
);
|
|
585
|
+
return injector.inject([plugin]);
|
|
586
|
+
}, [injector]);
|
|
587
|
+
return {
|
|
588
|
+
isSelected,
|
|
589
|
+
select,
|
|
590
|
+
remove
|
|
591
|
+
};
|
|
592
|
+
}
|
|
593
|
+
function findElement(state, elementId) {
|
|
594
|
+
var _a;
|
|
595
|
+
const decorations = state.field(field);
|
|
596
|
+
const cursor = decorations.iter();
|
|
597
|
+
while (cursor.value) {
|
|
598
|
+
const { spec } = cursor.value;
|
|
599
|
+
if (((_a = spec == null ? void 0 : spec.widget) == null ? void 0 : _a.id) === elementId) {
|
|
600
|
+
return {
|
|
601
|
+
from: cursor.from,
|
|
602
|
+
to: cursor.to
|
|
603
|
+
};
|
|
604
|
+
}
|
|
605
|
+
cursor.next();
|
|
606
|
+
}
|
|
607
|
+
}
|
|
608
|
+
|
|
609
|
+
// src/index.ts
|
|
610
|
+
var preset = [
|
|
611
|
+
...import_preset_universal.default,
|
|
612
|
+
(0, import_core2.extension)([
|
|
613
|
+
import_view4.EditorView.theme({
|
|
614
|
+
"&.cm-focused": {
|
|
615
|
+
outline: "none"
|
|
616
|
+
}
|
|
617
|
+
}),
|
|
618
|
+
htmlLanguage,
|
|
619
|
+
(0, import_commands.history)(),
|
|
620
|
+
import_view4.keymap.of([...import_commands.defaultKeymap, ...import_commands.historyKeymap])
|
|
621
|
+
]),
|
|
622
|
+
...plugins_default
|
|
623
|
+
];
|
|
624
|
+
var index_default = preset;
|
|
625
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
626
|
+
0 && (module.exports = {
|
|
627
|
+
CUSTOM_CLIPBOARD_MIMETYPE,
|
|
628
|
+
schemaUtils,
|
|
629
|
+
useCurrentElement
|
|
630
|
+
});
|
|
631
|
+
//# sourceMappingURL=index.js.map
|