slate-vue3 0.2.12 → 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.
package/dist/index.js CHANGED
@@ -1,1768 +1,7 @@
1
- import { h, defineComponent, provide, ref, computed, onMounted, onUnmounted, renderSlot, inject, watch, renderList, Fragment, useAttrs, reactive, toRaw } from "vue";
2
- import { R as Range, N as Node, S as Scrubber, T as Text, P as Path, E as Editor, a as Element, c as Transforms } from "./batch-dirty-paths-DdGxRyrT.js";
1
+ import { S as SLATE_USE_SELECTED, a as SLATE_STATE_SELECTION } from "./use-focused-C0jEAq2w.js";
2
+ import { E, b, d, c, u, g, e, f } from "./use-focused-C0jEAq2w.js";
3
+ import { inject } from "vue";
3
4
  import { t } from "./batch-dirty-paths-DdGxRyrT.js";
4
- import { p as isDOMNode, I as IS_ANDROID, D as DOMEditor, P as MARK_PLACEHOLDER_SYMBOL, d as EDITOR_TO_ON_CHANGE, x as IS_IOS, E as EDITOR_TO_KEY_TO_ELEMENT, J as ELEMENT_TO_NODE, Q as NODE_TO_ELEMENT, R as NODE_TO_INDEX, S as NODE_TO_PARENT, y as IS_WEBKIT, e as applyStringDiff, q as isDOMSelection, n as normalizeStringDiff, O as IS_READ_ONLY, h as getDefaultView, G as EDITOR_TO_WINDOW, F as EDITOR_TO_ELEMENT, H as HAS_BEFORE_INPUT_SUPPORT, C as CAN_USE_DOM, f as getActiveElement, j as getSelection, M as IS_FOCUSED, r as isPlainTextOnlyPaste, L as IS_COMPOSING, B as Hotkeys, u as IS_CHROME, v as IS_FIREFOX, w as IS_FIREFOX_LEGACY, A as IS_WECHATBROWSER, z as IS_UC_MOBILE, a as EDITOR_TO_USER_MARKS, o as isDOMElement, b as EDITOR_TO_USER_SELECTION } from "./hotkeys-CmyQAN4Q.js";
5
- const SLATE_USE_EDITOR = Symbol("SLATE_USE_EDITOR");
6
- const SLATE_USE_DECORATE = Symbol("SLATE_USE_DECORATE");
7
- const SLATE_USE_SELECTED = Symbol("SLATE_USE_SELECTED");
8
- const SLATE_STATE_FOCUS = Symbol("SLATE_STATE_FOCUS");
9
- const SLATE_STATE_COMPOSING = Symbol("SLATE_STATE_COMPOSING");
10
- const SLATE_STATE_READ_ONLY = Symbol("SLATE_STATE_READ_ONLY");
11
- const SLATE_STATE_SELECTION = Symbol("SLATE_STATE_SELECTION");
12
- const SLATE_INNER_RENDER_ELEMENT = Symbol("SLATE_INNER_RENDER_ELEMENT");
13
- const SLATE_INNER_RENDER_LEAF = Symbol("SLATE_INNER_RENDER_LEAF");
14
- const SLATE_INNER_RENDER_PLACEHOLDER = Symbol("SLATE_INNER_RENDER_PLACEHOLDER");
15
- const SLATE_INNER_CHANGE_EFFECT_INJECT = Symbol("SLATE_INNER_CHANGE_EFFECT_INJECT");
16
- const SLATE_INNER_MARK_PLACEHOLDER = Symbol("SLATE_INNER_MARK_PLACEHOLDER");
17
- const t2 = (t3) => "object" == typeof t3 && null != t3 && 1 === t3.nodeType, e$1 = (t3, e2) => (!e2 || "hidden" !== t3) && ("visible" !== t3 && "clip" !== t3), n = (t3, n2) => {
18
- if (t3.clientHeight < t3.scrollHeight || t3.clientWidth < t3.scrollWidth) {
19
- const o2 = getComputedStyle(t3, null);
20
- return e$1(o2.overflowY, n2) || e$1(o2.overflowX, n2) || ((t4) => {
21
- const e2 = ((t5) => {
22
- if (!t5.ownerDocument || !t5.ownerDocument.defaultView) return null;
23
- try {
24
- return t5.ownerDocument.defaultView.frameElement;
25
- } catch (t6) {
26
- return null;
27
- }
28
- })(t4);
29
- return !!e2 && (e2.clientHeight < t4.scrollHeight || e2.clientWidth < t4.scrollWidth);
30
- })(t3);
31
- }
32
- return false;
33
- }, o$1 = (t3, e2, n2, o2, l2, r2, i, s) => r2 < t3 && i > e2 || r2 > t3 && i < e2 ? 0 : r2 <= t3 && s <= n2 || i >= e2 && s >= n2 ? r2 - t3 - o2 : i > e2 && s < n2 || r2 < t3 && s > n2 ? i - e2 + l2 : 0, l = (t3) => {
34
- const e2 = t3.parentElement;
35
- return null == e2 ? t3.getRootNode().host || null : e2;
36
- }, r = (e2, r2) => {
37
- var i, s, d, h2;
38
- if ("undefined" == typeof document) return [];
39
- const { scrollMode: c, block: f, inline: u, boundary: a, skipOverflowHiddenElements: g } = r2, p = "function" == typeof a ? a : (t3) => t3 !== a;
40
- if (!t2(e2)) throw new TypeError("Invalid target");
41
- const m = document.scrollingElement || document.documentElement, w = [];
42
- let W = e2;
43
- for (; t2(W) && p(W); ) {
44
- if (W = l(W), W === m) {
45
- w.push(W);
46
- break;
47
- }
48
- null != W && W === document.body && n(W) && !n(document.documentElement) || null != W && n(W, g) && w.push(W);
49
- }
50
- const b = null != (s = null == (i = window.visualViewport) ? void 0 : i.width) ? s : innerWidth, H = null != (h2 = null == (d = window.visualViewport) ? void 0 : d.height) ? h2 : innerHeight, { scrollX: y, scrollY: M } = window, { height: v, width: E, top: x, right: C, bottom: I, left: R } = e2.getBoundingClientRect(), { top: T, right: B, bottom: F, left: V } = ((t3) => {
51
- const e3 = window.getComputedStyle(t3);
52
- return { top: parseFloat(e3.scrollMarginTop) || 0, right: parseFloat(e3.scrollMarginRight) || 0, bottom: parseFloat(e3.scrollMarginBottom) || 0, left: parseFloat(e3.scrollMarginLeft) || 0 };
53
- })(e2);
54
- let k = "start" === f || "nearest" === f ? x - T : "end" === f ? I + F : x + v / 2 - T + F, D = "center" === u ? R + E / 2 - V + B : "end" === u ? C + B : R - V;
55
- const L = [];
56
- for (let t3 = 0; t3 < w.length; t3++) {
57
- const e3 = w[t3], { height: n2, width: l2, top: r3, right: i2, bottom: s2, left: d2 } = e3.getBoundingClientRect();
58
- if ("if-needed" === c && x >= 0 && R >= 0 && I <= H && C <= b && x >= r3 && I <= s2 && R >= d2 && C <= i2) return L;
59
- const h3 = getComputedStyle(e3), a2 = parseInt(h3.borderLeftWidth, 10), g2 = parseInt(h3.borderTopWidth, 10), p2 = parseInt(h3.borderRightWidth, 10), W2 = parseInt(h3.borderBottomWidth, 10);
60
- let T2 = 0, B2 = 0;
61
- const F2 = "offsetWidth" in e3 ? e3.offsetWidth - e3.clientWidth - a2 - p2 : 0, V2 = "offsetHeight" in e3 ? e3.offsetHeight - e3.clientHeight - g2 - W2 : 0, S = "offsetWidth" in e3 ? 0 === e3.offsetWidth ? 0 : l2 / e3.offsetWidth : 0, X = "offsetHeight" in e3 ? 0 === e3.offsetHeight ? 0 : n2 / e3.offsetHeight : 0;
62
- if (m === e3) T2 = "start" === f ? k : "end" === f ? k - H : "nearest" === f ? o$1(M, M + H, H, g2, W2, M + k, M + k + v, v) : k - H / 2, B2 = "start" === u ? D : "center" === u ? D - b / 2 : "end" === u ? D - b : o$1(y, y + b, b, a2, p2, y + D, y + D + E, E), T2 = Math.max(0, T2 + M), B2 = Math.max(0, B2 + y);
63
- else {
64
- T2 = "start" === f ? k - r3 - g2 : "end" === f ? k - s2 + W2 + V2 : "nearest" === f ? o$1(r3, s2, n2, g2, W2 + V2, k, k + v, v) : k - (r3 + n2 / 2) + V2 / 2, B2 = "start" === u ? D - d2 - a2 : "center" === u ? D - (d2 + l2 / 2) + F2 / 2 : "end" === u ? D - i2 + p2 + F2 : o$1(d2, i2, l2, a2, p2 + F2, D, D + E, E);
65
- const { scrollLeft: t4, scrollTop: h4 } = e3;
66
- T2 = 0 === X ? 0 : Math.max(0, Math.min(h4 + T2 / X, e3.scrollHeight - n2 / X + V2)), B2 = 0 === S ? 0 : Math.max(0, Math.min(t4 + B2 / S, e3.scrollWidth - l2 / S + F2)), k += h4 - T2, D += t4 - B2;
67
- }
68
- L.push({ el: e3, top: T2, left: B2 });
69
- }
70
- return L;
71
- };
72
- const o = (t3) => false === t3 ? { block: "end", inline: "nearest" } : ((t4) => t4 === Object(t4) && 0 !== Object.keys(t4).length)(t3) ? t3 : { block: "start", inline: "nearest" };
73
- function e(e2, r$1) {
74
- if (!e2.isConnected || !((t3) => {
75
- let o2 = t3;
76
- for (; o2 && o2.parentNode; ) {
77
- if (o2.parentNode === document) return true;
78
- o2 = o2.parentNode instanceof ShadowRoot ? o2.parentNode.host : o2.parentNode;
79
- }
80
- return false;
81
- })(e2)) return;
82
- const n2 = ((t3) => {
83
- const o2 = window.getComputedStyle(t3);
84
- return { top: parseFloat(o2.scrollMarginTop) || 0, right: parseFloat(o2.scrollMarginRight) || 0, bottom: parseFloat(o2.scrollMarginBottom) || 0, left: parseFloat(o2.scrollMarginLeft) || 0 };
85
- })(e2);
86
- if (((t3) => "object" == typeof t3 && "function" == typeof t3.behavior)(r$1)) return r$1.behavior(r(e2, r$1));
87
- const l2 = "boolean" == typeof r$1 || null == r$1 ? void 0 : r$1.behavior;
88
- for (const { el: a, top: i, left: s } of r(e2, o(r$1))) {
89
- const t3 = i - n2.top + n2.bottom, o2 = s - n2.left + n2.right;
90
- a.scroll({ top: t3, left: o2, behavior: l2 });
91
- }
92
- }
93
- const isEventHandled = (event, handler) => {
94
- if (!handler) {
95
- return false;
96
- }
97
- event.stopImmediatePropagation();
98
- const shouldTreatEventAsHandled = handler(event);
99
- if (shouldTreatEventAsHandled != null) {
100
- return shouldTreatEventAsHandled;
101
- }
102
- return event.defaultPrevented || !event.bubbles;
103
- };
104
- const isDOMEventTargetInput = (event) => isDOMNode(event.target) && (event.target instanceof HTMLInputElement || event.target instanceof HTMLTextAreaElement);
105
- const isDOMEventHandled = (event, handler) => {
106
- if (!handler) {
107
- return false;
108
- }
109
- event.stopImmediatePropagation();
110
- const shouldTreatEventAsHandled = handler(event);
111
- if (shouldTreatEventAsHandled != null) {
112
- return shouldTreatEventAsHandled;
113
- }
114
- return event.defaultPrevented;
115
- };
116
- const handleNativeHistoryEvents = (editor, event) => {
117
- if (event.inputType === "historyUndo" && "undo" in editor && typeof editor.undo === "function") {
118
- editor.undo();
119
- }
120
- if (event.inputType === "historyRedo" && "redo" in editor && typeof editor.redo === "function") {
121
- editor.redo();
122
- }
123
- };
124
- const defaultScrollSelectionIntoView = (editor, domRange) => {
125
- if (domRange.getBoundingClientRect && (!editor.selection || editor.selection && Range.isCollapsed(editor.selection))) {
126
- const leafEl = domRange.startContainer.parentElement;
127
- leafEl.getBoundingClientRect = domRange.getBoundingClientRect.bind(domRange);
128
- e(leafEl, {
129
- scrollMode: "if-needed"
130
- });
131
- delete leafEl.getBoundingClientRect;
132
- }
133
- };
134
- const defaultRenderLeaf = ({ attributes, children }) => {
135
- return h(
136
- "span",
137
- attributes,
138
- children
139
- );
140
- };
141
- const defaultRenderPlaceHolder = ({ attributes, children }) => {
142
- return h("span", attributes, [
143
- children,
144
- IS_ANDROID ? h("br") : void 0
145
- ]);
146
- };
147
- const DEFAULT_DECORATE_FN = () => [];
148
- const Slate = defineComponent({
149
- name: "slate-editor",
150
- emits: ["change", "selectionchange", "valuechange"],
151
- props: {
152
- editor: {
153
- type: Object,
154
- require: true
155
- },
156
- decorate: {
157
- type: Function,
158
- default: DEFAULT_DECORATE_FN
159
- },
160
- renderElement: {
161
- type: Function,
162
- required: true
163
- },
164
- renderLeaf: {
165
- type: Function,
166
- required: true
167
- },
168
- renderPlaceholder: {
169
- type: Function,
170
- required: true
171
- }
172
- },
173
- setup(props, { slots, emit }) {
174
- const { editor, decorate, renderElement, renderLeaf, renderPlaceholder } = props;
175
- if (!Node.isNodeList(editor.children)) {
176
- throw new Error(
177
- `[Slate] initialValue is invalid! Expected a list of elements but got: ${Scrubber.stringify(
178
- editor.children
179
- )}`
180
- );
181
- }
182
- provide(SLATE_USE_EDITOR, editor);
183
- provide(SLATE_USE_DECORATE, decorate);
184
- provide(SLATE_INNER_RENDER_ELEMENT, renderElement);
185
- provide(SLATE_INNER_RENDER_LEAF, renderLeaf);
186
- provide(SLATE_INNER_RENDER_PLACEHOLDER, renderPlaceholder);
187
- const isFocus = ref(DOMEditor.isFocused(editor));
188
- const selection = computed(() => editor.selection);
189
- provide(SLATE_STATE_FOCUS, isFocus);
190
- provide(SLATE_STATE_COMPOSING, ref(false));
191
- provide(SLATE_STATE_READ_ONLY, ref(false));
192
- provide(SLATE_STATE_SELECTION, selection);
193
- const focusCb = () => isFocus.value = DOMEditor.isFocused(editor);
194
- const changeEffect = ref(0);
195
- provide(SLATE_INNER_CHANGE_EFFECT_INJECT, changeEffect);
196
- const markPlaceholder = computed(() => {
197
- if (editor.selection && Range.isCollapsed(editor.selection) && editor.marks) {
198
- const anchor = editor.selection.anchor;
199
- const leaf = Node.leaf(editor, anchor.path);
200
- const { text, ...rest } = leaf;
201
- if (!Text.equals(leaf, editor.marks, { loose: true })) {
202
- const unset = Object.fromEntries(
203
- Object.keys(rest).map((mark) => [mark, null])
204
- );
205
- return {
206
- [MARK_PLACEHOLDER_SYMBOL]: true,
207
- ...unset,
208
- ...editor.marks,
209
- anchor,
210
- focus: anchor
211
- };
212
- }
213
- }
214
- return null;
215
- });
216
- provide(SLATE_INNER_MARK_PLACEHOLDER, markPlaceholder);
217
- onMounted(() => {
218
- document.addEventListener("focusin", focusCb);
219
- document.addEventListener("focusout", focusCb);
220
- EDITOR_TO_ON_CHANGE.set(editor, (options) => {
221
- var _a;
222
- emit("change", options);
223
- changeEffect.value++;
224
- switch ((_a = options == null ? void 0 : options.operation) == null ? void 0 : _a.type) {
225
- case "set_selection":
226
- emit("selectionchange", options);
227
- break;
228
- default:
229
- emit("valuechange", options);
230
- }
231
- });
232
- });
233
- onUnmounted(() => {
234
- document.removeEventListener("focusin", focusCb);
235
- document.removeEventListener("focusout", focusCb);
236
- EDITOR_TO_ON_CHANGE.delete(editor);
237
- });
238
- return () => renderSlot(slots, "default");
239
- }
240
- });
241
- const rtlRange = "֑-߿יִ-﷽ﹰ-ﻼ";
242
- const ltrRange = "A-Za-zÀ-ÖØ-öø-ʸ̀-֐ࠀ-῿‎Ⰰ-﬜︀-﹯﻽-￿";
243
- const rtl = new RegExp("^[^" + ltrRange + "]*[" + rtlRange + "]");
244
- const ltr = new RegExp("^[^" + rtlRange + "]*[" + ltrRange + "]");
245
- function direction(value) {
246
- const source = String(value || "");
247
- return rtl.test(source) ? "rtl" : ltr.test(source) ? "ltr" : "neutral";
248
- }
249
- const useDecorate = () => {
250
- const decorate = inject(SLATE_USE_DECORATE);
251
- if (decorate === void 0) {
252
- throw new Error(
253
- `The \`useDecorate\` hook must be used inside the <Slate> component's context.`
254
- );
255
- }
256
- return decorate;
257
- };
258
- const useEditor = () => {
259
- const editor = inject(SLATE_USE_EDITOR);
260
- if (editor === void 0) {
261
- throw new Error(
262
- `The \`useEditor\` hook must be used inside the <Slate> component's context.`
263
- );
264
- }
265
- return editor;
266
- };
267
- const StringComp = defineComponent({
268
- name: "slate-string",
269
- props: ["leaf", "text", "element", "isLast"],
270
- setup(props) {
271
- const { leaf, text, element, isLast } = props;
272
- const editor = useEditor();
273
- const getTextContent = computed(() => {
274
- const text2 = leaf.text;
275
- return (text2 ?? "") + (isLast && text2.at(-1) === "\n" ? "\n" : "");
276
- });
277
- const isLineBreak = computed(() => {
278
- const pathParent = Path.parent(DOMEditor.findPath(editor, text));
279
- return leaf.text === "" && element.children.at(-1) === text && !editor.isInline(element) && Editor.string(editor, pathParent) === "";
280
- });
281
- const zeroStringAttrs = computed(() => {
282
- const length = Node.string(element).length || 0;
283
- const isMarkPlaceholder = Boolean(leaf[MARK_PLACEHOLDER_SYMBOL]) || false;
284
- const isVoidParent = editor.isVoid(element);
285
- if (isVoidParent || isLineBreak.value || leaf.text === "") {
286
- return {
287
- "data-slate-zero-width": isLineBreak.value ? "n" : "z",
288
- "data-slate-length": length,
289
- "data-slate-mark-placeholder": isMarkPlaceholder ? true : void 0
290
- };
291
- }
292
- return null;
293
- });
294
- return () => zeroStringAttrs.value ? h("span", zeroStringAttrs.value, [
295
- !(IS_ANDROID || IS_IOS) || !isLineBreak.value ? "\uFEFF" : null,
296
- isLineBreak.value ? h("br") : null
297
- ]) : h("span", { "data-slate-string": true }, getTextContent.value);
298
- }
299
- });
300
- const useRenderElement = () => {
301
- const ELEMENT_RENDER = inject(
302
- SLATE_INNER_RENDER_ELEMENT
303
- );
304
- if (ELEMENT_RENDER === void 0) {
305
- throw new Error(
306
- `The \`useRenderElement\` hook must be used inside the <Slate> component's context.`
307
- );
308
- }
309
- return ELEMENT_RENDER;
310
- };
311
- const useRenderLeaf = () => {
312
- const LEAF_RENDER = inject(
313
- SLATE_INNER_RENDER_LEAF
314
- );
315
- if (LEAF_RENDER === void 0) {
316
- throw new Error(
317
- `The \`useRenderLeaf\` hook must be used inside the <Slate> component's context.`
318
- );
319
- }
320
- return LEAF_RENDER;
321
- };
322
- const useRenderPlaceholder = () => {
323
- const PLACEHOLDER_RENDER = inject(
324
- SLATE_INNER_RENDER_PLACEHOLDER
325
- );
326
- if (PLACEHOLDER_RENDER === void 0) {
327
- throw new Error(
328
- `The \`useRenderPlaceholder\` hook must be used inside the <Slate> component's context.`
329
- );
330
- }
331
- return PLACEHOLDER_RENDER;
332
- };
333
- const useChangeEffect = (fn) => {
334
- const CHANGE_EFFECT_INJECT = inject(
335
- SLATE_INNER_CHANGE_EFFECT_INJECT
336
- );
337
- if (CHANGE_EFFECT_INJECT === void 0) {
338
- throw new Error(
339
- `The \`useChangeEffect\` hook must be used inside the <Slate> component's context.`
340
- );
341
- }
342
- watch(() => CHANGE_EFFECT_INJECT.value, fn);
343
- return CHANGE_EFFECT_INJECT;
344
- };
345
- const useMarkPlaceholder = () => {
346
- const MARK_PLACEHOLDER_INJECT = inject(SLATE_INNER_MARK_PLACEHOLDER);
347
- if (MARK_PLACEHOLDER_INJECT === void 0) {
348
- throw new Error(
349
- `The \`useChangeEffect\` hook must be used inside the <Slate> component's context.`
350
- );
351
- }
352
- return MARK_PLACEHOLDER_INJECT;
353
- };
354
- const TextComp = defineComponent({
355
- name: "slate-text",
356
- props: ["text", "element"],
357
- setup(props) {
358
- const { text, element } = props;
359
- const editor = useEditor();
360
- const spanRef = ref();
361
- const decorate = useDecorate();
362
- const markPlaceholder = useMarkPlaceholder();
363
- const leaves = computed(() => {
364
- if (decorate === DEFAULT_DECORATE_FN) {
365
- return [text];
366
- }
367
- const elemPath = DOMEditor.findPath(editor, element);
368
- const textPath = DOMEditor.findPath(editor, text);
369
- const textDs = decorate([text, textPath]);
370
- const elemDs = decorate([element, elemPath]);
371
- const range = Editor.range(editor, textPath);
372
- elemDs.forEach((dec) => {
373
- textDs.push(Range.intersection(dec, range));
374
- });
375
- if (markPlaceholder.value) {
376
- textDs.unshift(markPlaceholder.value);
377
- }
378
- const filterDs = textDs.filter(Boolean);
379
- return Text.decorations(text, filterDs.length ? filterDs : []);
380
- });
381
- onMounted(() => {
382
- const key = DOMEditor.findKey(editor, text);
383
- if (spanRef.value) {
384
- const KEY_TO_ELEMENT = EDITOR_TO_KEY_TO_ELEMENT.get(editor);
385
- KEY_TO_ELEMENT == null ? void 0 : KEY_TO_ELEMENT.set(key, spanRef.value);
386
- ELEMENT_TO_NODE.set(spanRef.value, text);
387
- NODE_TO_ELEMENT.set(text, spanRef.value);
388
- }
389
- });
390
- onUnmounted(() => {
391
- NODE_TO_ELEMENT.delete(text);
392
- if (spanRef.value) {
393
- ELEMENT_TO_NODE.delete(spanRef.value);
394
- }
395
- });
396
- const isLastText = computed(() => {
397
- const isVoid = Editor.isVoid(editor, element);
398
- const isLeafBlock = Element.isElement(element) && !editor.isInline(element) && Editor.hasInlines(editor, element);
399
- return !isVoid && isLeafBlock && NODE_TO_INDEX.get(text) === element.children.length - 1;
400
- });
401
- const renderLeaf = useRenderLeaf();
402
- return () => h(
403
- "span",
404
- { "data-slate-node": "text", ref: spanRef },
405
- renderList(
406
- leaves.value,
407
- (leaf, i) => renderLeaf({
408
- text,
409
- leaf,
410
- attributes: { "data-slate-leaf": true },
411
- children: h(StringComp, {
412
- text,
413
- element,
414
- leaf,
415
- isLast: isLastText.value && i === leaves.value.length - 1,
416
- key: `${text.text}-${leaf.text}-${i}`
417
- })
418
- })
419
- )
420
- );
421
- }
422
- });
423
- const useReadOnly = () => {
424
- const isReadOnly = inject(SLATE_STATE_READ_ONLY);
425
- if (isReadOnly === void 0) {
426
- throw new Error(
427
- `The \`useFocused\` hook must be used inside the <Slate> component's context.`
428
- );
429
- }
430
- return isReadOnly;
431
- };
432
- const VOID_CHILDREN_ATTRS = {
433
- "data-slate-spacer": true,
434
- style: {
435
- height: "0",
436
- color: "transparent",
437
- outline: "none",
438
- position: "absolute"
439
- }
440
- };
441
- const ElementComp = defineComponent({
442
- name: "slate-element",
443
- props: ["element"],
444
- setup(props) {
445
- const element = props.element;
446
- const editor = useEditor();
447
- const selected = computed(() => {
448
- const path = DOMEditor.findPath(editor, element);
449
- if (!editor.selection) {
450
- return false;
451
- }
452
- return !!Range.intersection(Editor.range(editor, path), editor.selection);
453
- });
454
- provide(SLATE_USE_SELECTED, selected);
455
- const elementRef = ref(null);
456
- onMounted(() => {
457
- const key = DOMEditor.findKey(editor, element);
458
- const KEY_TO_ELEMENT = EDITOR_TO_KEY_TO_ELEMENT.get(editor);
459
- if (elementRef.value) {
460
- KEY_TO_ELEMENT == null ? void 0 : KEY_TO_ELEMENT.set(key, elementRef.value);
461
- NODE_TO_ELEMENT.set(element, elementRef.value);
462
- ELEMENT_TO_NODE.set(elementRef.value, element);
463
- }
464
- });
465
- onUnmounted(() => {
466
- const key = DOMEditor.findKey(editor, element);
467
- const KEY_TO_ELEMENT = EDITOR_TO_KEY_TO_ELEMENT.get(editor);
468
- KEY_TO_ELEMENT == null ? void 0 : KEY_TO_ELEMENT.delete(key);
469
- NODE_TO_ELEMENT.delete(element);
470
- });
471
- const isInline = computed(() => editor.isInline(element));
472
- const readOnly = useReadOnly();
473
- const attributes = computed(() => {
474
- const attr = {
475
- "data-slate-node": "element",
476
- ref: elementRef
477
- };
478
- if (isInline.value) {
479
- attr["data-slate-inline"] = true;
480
- } else if (Editor.hasInlines(editor, element)) {
481
- const text = Node.string(element);
482
- const dir = direction(text);
483
- if (dir === "rtl") {
484
- attr.dir = dir;
485
- }
486
- }
487
- if (Editor.isVoid(editor, element)) {
488
- attr["data-slate-void"] = true;
489
- if (!readOnly && isInline.value) {
490
- attr.contenteditable = false;
491
- }
492
- }
493
- return attr;
494
- });
495
- const children = computed(() => {
496
- if (!Editor.isVoid(editor, element)) {
497
- return h(Fragment, ChildrenFC(element, editor));
498
- }
499
- const [[text]] = Node.texts(element);
500
- NODE_TO_INDEX.set(text, 0);
501
- NODE_TO_PARENT.set(text, element);
502
- const tag = isInline.value ? "span" : "div";
503
- return h(tag, VOID_CHILDREN_ATTRS, h(TextComp, { element, text }));
504
- });
505
- const renderElement = useRenderElement();
506
- return () => renderElement({
507
- attributes: attributes.value,
508
- children: children.value,
509
- element
510
- });
511
- }
512
- });
513
- const ChildrenFC = (element, editor) => renderList(element.children, (child, i) => {
514
- const key = DOMEditor.findKey(editor, child);
515
- NODE_TO_INDEX.set(child, i);
516
- NODE_TO_PARENT.set(child, element);
517
- return Element.isElement(child) ? h(ElementComp, { element: child, key: key.id }) : h(TextComp, { text: child, element, key: key.id });
518
- });
519
- const useComposing = () => {
520
- const isComposing = inject(SLATE_STATE_COMPOSING);
521
- if (isComposing === void 0) {
522
- throw new Error(
523
- `The \`useFocused\` hook must be used inside the <Slate> component's context.`
524
- );
525
- }
526
- return isComposing;
527
- };
528
- const style = {
529
- position: "absolute",
530
- top: 0,
531
- pointerEvents: "none",
532
- width: "100%",
533
- maxWidth: "100%",
534
- display: "block",
535
- opacity: "0.333",
536
- userSelect: "none",
537
- textDecoration: "none",
538
- // Fixes https://github.com/udecode/plate/issues/2315
539
- WebkitUserModify: IS_WEBKIT ? "inherit" : void 0
540
- };
541
- const PlaceholderComp = defineComponent({
542
- name: "slate-placeholder",
543
- props: ["placeholder", "onPlaceholderResize"],
544
- setup(props) {
545
- const editor = useEditor();
546
- const placeholderResizeObserver = ref();
547
- const isComposing = useComposing();
548
- const showPlaceholder = computed(
549
- () => {
550
- var _a;
551
- return props.placeholder && ((_a = editor.children) == null ? void 0 : _a.length) === 1 && Array.from(Node.texts(editor)).length === 1 && Node.string(editor) === "" && !isComposing.value;
552
- }
553
- );
554
- const attributes = computed(() => ({
555
- "data-slate-placeholder": true,
556
- style,
557
- contenteditable: false,
558
- onVnodeMounted(vNode) {
559
- if (vNode.el) {
560
- placeholderResizeObserver.value = new ResizeObserver(() => {
561
- var _a, _b;
562
- props.onPlaceholderResize(
563
- (_b = (_a = vNode.el) == null ? void 0 : _a.getBoundingClientRect()) == null ? void 0 : _b.height
564
- );
565
- });
566
- placeholderResizeObserver.value.observe(vNode.el);
567
- }
568
- },
569
- onVnodeUnmounted() {
570
- var _a;
571
- (_a = placeholderResizeObserver.value) == null ? void 0 : _a.disconnect();
572
- placeholderResizeObserver.value = void 0;
573
- props.onPlaceholderResize();
574
- }
575
- }));
576
- const renderPlaceholder = useRenderPlaceholder();
577
- return () => showPlaceholder.value && renderPlaceholder({
578
- children: props.placeholder,
579
- attributes: attributes.value
580
- });
581
- }
582
- });
583
- const MUTATION_OBSERVER_CONFIG = {
584
- subtree: true,
585
- childList: true,
586
- characterData: true,
587
- characterDataOldValue: true
588
- };
589
- const useAndroidManager = (editableRef) => {
590
- const editor = useEditor();
591
- const mutationObserver = ref();
592
- const schedule = ref();
593
- let insertPositionHint = false;
594
- onMounted(() => {
595
- mutationObserver.value = new MutationObserver((mutations) => {
596
- var _a, _b, _c;
597
- (_a = mutationObserver.value) == null ? void 0 : _a.disconnect();
598
- (_b = mutationObserver.value) == null ? void 0 : _b.takeRecords();
599
- mutations.reverse().forEach((mutation) => {
600
- if (mutation.type === "characterData") {
601
- return;
602
- }
603
- mutation.removedNodes.forEach((node) => {
604
- mutation.target.insertBefore(node, mutation.nextSibling);
605
- });
606
- mutation.addedNodes.forEach((node) => {
607
- mutation.target.removeChild(node);
608
- });
609
- });
610
- (_c = schedule.value) == null ? void 0 : _c.call(schedule);
611
- schedule.value = void 0;
612
- });
613
- });
614
- const scheduleAction = (run, at) => {
615
- insertPositionHint = false;
616
- schedule.value = run;
617
- };
618
- const storeDiff = (path, diff) => {
619
- const target = Node.leaf(editor, path);
620
- const normalized = normalizeStringDiff(target.text, diff);
621
- if (!normalized) {
622
- return;
623
- }
624
- scheduleAction(() => {
625
- if (normalized.start !== normalized.end) {
626
- if (normalized.text) {
627
- Editor.deleteBackward(editor, { unit: "word" });
628
- } else {
629
- Editor.deleteBackward(editor, { unit: "character" });
630
- }
631
- }
632
- Editor.insertText(editor, normalized.text);
633
- });
634
- };
635
- const handleDOMBeforeInput = (event) => {
636
- var _a;
637
- (_a = mutationObserver.value) == null ? void 0 : _a.observe(
638
- editableRef.value,
639
- MUTATION_OBSERVER_CONFIG
640
- );
641
- const { inputType: type } = event;
642
- let targetRange = null;
643
- const data = event.dataTransfer || event.data || void 0;
644
- if (insertPositionHint !== false && type !== "insertText" && type !== "insertCompositionText") {
645
- insertPositionHint = false;
646
- }
647
- let [nativeTargetRange] = event.getTargetRanges();
648
- if (nativeTargetRange) {
649
- targetRange = DOMEditor.toSlateRange(editor, nativeTargetRange, {
650
- exactMatch: false,
651
- suppressThrow: true
652
- });
653
- }
654
- const window2 = DOMEditor.getWindow(editor);
655
- const domSelection = window2.getSelection();
656
- if (!targetRange && domSelection) {
657
- nativeTargetRange = domSelection;
658
- targetRange = DOMEditor.toSlateRange(editor, domSelection, {
659
- exactMatch: false,
660
- suppressThrow: true
661
- });
662
- }
663
- targetRange = targetRange ?? editor.selection;
664
- if (!targetRange) {
665
- return;
666
- }
667
- let canStoreDiff = true;
668
- if (type.startsWith("delete")) {
669
- if (Range.isExpanded(targetRange)) {
670
- const [start2, end2] = Range.edges(targetRange);
671
- const leaf2 = Node.leaf(editor, start2.path);
672
- if (leaf2.text.length === start2.offset && end2.offset === 0) {
673
- const next = Editor.next(editor, {
674
- at: start2.path,
675
- match: Text.isText
676
- });
677
- if (next && Path.equals(next[1], end2.path)) {
678
- targetRange = { anchor: end2, focus: end2 };
679
- }
680
- }
681
- }
682
- const direction2 = type.endsWith("Backward") ? "backward" : "forward";
683
- const [start, end] = Range.edges(targetRange);
684
- const [leaf] = Editor.leaf(editor, start.path);
685
- const diff = {
686
- text: "",
687
- start: start.offset,
688
- end: end.offset
689
- };
690
- const diffs = [diff];
691
- const text = applyStringDiff(leaf.text, ...diffs);
692
- if (text.length === 0) {
693
- canStoreDiff = false;
694
- }
695
- if (Range.isExpanded(targetRange)) {
696
- if (canStoreDiff && Path.equals(targetRange.anchor.path, targetRange.focus.path)) {
697
- const point = { path: targetRange.anchor.path, offset: start.offset };
698
- Editor.range(editor, point, point);
699
- return storeDiff(targetRange.anchor.path, {
700
- text: "",
701
- end: end.offset,
702
- start: start.offset
703
- });
704
- }
705
- return scheduleAction(
706
- () => Editor.deleteFragment(editor, { direction: direction2 })
707
- );
708
- }
709
- }
710
- switch (type) {
711
- case "deleteByComposition":
712
- case "deleteByCut":
713
- case "deleteByDrag": {
714
- return scheduleAction(() => Editor.deleteFragment(editor));
715
- }
716
- case "deleteContent":
717
- case "deleteContentForward": {
718
- const { anchor } = targetRange;
719
- if (canStoreDiff && Range.isCollapsed(targetRange)) {
720
- const targetNode = Node.leaf(editor, anchor.path);
721
- if (anchor.offset < targetNode.text.length) {
722
- return storeDiff(anchor.path, {
723
- text: "",
724
- start: anchor.offset,
725
- end: anchor.offset + 1
726
- });
727
- }
728
- }
729
- return scheduleAction(() => Editor.deleteForward(editor));
730
- }
731
- case "deleteContentBackward": {
732
- const { anchor } = targetRange;
733
- const nativeCollapsed = isDOMSelection(nativeTargetRange) ? nativeTargetRange.isCollapsed : !!(nativeTargetRange == null ? void 0 : nativeTargetRange.collapsed);
734
- if (canStoreDiff && nativeCollapsed && Range.isCollapsed(targetRange) && anchor.offset > 0) {
735
- return storeDiff(anchor.path, {
736
- text: "",
737
- start: anchor.offset - 1,
738
- end: anchor.offset
739
- });
740
- }
741
- return scheduleAction(() => Editor.deleteBackward(editor));
742
- }
743
- case "deleteEntireSoftLine": {
744
- return scheduleAction(() => {
745
- Editor.deleteBackward(editor, { unit: "line" });
746
- Editor.deleteForward(editor, { unit: "line" });
747
- });
748
- }
749
- case "deleteHardLineBackward": {
750
- return scheduleAction(
751
- () => Editor.deleteBackward(editor, { unit: "block" })
752
- );
753
- }
754
- case "deleteSoftLineBackward": {
755
- return scheduleAction(
756
- () => Editor.deleteBackward(editor, { unit: "line" })
757
- );
758
- }
759
- case "deleteHardLineForward": {
760
- return scheduleAction(
761
- () => Editor.deleteForward(editor, { unit: "block" })
762
- );
763
- }
764
- case "deleteSoftLineForward": {
765
- return scheduleAction(
766
- () => Editor.deleteForward(editor, { unit: "line" })
767
- );
768
- }
769
- case "deleteWordBackward": {
770
- return scheduleAction(
771
- () => Editor.deleteBackward(editor, { unit: "word" })
772
- );
773
- }
774
- case "deleteWordForward": {
775
- return scheduleAction(
776
- () => Editor.deleteForward(editor, { unit: "word" })
777
- );
778
- }
779
- case "insertLineBreak": {
780
- return scheduleAction(
781
- () => Editor.insertSoftBreak(editor)
782
- );
783
- }
784
- case "insertParagraph": {
785
- return scheduleAction(() => Editor.insertBreak(editor));
786
- }
787
- case "insertCompositionText":
788
- case "deleteCompositionText":
789
- case "insertFromComposition":
790
- case "insertFromDrop":
791
- case "insertFromPaste":
792
- case "insertFromYank":
793
- case "insertReplacementText":
794
- case "insertText": {
795
- if (data instanceof DataTransfer) {
796
- return scheduleAction(
797
- () => DOMEditor.insertData(editor, data)
798
- );
799
- }
800
- let text = data ?? "";
801
- if (type === "insertText" && /.*\n.*\n$/.test(text)) {
802
- text = text.slice(0, -1);
803
- }
804
- if (text.includes("\n")) {
805
- return scheduleAction(() => {
806
- const parts = text.split("\n");
807
- parts.forEach((line, i) => {
808
- if (line) {
809
- Editor.insertText(editor, line);
810
- }
811
- if (i !== parts.length - 1) {
812
- Editor.insertSoftBreak(editor);
813
- }
814
- });
815
- });
816
- }
817
- if (Path.equals(targetRange.anchor.path, targetRange.focus.path)) {
818
- const [start, end] = Range.edges(targetRange);
819
- const diff = {
820
- start: start.offset,
821
- end: end.offset,
822
- text
823
- };
824
- if (text && insertPositionHint && type === "insertCompositionText") {
825
- const hintPosition = insertPositionHint.start + insertPositionHint.text.search(/\S|$/);
826
- const diffPosition = diff.start + diff.text.search(/\S|$/);
827
- if (diffPosition === hintPosition + 1 && diff.end === insertPositionHint.start + insertPositionHint.text.length) {
828
- diff.start -= 1;
829
- insertPositionHint = null;
830
- } else {
831
- insertPositionHint = false;
832
- }
833
- } else if (type === "insertText") {
834
- if (insertPositionHint === null) {
835
- insertPositionHint = diff;
836
- } else if (insertPositionHint && Range.isCollapsed(targetRange) && insertPositionHint.end + insertPositionHint.text.length === start.offset) {
837
- insertPositionHint = {
838
- ...insertPositionHint,
839
- text: insertPositionHint.text + text
840
- };
841
- } else {
842
- insertPositionHint = false;
843
- }
844
- } else {
845
- insertPositionHint = false;
846
- }
847
- if (canStoreDiff) {
848
- storeDiff(start.path, diff);
849
- return;
850
- }
851
- }
852
- return scheduleAction(
853
- () => Editor.insertText(editor, text)
854
- );
855
- }
856
- }
857
- };
858
- return {
859
- handleDOMBeforeInput
860
- };
861
- };
862
- const Editable = defineComponent({
863
- name: "slate-editable",
864
- props: {
865
- scrollSelectionIntoView: {
866
- type: Function,
867
- required: false,
868
- default: defaultScrollSelectionIntoView
869
- },
870
- placeholder: { type: String },
871
- readOnly: {
872
- type: Boolean,
873
- default: () => false
874
- },
875
- role: { type: String },
876
- style: {
877
- type: Object,
878
- default: () => {
879
- }
880
- },
881
- is: {
882
- type: String,
883
- default: () => "div"
884
- }
885
- },
886
- setup(props) {
887
- const { placeholder, readOnly, scrollSelectionIntoView, is } = props;
888
- const editor = useEditor();
889
- const attributes = useAttrs();
890
- const isComposing = useComposing();
891
- const isReadOnly = useReadOnly();
892
- watch(
893
- () => readOnly,
894
- (current) => {
895
- IS_READ_ONLY.set(editor, current);
896
- isReadOnly.value = current;
897
- }
898
- );
899
- const state = reactive({
900
- isDraggingInternally: false,
901
- latestElement: null
902
- });
903
- const placeholderHeight = ref();
904
- const onPlaceholderResize = (h2) => placeholderHeight.value = h2;
905
- const processing = ref(false);
906
- const onDOMSelectionChange = (event) => {
907
- const target = event == null ? void 0 : event.target;
908
- const targetElement = target instanceof HTMLElement ? target : null;
909
- const targetTagName = targetElement == null ? void 0 : targetElement.tagName;
910
- if (targetTagName === "INPUT" || targetTagName === "TEXTAREA") {
911
- return;
912
- }
913
- const el = DOMEditor.toDOMNode(editor, editor);
914
- const root = el.getRootNode();
915
- if (!processing.value && IS_WEBKIT && root instanceof ShadowRoot) {
916
- processing.value = true;
917
- const active = getActiveElement();
918
- if (active) {
919
- document.execCommand("indent");
920
- } else {
921
- Transforms.deselect(editor);
922
- }
923
- return;
924
- }
925
- if ((IS_ANDROID || !DOMEditor.isComposing(editor)) && !state.isDraggingInternally) {
926
- const root2 = DOMEditor.findDocumentOrShadowRoot(editor);
927
- const { activeElement } = root2;
928
- const el2 = DOMEditor.toDOMNode(editor, editor);
929
- const domSelection = getSelection(root2);
930
- if (activeElement === el2) {
931
- state.latestElement = activeElement;
932
- IS_FOCUSED.set(editor, true);
933
- } else {
934
- IS_FOCUSED.delete(editor);
935
- }
936
- if (!domSelection) {
937
- return Transforms.deselect(editor);
938
- }
939
- const { anchorNode, focusNode } = domSelection;
940
- const anchorNodeSelectable = DOMEditor.hasEditableTarget(editor, anchorNode) || DOMEditor.isTargetInsideNonReadonlyVoid(editor, anchorNode);
941
- const focusNodeInEditor = DOMEditor.hasTarget(editor, focusNode);
942
- if (anchorNodeSelectable && focusNodeInEditor) {
943
- const range = DOMEditor.toSlateRange(editor, domSelection, {
944
- exactMatch: false,
945
- suppressThrow: true
946
- });
947
- if (range && !DOMEditor.isComposing(editor)) {
948
- Transforms.select(editor, range);
949
- }
950
- }
951
- if (readOnly && (!anchorNodeSelectable || !focusNodeInEditor)) {
952
- Transforms.deselect(editor);
953
- }
954
- }
955
- };
956
- const editableRef = ref();
957
- const androidManager = ref();
958
- if (IS_ANDROID) {
959
- androidManager.value = useAndroidManager(editableRef);
960
- }
961
- onMounted(() => {
962
- let window2;
963
- if (editableRef.value && (window2 = getDefaultView(editableRef.value))) {
964
- EDITOR_TO_WINDOW.set(editor, window2);
965
- EDITOR_TO_ELEMENT.set(editor, editableRef.value);
966
- NODE_TO_ELEMENT.set(editor, editableRef.value);
967
- ELEMENT_TO_NODE.set(editableRef.value, editor);
968
- }
969
- });
970
- onUnmounted(() => {
971
- EDITOR_TO_ELEMENT.delete(editor);
972
- NODE_TO_ELEMENT.delete(editor);
973
- });
974
- const hasMarkPlaceholder = computed(() => {
975
- if (editor.selection && Range.isCollapsed(editor.selection) && editor.marks) {
976
- const anchor = editor.selection.anchor;
977
- const leaf = Node.leaf(editor, anchor.path);
978
- if (!Text.equals(leaf, editor.marks, { loose: true })) {
979
- return true;
980
- }
981
- }
982
- return false;
983
- });
984
- const setDomSelection = () => {
985
- var _a;
986
- const root = DOMEditor.findDocumentOrShadowRoot(editor);
987
- const domSelection = getSelection(root);
988
- if (!domSelection) {
989
- return;
990
- }
991
- const hasDomSelection = domSelection.type !== "None";
992
- if (!editor.selection && !hasDomSelection) {
993
- return;
994
- }
995
- const focusNode = domSelection.focusNode;
996
- let anchorNode;
997
- if (IS_FIREFOX && domSelection.rangeCount > 1) {
998
- const firstRange = domSelection.getRangeAt(0);
999
- const lastRange = domSelection.getRangeAt(domSelection.rangeCount - 1);
1000
- if (firstRange.startContainer === focusNode) {
1001
- anchorNode = lastRange.endContainer;
1002
- } else {
1003
- anchorNode = firstRange.startContainer;
1004
- }
1005
- } else {
1006
- anchorNode = domSelection.anchorNode;
1007
- }
1008
- const editorElement = EDITOR_TO_ELEMENT.get(editor);
1009
- let hasDomSelectionInEditor = false;
1010
- if (editorElement.contains(anchorNode) && editorElement.contains(focusNode)) {
1011
- hasDomSelectionInEditor = true;
1012
- }
1013
- if (hasDomSelection && hasDomSelectionInEditor && editor.selection) {
1014
- const slateRange = DOMEditor.toSlateRange(editor, domSelection, {
1015
- exactMatch: true,
1016
- // domSelection is not necessarily a valid Slate range
1017
- // (e.g. when clicking on contenteditable:false element)
1018
- suppressThrow: true
1019
- });
1020
- if (slateRange && Range.equals(slateRange, editor.selection)) {
1021
- if (!hasMarkPlaceholder.value) {
1022
- return;
1023
- }
1024
- if ((_a = anchorNode == null ? void 0 : anchorNode.parentElement) == null ? void 0 : _a.hasAttribute(
1025
- "data-slate-mark-placeholder"
1026
- )) {
1027
- return;
1028
- }
1029
- }
1030
- }
1031
- if (editor.selection && !DOMEditor.hasRange(editor, editor.selection)) {
1032
- editor.selection = DOMEditor.toSlateRange(editor, domSelection, {
1033
- exactMatch: false,
1034
- suppressThrow: true
1035
- });
1036
- return;
1037
- }
1038
- let newDomRange = null;
1039
- try {
1040
- newDomRange = editor.selection && DOMEditor.toDOMRange(editor, editor.selection);
1041
- } catch (e2) {
1042
- }
1043
- if (newDomRange) {
1044
- if (DOMEditor.isComposing(editor) && !IS_ANDROID) {
1045
- domSelection.collapseToEnd();
1046
- } else if (Range.isBackward(editor.selection)) {
1047
- domSelection.setBaseAndExtent(
1048
- newDomRange.endContainer,
1049
- newDomRange.endOffset,
1050
- newDomRange.startContainer,
1051
- newDomRange.startOffset
1052
- );
1053
- } else {
1054
- domSelection.setBaseAndExtent(
1055
- newDomRange.startContainer,
1056
- newDomRange.startOffset,
1057
- newDomRange.endContainer,
1058
- newDomRange.endOffset
1059
- );
1060
- }
1061
- scrollSelectionIntoView(editor, newDomRange);
1062
- } else {
1063
- domSelection.removeAllRanges();
1064
- }
1065
- return newDomRange;
1066
- };
1067
- useChangeEffect(() => {
1068
- const root = DOMEditor.findDocumentOrShadowRoot(editor);
1069
- const domSelection = getSelection(root);
1070
- if (!domSelection || !DOMEditor.isFocused(editor)) {
1071
- return;
1072
- }
1073
- if (domSelection.rangeCount <= 1) {
1074
- setDomSelection();
1075
- }
1076
- });
1077
- const stoppedDragging = () => state.isDraggingInternally = false;
1078
- onMounted(() => {
1079
- const window2 = DOMEditor.getWindow(editor);
1080
- window2.document.addEventListener("selectionchange", onDOMSelectionChange);
1081
- window2.document.addEventListener("dragend", stoppedDragging);
1082
- window2.document.addEventListener("drop", stoppedDragging);
1083
- });
1084
- onUnmounted(() => {
1085
- const window2 = DOMEditor.getWindow(editor);
1086
- window2.document.removeEventListener(
1087
- "selectionchange",
1088
- onDOMSelectionChange
1089
- );
1090
- window2.document.removeEventListener("dragend", stoppedDragging);
1091
- window2.document.removeEventListener("drop", stoppedDragging);
1092
- });
1093
- const deferredOperations = ref([]);
1094
- const onBeforeinput = (event) => {
1095
- var _a, _b, _c, _d, _e;
1096
- const isInputEvent = event instanceof InputEvent;
1097
- if (!isInputEvent) {
1098
- return;
1099
- }
1100
- if (HAS_BEFORE_INPUT_SUPPORT) {
1101
- handleNativeHistoryEvents(editor, event);
1102
- const el = DOMEditor.toDOMNode(editor, editor);
1103
- const root = el.getRootNode();
1104
- if (processing.value && IS_WEBKIT && root instanceof ShadowRoot) {
1105
- const ranges = event.getTargetRanges();
1106
- const range = ranges[0];
1107
- const newRange = new window.Range();
1108
- let endContainer = range.endContainer;
1109
- let endOffset = range.endOffset;
1110
- if (endContainer.nodeType === 3 && endContainer.textContent === "" && endContainer.previousSibling) {
1111
- endContainer = endContainer.previousSibling;
1112
- }
1113
- while (endContainer instanceof HTMLElement && !endContainer.attributes.getNamedItem("data-slate-string") && endContainer.lastElementChild) {
1114
- endContainer = endContainer.lastElementChild;
1115
- }
1116
- if (endContainer instanceof HTMLElement && endContainer.attributes.getNamedItem("data-slate-string") && endContainer.lastChild) {
1117
- endContainer = endContainer.lastChild;
1118
- }
1119
- newRange.setStart(range.startContainer, range.startOffset);
1120
- newRange.setEnd(endContainer, endOffset);
1121
- const slateRange = DOMEditor.toSlateRange(editor, newRange, {
1122
- exactMatch: false,
1123
- suppressThrow: false
1124
- });
1125
- Transforms.select(editor, slateRange);
1126
- processing.value = false;
1127
- event.preventDefault();
1128
- event.stopImmediatePropagation();
1129
- return;
1130
- }
1131
- if (!readOnly && DOMEditor.hasEditableTarget(editor, event.target) && !isDOMEventHandled(event, attributes.onBeforeinput)) {
1132
- if (IS_ANDROID) {
1133
- return (_a = androidManager.value) == null ? void 0 : _a.handleDOMBeforeInput(event);
1134
- }
1135
- const { selection } = editor;
1136
- const { inputType } = event;
1137
- const data = event.dataTransfer || event.data || void 0;
1138
- const isCompositionChange = inputType === "insertCompositionText" || inputType === "deleteCompositionText";
1139
- if (isCompositionChange && DOMEditor.isComposing(editor)) {
1140
- return;
1141
- }
1142
- let native = false;
1143
- if (inputType === "insertText" && selection && Range.isCollapsed(selection) && // Only use native character insertion for single characters a-z or space for now.
1144
- // Long-press events (hold a + press 4 = ä) to choose a special character otherwise
1145
- // causes duplicate inserts.
1146
- event.data && event.data.length === 1 && /[a-z ]/i.test(event.data) && // Chrome has issues correctly editing the start of nodes: https://bugs.chromium.org/p/chromium/issues/detail?id=1249405
1147
- // When there is an inline element, e.g. a link, and you select
1148
- // right after it (the start of the next node).
1149
- selection.anchor.offset !== 0) {
1150
- native = true;
1151
- if (editor.marks) {
1152
- native = false;
1153
- }
1154
- const { anchor } = selection;
1155
- const [node, offset] = DOMEditor.toDOMPoint(editor, anchor);
1156
- const anchorNode = (_b = node.parentElement) == null ? void 0 : _b.closest("a");
1157
- const window2 = DOMEditor.getWindow(editor);
1158
- if (native && anchorNode && DOMEditor.hasDOMNode(editor, anchorNode)) {
1159
- const lastText = window2 == null ? void 0 : window2.document.createTreeWalker(anchorNode, NodeFilter.SHOW_TEXT).lastChild();
1160
- if (lastText === node && ((_c = lastText.textContent) == null ? void 0 : _c.length) === offset) {
1161
- native = false;
1162
- }
1163
- }
1164
- if (native && node.parentElement && ((_d = window2 == null ? void 0 : window2.getComputedStyle(node.parentElement)) == null ? void 0 : _d.whiteSpace) === "pre") {
1165
- const block = Editor.above(editor, {
1166
- at: anchor.path,
1167
- match: (n2) => Element.isElement(n2) && Editor.isBlock(editor, n2)
1168
- });
1169
- if (block && Node.string(block[0]).includes(" ")) {
1170
- native = false;
1171
- }
1172
- }
1173
- }
1174
- if (!inputType.startsWith("delete") || inputType.startsWith("deleteBy")) {
1175
- const [targetRange] = event.getTargetRanges();
1176
- if (targetRange) {
1177
- const range = DOMEditor.toSlateRange(editor, targetRange, {
1178
- exactMatch: false,
1179
- suppressThrow: false
1180
- });
1181
- if (!selection || !Range.equals(selection, range)) {
1182
- native = false;
1183
- const selectionRef = !isCompositionChange && editor.selection && Editor.rangeRef(editor, editor.selection);
1184
- Transforms.select(editor, range);
1185
- if (selectionRef) {
1186
- EDITOR_TO_USER_SELECTION.set(editor, selectionRef);
1187
- }
1188
- }
1189
- }
1190
- }
1191
- if (isCompositionChange) {
1192
- return;
1193
- }
1194
- if (!native) {
1195
- event.preventDefault();
1196
- }
1197
- if (selection && Range.isExpanded(selection) && inputType.startsWith("delete")) {
1198
- const direction2 = inputType.endsWith("Backward") ? "backward" : "forward";
1199
- Editor.deleteFragment(editor, { direction: direction2 });
1200
- return;
1201
- }
1202
- switch (inputType) {
1203
- case "deleteByComposition":
1204
- case "deleteByCut":
1205
- case "deleteByDrag": {
1206
- Editor.deleteFragment(editor);
1207
- break;
1208
- }
1209
- case "deleteContent":
1210
- case "deleteContentForward": {
1211
- Editor.deleteForward(editor);
1212
- break;
1213
- }
1214
- case "deleteContentBackward": {
1215
- Editor.deleteBackward(editor);
1216
- break;
1217
- }
1218
- case "deleteEntireSoftLine": {
1219
- Editor.deleteBackward(editor, { unit: "line" });
1220
- Editor.deleteForward(editor, { unit: "line" });
1221
- break;
1222
- }
1223
- case "deleteHardLineBackward": {
1224
- Editor.deleteBackward(editor, { unit: "block" });
1225
- break;
1226
- }
1227
- case "deleteSoftLineBackward": {
1228
- Editor.deleteBackward(editor, { unit: "line" });
1229
- break;
1230
- }
1231
- case "deleteHardLineForward": {
1232
- Editor.deleteForward(editor, { unit: "block" });
1233
- break;
1234
- }
1235
- case "deleteSoftLineForward": {
1236
- Editor.deleteForward(editor, { unit: "line" });
1237
- break;
1238
- }
1239
- case "deleteWordBackward": {
1240
- Editor.deleteBackward(editor, { unit: "word" });
1241
- break;
1242
- }
1243
- case "deleteWordForward": {
1244
- Editor.deleteForward(editor, { unit: "word" });
1245
- break;
1246
- }
1247
- case "insertLineBreak":
1248
- Editor.insertSoftBreak(editor);
1249
- break;
1250
- case "insertParagraph": {
1251
- Editor.insertBreak(editor);
1252
- break;
1253
- }
1254
- case "insertFromComposition":
1255
- case "insertFromDrop":
1256
- case "insertFromPaste":
1257
- case "insertFromYank":
1258
- case "insertReplacementText":
1259
- case "insertText": {
1260
- if (inputType === "insertFromComposition") {
1261
- if (DOMEditor.isComposing(editor)) {
1262
- isComposing.value = false;
1263
- IS_COMPOSING.set(editor, false);
1264
- }
1265
- }
1266
- if ((data == null ? void 0 : data.constructor.name) === "DataTransfer") {
1267
- DOMEditor.insertData(editor, data);
1268
- } else if (typeof data === "string") {
1269
- if (native) {
1270
- deferredOperations.value.push(
1271
- () => Editor.insertText(editor, data)
1272
- );
1273
- } else {
1274
- Editor.insertText(editor, data);
1275
- }
1276
- }
1277
- break;
1278
- }
1279
- }
1280
- const toRestore = (_e = EDITOR_TO_USER_SELECTION.get(editor)) == null ? void 0 : _e.unref();
1281
- EDITOR_TO_USER_SELECTION.delete(editor);
1282
- if (toRestore && (!editor.selection || !Range.equals(editor.selection, toRestore))) {
1283
- Transforms.select(editor, toRestore);
1284
- }
1285
- }
1286
- } else if (!readOnly && !isEventHandled(event, attributes.onBeforeinput) && DOMEditor.hasSelectableTarget(editor, event.target)) {
1287
- event.preventDefault();
1288
- if (!DOMEditor.isComposing(editor)) {
1289
- const text = event.data;
1290
- Editor.insertText(editor, text);
1291
- }
1292
- }
1293
- };
1294
- const onInput = (event) => {
1295
- if (IS_ANDROID || isEventHandled(event, attributes.onInput)) {
1296
- return;
1297
- }
1298
- for (const op of deferredOperations.value) {
1299
- op();
1300
- }
1301
- deferredOperations.value = [];
1302
- if (!DOMEditor.isFocused(editor)) {
1303
- handleNativeHistoryEvents(editor, event);
1304
- }
1305
- };
1306
- const onBlur = (event) => {
1307
- if (readOnly || !DOMEditor.hasSelectableTarget(editor, event.target) || isEventHandled(event, attributes.onBlur)) {
1308
- return;
1309
- }
1310
- const root = DOMEditor.findDocumentOrShadowRoot(editor);
1311
- if (state.latestElement === root.activeElement) {
1312
- return;
1313
- }
1314
- const { relatedTarget } = event;
1315
- const el = DOMEditor.toDOMNode(editor, editor);
1316
- if (relatedTarget === el) {
1317
- return;
1318
- }
1319
- if (isDOMElement(relatedTarget) && relatedTarget.hasAttribute("data-slate-spacer")) {
1320
- return;
1321
- }
1322
- if (relatedTarget != null && isDOMNode(relatedTarget) && DOMEditor.hasDOMNode(editor, relatedTarget)) {
1323
- const node = DOMEditor.toSlateNode(editor, relatedTarget);
1324
- if (Element.isElement(node) && !editor.isVoid(node)) {
1325
- return;
1326
- }
1327
- }
1328
- if (IS_WEBKIT) {
1329
- const domSelection = getSelection(root);
1330
- domSelection == null ? void 0 : domSelection.removeAllRanges();
1331
- }
1332
- IS_FOCUSED.delete(editor);
1333
- };
1334
- const onClick = (event) => {
1335
- if (DOMEditor.hasTarget(editor, event.target) && !isEventHandled(event, attributes.onClick) && isDOMNode(event.target)) {
1336
- const node = DOMEditor.toSlateNode(editor, event.target);
1337
- const path = DOMEditor.findPath(editor, node);
1338
- if (!Editor.hasPath(editor, path) || toRaw(Node.get(editor, path)) !== node) {
1339
- return;
1340
- }
1341
- if (event.detail === 3 && path.length >= 1) {
1342
- let blockPath = path;
1343
- if (!(Element.isElement(node) && Editor.isBlock(editor, node))) {
1344
- const block = Editor.above(editor, {
1345
- match: (n2) => Element.isElement(n2) && Editor.isBlock(editor, n2),
1346
- at: path
1347
- });
1348
- blockPath = (block == null ? void 0 : block[1]) ?? path.slice(0, 1);
1349
- }
1350
- const range = Editor.range(editor, blockPath);
1351
- Transforms.select(editor, range);
1352
- return;
1353
- }
1354
- if (readOnly) {
1355
- return;
1356
- }
1357
- const start = Editor.start(editor, path);
1358
- const end = Editor.end(editor, path);
1359
- const startVoid = Editor.void(editor, { at: start });
1360
- const endVoid = Editor.void(editor, { at: end });
1361
- if (startVoid && endVoid && Path.equals(startVoid[1], endVoid[1])) {
1362
- const range = Editor.range(editor, start);
1363
- Transforms.select(editor, range);
1364
- }
1365
- }
1366
- };
1367
- const onCompositionend = (event) => {
1368
- if (!DOMEditor.hasEditableTarget(editor, event.target) || DOMEditor.isTargetInsideNonReadonlyVoid(editor, event.target)) {
1369
- return;
1370
- }
1371
- if (DOMEditor.isComposing(editor)) {
1372
- isComposing.value = false;
1373
- IS_COMPOSING.set(editor, false);
1374
- }
1375
- if (IS_ANDROID || isEventHandled(event, attributes.onCompositionend)) {
1376
- return;
1377
- }
1378
- if (!IS_WEBKIT && !IS_FIREFOX_LEGACY && !IS_IOS && !IS_WECHATBROWSER && !IS_UC_MOBILE && event.data) {
1379
- Editor.insertText(editor, event.data);
1380
- const userMarks = EDITOR_TO_USER_MARKS.get(editor);
1381
- EDITOR_TO_USER_MARKS.delete(editor);
1382
- if (userMarks !== void 0) {
1383
- editor.marks = userMarks;
1384
- }
1385
- }
1386
- };
1387
- const onCompositionupdate = (event) => {
1388
- if (isEventHandled(event, attributes.onCompositionupdate)) {
1389
- return;
1390
- }
1391
- if (DOMEditor.hasSelectableTarget(editor, event.target)) {
1392
- if (!DOMEditor.isComposing(editor)) {
1393
- isComposing.value = true;
1394
- IS_COMPOSING.set(editor, true);
1395
- }
1396
- }
1397
- };
1398
- const onCompositionstart = (event) => {
1399
- if (DOMEditor.hasSelectableTarget(editor, event.target)) {
1400
- isComposing.value = true;
1401
- IS_COMPOSING.set(editor, true);
1402
- if (IS_ANDROID || isEventHandled(event, attributes.onCompositionstart)) {
1403
- return;
1404
- }
1405
- const { selection } = editor;
1406
- if (selection && Range.isExpanded(selection)) {
1407
- Editor.deleteFragment(editor);
1408
- return;
1409
- }
1410
- }
1411
- };
1412
- const onCopy = (event) => {
1413
- if (DOMEditor.hasSelectableTarget(editor, event.target) && !isEventHandled(event, attributes.onCopy) && !isDOMEventTargetInput(event)) {
1414
- event.preventDefault();
1415
- event.clipboardData && DOMEditor.setFragmentData(editor, event.clipboardData, "copy");
1416
- }
1417
- };
1418
- const onCut = (event) => {
1419
- if (!readOnly && DOMEditor.hasSelectableTarget(editor, event.target) && !isEventHandled(event, attributes.onCut) && !isDOMEventTargetInput(event)) {
1420
- event.preventDefault();
1421
- event.clipboardData && DOMEditor.setFragmentData(editor, event.clipboardData, "cut");
1422
- const selection = editor.selection;
1423
- if (selection) {
1424
- if (Range.isExpanded(selection)) {
1425
- Editor.deleteFragment(editor);
1426
- } else {
1427
- const node = Node.parent(editor, selection.anchor.path);
1428
- if (Editor.isVoid(editor, node)) {
1429
- Transforms.delete(editor);
1430
- }
1431
- }
1432
- }
1433
- }
1434
- };
1435
- const onDragover = (event) => {
1436
- if (DOMEditor.hasTarget(editor, event.target) && !isEventHandled(event, attributes.onDragover)) {
1437
- const node = DOMEditor.toSlateNode(editor, event.target);
1438
- if (Element.isElement(node) && Editor.isVoid(editor, node)) {
1439
- event.preventDefault();
1440
- }
1441
- }
1442
- };
1443
- const onDragstart = (event) => {
1444
- if (!readOnly && DOMEditor.hasTarget(editor, event.target) && !isEventHandled(event, attributes.onDragstart)) {
1445
- const node = DOMEditor.toSlateNode(editor, event.target);
1446
- const path = DOMEditor.findPath(editor, node);
1447
- const voidMatch = Element.isElement(node) && Editor.isVoid(editor, node) || Editor.void(editor, { at: path, voids: true });
1448
- if (voidMatch) {
1449
- const range = Editor.range(editor, path);
1450
- Transforms.select(editor, range);
1451
- }
1452
- state.isDraggingInternally = true;
1453
- event.dataTransfer && DOMEditor.setFragmentData(editor, event.dataTransfer, "drag");
1454
- }
1455
- };
1456
- const onDrop = (event) => {
1457
- if (!readOnly && DOMEditor.hasTarget(editor, event.target) && !isEventHandled(event, attributes.onDrop)) {
1458
- event.preventDefault();
1459
- const draggedRange = editor.selection;
1460
- const range = DOMEditor.findEventRange(editor, event);
1461
- const data = event.dataTransfer;
1462
- Transforms.select(editor, range);
1463
- if (state.isDraggingInternally) {
1464
- if (draggedRange && !Range.equals(draggedRange, range) && !Editor.void(editor, { at: range, voids: true })) {
1465
- Transforms.delete(editor, {
1466
- at: draggedRange
1467
- });
1468
- }
1469
- }
1470
- data && DOMEditor.insertData(editor, data);
1471
- if (!DOMEditor.isFocused(editor)) {
1472
- DOMEditor.focus(editor);
1473
- }
1474
- }
1475
- };
1476
- const onDragend = (event) => {
1477
- if (!readOnly && state.isDraggingInternally && attributes.onDragend && DOMEditor.hasTarget(editor, event.target)) {
1478
- attributes.onDragend(event);
1479
- }
1480
- };
1481
- const onFocus = (event) => {
1482
- if (!readOnly && DOMEditor.hasEditableTarget(editor, event.target) && !isEventHandled(event, attributes.onFocus)) {
1483
- const el = DOMEditor.toDOMNode(editor, editor);
1484
- const root = DOMEditor.findDocumentOrShadowRoot(editor);
1485
- state.latestElement = root.activeElement;
1486
- if (IS_FIREFOX && event.target !== el) {
1487
- el.focus();
1488
- return;
1489
- }
1490
- IS_FOCUSED.set(editor, true);
1491
- }
1492
- };
1493
- const onKeydown = (event) => {
1494
- if (!readOnly && DOMEditor.hasEditableTarget(editor, event.target)) {
1495
- if (DOMEditor.isComposing(editor) && event.isComposing === false) {
1496
- IS_COMPOSING.set(editor, false);
1497
- isComposing.value = false;
1498
- }
1499
- if (isEventHandled(event, attributes.onKeydown) || DOMEditor.isComposing(editor)) {
1500
- return;
1501
- }
1502
- const element = editor.children[editor.selection !== null ? editor.selection.focus.path[0] : 0];
1503
- const isRTL = direction(Node.string(element)) === "rtl";
1504
- if (Hotkeys.isRedo(event)) {
1505
- event.preventDefault();
1506
- const maybeHistoryEditor = editor;
1507
- if (typeof maybeHistoryEditor.redo === "function") {
1508
- maybeHistoryEditor.redo();
1509
- }
1510
- return;
1511
- }
1512
- if (Hotkeys.isUndo(event)) {
1513
- event.preventDefault();
1514
- const maybeHistoryEditor = editor;
1515
- if (typeof maybeHistoryEditor.undo === "function") {
1516
- maybeHistoryEditor.undo();
1517
- }
1518
- return;
1519
- }
1520
- if (Hotkeys.isMoveLineBackward(event)) {
1521
- event.preventDefault();
1522
- Transforms.move(editor, { unit: "line", reverse: true });
1523
- return;
1524
- }
1525
- if (Hotkeys.isMoveLineForward(event)) {
1526
- event.preventDefault();
1527
- Transforms.move(editor, { unit: "line" });
1528
- return;
1529
- }
1530
- if (Hotkeys.isExtendLineBackward(event)) {
1531
- event.preventDefault();
1532
- Transforms.move(editor, {
1533
- unit: "line",
1534
- edge: "focus",
1535
- reverse: true
1536
- });
1537
- return;
1538
- }
1539
- if (Hotkeys.isExtendLineForward(event)) {
1540
- event.preventDefault();
1541
- Transforms.move(editor, { unit: "line", edge: "focus" });
1542
- return;
1543
- }
1544
- if (Hotkeys.isMoveBackward(event)) {
1545
- event.preventDefault();
1546
- if (editor.selection && Range.isCollapsed(editor.selection)) {
1547
- Transforms.move(editor, { reverse: !isRTL });
1548
- } else {
1549
- Transforms.collapse(editor, {
1550
- edge: isRTL ? "end" : "start"
1551
- });
1552
- }
1553
- return;
1554
- }
1555
- if (Hotkeys.isMoveForward(event)) {
1556
- event.preventDefault();
1557
- if (editor.selection && Range.isCollapsed(editor.selection)) {
1558
- Transforms.move(editor, { reverse: isRTL });
1559
- } else {
1560
- Transforms.collapse(editor, {
1561
- edge: isRTL ? "start" : "end"
1562
- });
1563
- }
1564
- return;
1565
- }
1566
- if (Hotkeys.isMoveWordBackward(event)) {
1567
- event.preventDefault();
1568
- if (editor.selection && Range.isExpanded(editor.selection)) {
1569
- Transforms.collapse(editor, { edge: "focus" });
1570
- }
1571
- Transforms.move(editor, {
1572
- unit: "word",
1573
- reverse: !isRTL
1574
- });
1575
- return;
1576
- }
1577
- if (Hotkeys.isMoveWordForward(event)) {
1578
- event.preventDefault();
1579
- if (editor.selection && Range.isExpanded(editor.selection)) {
1580
- Transforms.collapse(editor, { edge: "focus" });
1581
- }
1582
- Transforms.move(editor, {
1583
- unit: "word",
1584
- reverse: isRTL
1585
- });
1586
- return;
1587
- }
1588
- if (!HAS_BEFORE_INPUT_SUPPORT) {
1589
- if (Hotkeys.isBold(event) || Hotkeys.isItalic(event) || Hotkeys.isTransposeCharacter(event)) {
1590
- event.preventDefault();
1591
- return;
1592
- }
1593
- if (Hotkeys.isSoftBreak(event)) {
1594
- event.preventDefault();
1595
- Editor.insertSoftBreak(editor);
1596
- return;
1597
- }
1598
- if (Hotkeys.isSplitBlock(event)) {
1599
- event.preventDefault();
1600
- Editor.insertBreak(editor);
1601
- return;
1602
- }
1603
- if (Hotkeys.isDeleteBackward(event)) {
1604
- event.preventDefault();
1605
- if (editor.selection && Range.isExpanded(editor.selection)) {
1606
- Editor.deleteFragment(editor, {
1607
- direction: "backward"
1608
- });
1609
- } else {
1610
- Editor.deleteBackward(editor);
1611
- }
1612
- return;
1613
- }
1614
- if (Hotkeys.isDeleteForward(event)) {
1615
- event.preventDefault();
1616
- if (editor.selection && Range.isExpanded(editor.selection)) {
1617
- Editor.deleteFragment(editor, {
1618
- direction: "forward"
1619
- });
1620
- } else {
1621
- Editor.deleteForward(editor);
1622
- }
1623
- return;
1624
- }
1625
- if (Hotkeys.isDeleteLineBackward(event)) {
1626
- event.preventDefault();
1627
- if (editor.selection && Range.isExpanded(editor.selection)) {
1628
- Editor.deleteFragment(editor, {
1629
- direction: "backward"
1630
- });
1631
- } else {
1632
- Editor.deleteBackward(editor, { unit: "line" });
1633
- }
1634
- return;
1635
- }
1636
- if (Hotkeys.isDeleteLineForward(event)) {
1637
- event.preventDefault();
1638
- if (editor.selection && Range.isExpanded(editor.selection)) {
1639
- Editor.deleteFragment(editor, {
1640
- direction: "forward"
1641
- });
1642
- } else {
1643
- Editor.deleteForward(editor, { unit: "line" });
1644
- }
1645
- return;
1646
- }
1647
- if (Hotkeys.isDeleteWordBackward(event)) {
1648
- event.preventDefault();
1649
- if (editor.selection && Range.isExpanded(editor.selection)) {
1650
- Editor.deleteFragment(editor, {
1651
- direction: "backward"
1652
- });
1653
- } else {
1654
- Editor.deleteBackward(editor, { unit: "word" });
1655
- }
1656
- return;
1657
- }
1658
- if (Hotkeys.isDeleteWordForward(event)) {
1659
- event.preventDefault();
1660
- if (editor.selection && Range.isExpanded(editor.selection)) {
1661
- Editor.deleteFragment(editor, {
1662
- direction: "forward"
1663
- });
1664
- } else {
1665
- Editor.deleteForward(editor, { unit: "word" });
1666
- }
1667
- return;
1668
- }
1669
- } else {
1670
- if (IS_CHROME || IS_WEBKIT) {
1671
- if (editor.selection && (Hotkeys.isDeleteBackward(event) || Hotkeys.isDeleteForward(event)) && Range.isCollapsed(editor.selection)) {
1672
- const currentNode = Node.parent(
1673
- editor,
1674
- editor.selection.anchor.path
1675
- );
1676
- if (Element.isElement(currentNode) && Editor.isVoid(editor, currentNode) && (Editor.isInline(editor, currentNode) || Editor.isBlock(editor, currentNode))) {
1677
- event.preventDefault();
1678
- Editor.deleteBackward(editor, { unit: "block" });
1679
- return;
1680
- }
1681
- }
1682
- }
1683
- }
1684
- }
1685
- };
1686
- const onPaste = (event) => {
1687
- if (!readOnly && DOMEditor.hasEditableTarget(editor, event.target) && !isEventHandled(event, attributes.onPaste)) {
1688
- if (!HAS_BEFORE_INPUT_SUPPORT || isPlainTextOnlyPaste(event) || IS_WEBKIT) {
1689
- event.preventDefault();
1690
- event.clipboardData && DOMEditor.insertData(editor, event.clipboardData);
1691
- }
1692
- }
1693
- };
1694
- const mergedEditableStyle = computed(() => ({
1695
- // Allow positioning relative to the editable element.
1696
- position: "relative",
1697
- // Preserve adjacent whitespace and new lines.
1698
- whiteSpace: "pre-wrap",
1699
- // Allow words to break if they are too long.
1700
- wordWrap: "break-word",
1701
- // Make the minimum height that of the placeholder.
1702
- minHeight: placeholderHeight.value ? placeholderHeight.value + "px" : void 0,
1703
- // Allow for passed-in styles to override anything.
1704
- ...props.style
1705
- }));
1706
- const spellcheck = computed(
1707
- () => HAS_BEFORE_INPUT_SUPPORT || !CAN_USE_DOM ? attributes.spellcheck : false
1708
- );
1709
- const autocorrect = computed(
1710
- () => HAS_BEFORE_INPUT_SUPPORT || !CAN_USE_DOM ? attributes.autocorrect : void 0
1711
- );
1712
- const autocapitalize = computed(
1713
- () => HAS_BEFORE_INPUT_SUPPORT || !CAN_USE_DOM ? attributes.autocapitalize : void 0
1714
- );
1715
- return () => h(
1716
- is,
1717
- {
1718
- role: readOnly ? void 0 : "textbox",
1719
- "aria-multiline": readOnly ? void 0 : true,
1720
- "data-slate-editor": true,
1721
- "data-slate-node": "value",
1722
- ...attributes,
1723
- zindex: -1,
1724
- spellcheck: spellcheck.value,
1725
- autocorrect: autocorrect.value,
1726
- autocapitalize: autocapitalize.value,
1727
- contenteditable: !readOnly,
1728
- ref: editableRef,
1729
- style: mergedEditableStyle.value,
1730
- onBeforeinput,
1731
- onInput,
1732
- onBlur,
1733
- onClick,
1734
- onCompositionend,
1735
- onCompositionupdate,
1736
- onCompositionstart,
1737
- onCopy,
1738
- onCut,
1739
- onDragover,
1740
- onDragstart,
1741
- onDrop,
1742
- onDragend,
1743
- onFocus,
1744
- onKeydown,
1745
- onPaste
1746
- },
1747
- [
1748
- ChildrenFC(editor, editor),
1749
- h(PlaceholderComp, {
1750
- placeholder,
1751
- onPlaceholderResize
1752
- })
1753
- ]
1754
- );
1755
- }
1756
- });
1757
- const useFocused = () => {
1758
- const isFocus = inject(SLATE_STATE_FOCUS);
1759
- if (isFocus === void 0) {
1760
- throw new Error(
1761
- `The \`useFocused\` hook must be used inside the <Slate> component's context.`
1762
- );
1763
- }
1764
- return isFocus;
1765
- };
1766
5
  const useSelected = () => {
1767
6
  const selected = inject(SLATE_USE_SELECTED);
1768
7
  if (selected === void 0) {
@@ -1795,16 +34,16 @@ const useInheritRef = (attribute) => {
1795
34
  return attribute;
1796
35
  };
1797
36
  export {
1798
- Editable,
1799
- Slate,
1800
- defaultRenderLeaf,
1801
- defaultRenderPlaceHolder,
37
+ E as Editable,
38
+ b as Slate,
39
+ d as defaultRenderLeaf,
40
+ c as defaultRenderPlaceHolder,
1802
41
  t as toRawWeakMap,
1803
- useComposing,
1804
- useEditor,
1805
- useFocused,
42
+ u as useComposing,
43
+ g as useEditor,
44
+ e as useFocused,
1806
45
  useInheritRef,
1807
- useReadOnly,
46
+ f as useReadOnly,
1808
47
  useSelected,
1809
48
  useSelection
1810
49
  };