@trafica/editor 1.0.32 → 1.0.33
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.d.mts +15 -3
- package/dist/index.d.ts +15 -3
- package/dist/index.js +865 -159
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +867 -161
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { useSyncExternalStore, useRef, useCallback, useState, useEffect, useLayoutEffect, useMemo } from 'react';
|
|
2
|
+
import { jsxs, jsx, Fragment } from 'react/jsx-runtime';
|
|
3
3
|
import { createPortal } from 'react-dom';
|
|
4
4
|
|
|
5
5
|
// src/editor/core/DocumentModel.ts
|
|
@@ -7831,6 +7831,857 @@ var jsonSerializer = {
|
|
|
7831
7831
|
}
|
|
7832
7832
|
}
|
|
7833
7833
|
};
|
|
7834
|
+
function Editor({
|
|
7835
|
+
engine,
|
|
7836
|
+
placeholder = "Start writing...",
|
|
7837
|
+
className = "",
|
|
7838
|
+
readOnly = false,
|
|
7839
|
+
onHTMLChange,
|
|
7840
|
+
onJSONChange,
|
|
7841
|
+
onOpenLinkPopup,
|
|
7842
|
+
onUploadImage: _onUploadImage
|
|
7843
|
+
}) {
|
|
7844
|
+
const containerRef = useRef(null);
|
|
7845
|
+
const isRenderingRef = useRef(false);
|
|
7846
|
+
const skipRestoreSelectionRef = useRef(false);
|
|
7847
|
+
const scrollCaretIntoView = useCallback(() => {
|
|
7848
|
+
var _a, _b;
|
|
7849
|
+
const sel = window.getSelection();
|
|
7850
|
+
if (!sel || sel.rangeCount === 0) return;
|
|
7851
|
+
const range = sel.getRangeAt(0).cloneRange();
|
|
7852
|
+
range.collapse(true);
|
|
7853
|
+
const caretRect = range.getBoundingClientRect();
|
|
7854
|
+
let rect = caretRect;
|
|
7855
|
+
if (!rect || rect.top === 0 && rect.bottom === 0 && rect.left === 0) {
|
|
7856
|
+
const node = range.startContainer;
|
|
7857
|
+
const el = node.nodeType === Node.ELEMENT_NODE ? node : node.parentElement;
|
|
7858
|
+
if (el) rect = el.getBoundingClientRect();
|
|
7859
|
+
}
|
|
7860
|
+
if (!rect || rect.bottom === 0) return;
|
|
7861
|
+
let scrollEl = (_b = (_a = containerRef.current) == null ? void 0 : _a.parentElement) != null ? _b : null;
|
|
7862
|
+
while (scrollEl) {
|
|
7863
|
+
const style = window.getComputedStyle(scrollEl);
|
|
7864
|
+
const overflow = style.overflow + style.overflowY;
|
|
7865
|
+
if (/auto|scroll/.test(overflow)) break;
|
|
7866
|
+
scrollEl = scrollEl.parentElement;
|
|
7867
|
+
}
|
|
7868
|
+
const PADDING = 24;
|
|
7869
|
+
if (scrollEl) {
|
|
7870
|
+
const containerRect = scrollEl.getBoundingClientRect();
|
|
7871
|
+
if (rect.bottom > containerRect.bottom - PADDING) {
|
|
7872
|
+
scrollEl.scrollBy({ top: rect.bottom - containerRect.bottom + PADDING, behavior: "smooth" });
|
|
7873
|
+
} else if (rect.top < containerRect.top + PADDING) {
|
|
7874
|
+
scrollEl.scrollBy({ top: rect.top - containerRect.top - PADDING, behavior: "smooth" });
|
|
7875
|
+
}
|
|
7876
|
+
} else {
|
|
7877
|
+
if (rect.bottom > window.innerHeight - PADDING) {
|
|
7878
|
+
window.scrollBy({ top: rect.bottom - window.innerHeight + PADDING, behavior: "smooth" });
|
|
7879
|
+
} else if (rect.top < PADDING) {
|
|
7880
|
+
window.scrollBy({ top: rect.top - PADDING, behavior: "smooth" });
|
|
7881
|
+
}
|
|
7882
|
+
}
|
|
7883
|
+
}, []);
|
|
7884
|
+
const isComposingRef = useRef(false);
|
|
7885
|
+
const stateRef = useRef(engine.getState());
|
|
7886
|
+
const [selectedImagePath, setSelectedImagePath] = useState(null);
|
|
7887
|
+
const [findReplaceOpen, setFindReplaceOpen] = useState(false);
|
|
7888
|
+
const [findReplaceMode, setFindReplaceMode] = useState("find");
|
|
7889
|
+
const [linkPopupOpen, setLinkPopupOpen] = useState(false);
|
|
7890
|
+
const [isSourceMode, setIsSourceMode] = useState(false);
|
|
7891
|
+
const [sourceHTML, setSourceHTML] = useState("");
|
|
7892
|
+
const [editorHeight, setEditorHeight] = useState(null);
|
|
7893
|
+
const editorRootRef = useRef(null);
|
|
7894
|
+
const resizeDragRef = useRef(null);
|
|
7895
|
+
const [linkTooltip, setLinkTooltip] = useState(null);
|
|
7896
|
+
const linkTooltipTimerRef = useRef(null);
|
|
7897
|
+
const handleToggleSource = useCallback(() => {
|
|
7898
|
+
if (!isSourceMode) {
|
|
7899
|
+
const html = htmlSerializer.serialize(engine.getState().doc);
|
|
7900
|
+
setSourceHTML(prettyPrintHTML(html));
|
|
7901
|
+
setIsSourceMode(true);
|
|
7902
|
+
} else {
|
|
7903
|
+
const newDoc = htmlSerializer.deserialize(sourceHTML);
|
|
7904
|
+
const tr = createTransaction();
|
|
7905
|
+
tr.steps.push(tr_replaceDoc(newDoc));
|
|
7906
|
+
engine.dispatch(tr);
|
|
7907
|
+
setIsSourceMode(false);
|
|
7908
|
+
}
|
|
7909
|
+
}, [isSourceMode, sourceHTML, engine]);
|
|
7910
|
+
useEffect(() => {
|
|
7911
|
+
const onMouseMove = (e) => {
|
|
7912
|
+
const drag = resizeDragRef.current;
|
|
7913
|
+
if (!drag) return;
|
|
7914
|
+
const newHeight = Math.max(120, drag.startHeight + (e.clientY - drag.startY));
|
|
7915
|
+
setEditorHeight(newHeight);
|
|
7916
|
+
};
|
|
7917
|
+
const onMouseUp = () => {
|
|
7918
|
+
resizeDragRef.current = null;
|
|
7919
|
+
};
|
|
7920
|
+
document.addEventListener("mousemove", onMouseMove);
|
|
7921
|
+
document.addEventListener("mouseup", onMouseUp);
|
|
7922
|
+
return () => {
|
|
7923
|
+
document.removeEventListener("mousemove", onMouseMove);
|
|
7924
|
+
document.removeEventListener("mouseup", onMouseUp);
|
|
7925
|
+
};
|
|
7926
|
+
}, []);
|
|
7927
|
+
const [tableContextMenu, setTableContextMenu] = useState(null);
|
|
7928
|
+
const [tableSelection, setTableSelection] = useState(null);
|
|
7929
|
+
const cellDragRef = useRef(null);
|
|
7930
|
+
const colResizeRef = useRef(null);
|
|
7931
|
+
const state = useEditorState(engine);
|
|
7932
|
+
const inTableCellPos = state.selection ? findCellPosition(state.doc, state.selection.anchor.path) : null;
|
|
7933
|
+
useLayoutEffect(() => {
|
|
7934
|
+
stateRef.current = state;
|
|
7935
|
+
}, [state]);
|
|
7936
|
+
useLayoutEffect(() => {
|
|
7937
|
+
const container = containerRef.current;
|
|
7938
|
+
if (!container) return;
|
|
7939
|
+
isRenderingRef.current = true;
|
|
7940
|
+
renderDocument(state.doc, container);
|
|
7941
|
+
if (!container.contains(document.activeElement)) {
|
|
7942
|
+
container.focus({ preventScroll: true });
|
|
7943
|
+
}
|
|
7944
|
+
if (state.selection) {
|
|
7945
|
+
restoreSelection(container, state.selection);
|
|
7946
|
+
scrollCaretIntoView();
|
|
7947
|
+
}
|
|
7948
|
+
isRenderingRef.current = false;
|
|
7949
|
+
if (onHTMLChange) {
|
|
7950
|
+
onHTMLChange(htmlSerializer.serialize(state.doc));
|
|
7951
|
+
}
|
|
7952
|
+
if (onJSONChange) {
|
|
7953
|
+
onJSONChange(jsonSerializer.serialize(state.doc));
|
|
7954
|
+
}
|
|
7955
|
+
}, [state.doc]);
|
|
7956
|
+
useLayoutEffect(() => {
|
|
7957
|
+
const container = containerRef.current;
|
|
7958
|
+
if (!container || !state.selection) return;
|
|
7959
|
+
if (skipRestoreSelectionRef.current) {
|
|
7960
|
+
skipRestoreSelectionRef.current = false;
|
|
7961
|
+
return;
|
|
7962
|
+
}
|
|
7963
|
+
isRenderingRef.current = true;
|
|
7964
|
+
restoreSelection(container, state.selection);
|
|
7965
|
+
scrollCaretIntoView();
|
|
7966
|
+
isRenderingRef.current = false;
|
|
7967
|
+
}, [state.selection]);
|
|
7968
|
+
useEffect(() => {
|
|
7969
|
+
const container = containerRef.current;
|
|
7970
|
+
if (!container || readOnly) return;
|
|
7971
|
+
return attachClipboardHandlers(container, engine);
|
|
7972
|
+
}, [engine, readOnly]);
|
|
7973
|
+
useEffect(() => {
|
|
7974
|
+
const container = containerRef.current;
|
|
7975
|
+
if (!container) return;
|
|
7976
|
+
container.querySelectorAll(".editor-cell-selected").forEach((el) => {
|
|
7977
|
+
el.classList.remove("editor-cell-selected");
|
|
7978
|
+
});
|
|
7979
|
+
if (!tableSelection) return;
|
|
7980
|
+
const { tablePath, anchorCell, focusCell } = tableSelection;
|
|
7981
|
+
const minRow = Math.min(anchorCell[0], focusCell[0]);
|
|
7982
|
+
const maxRow = Math.max(anchorCell[0], focusCell[0]);
|
|
7983
|
+
const minCol = Math.min(anchorCell[1], focusCell[1]);
|
|
7984
|
+
const maxCol = Math.max(anchorCell[1], focusCell[1]);
|
|
7985
|
+
for (let r = minRow; r <= maxRow; r++) {
|
|
7986
|
+
for (let c = minCol; c <= maxCol; c++) {
|
|
7987
|
+
const cellPath = [...tablePath, r, c];
|
|
7988
|
+
const td = container.querySelector(
|
|
7989
|
+
`[data-block-path="${JSON.stringify(cellPath)}"]`
|
|
7990
|
+
);
|
|
7991
|
+
if (td) td.classList.add("editor-cell-selected");
|
|
7992
|
+
}
|
|
7993
|
+
}
|
|
7994
|
+
}, [tableSelection, state.doc]);
|
|
7995
|
+
useEffect(() => {
|
|
7996
|
+
const container = containerRef.current;
|
|
7997
|
+
if (!container) return;
|
|
7998
|
+
container.querySelectorAll(".editor-cell-active").forEach((el) => {
|
|
7999
|
+
el.classList.remove("editor-cell-active");
|
|
8000
|
+
});
|
|
8001
|
+
if (!inTableCellPos) {
|
|
8002
|
+
setTableSelection(null);
|
|
8003
|
+
return;
|
|
8004
|
+
}
|
|
8005
|
+
if (tableSelection) {
|
|
8006
|
+
const { anchorCell, focusCell, tablePath } = tableSelection;
|
|
8007
|
+
const sameTable = JSON.stringify(tablePath) === JSON.stringify(inTableCellPos.tablePath);
|
|
8008
|
+
const singleCell = anchorCell[0] === focusCell[0] && anchorCell[1] === focusCell[1];
|
|
8009
|
+
if (!sameTable || singleCell) {
|
|
8010
|
+
setTableSelection(null);
|
|
8011
|
+
}
|
|
8012
|
+
}
|
|
8013
|
+
const cellPath = [...inTableCellPos.tablePath, inTableCellPos.row, inTableCellPos.col];
|
|
8014
|
+
const td = container.querySelector(
|
|
8015
|
+
`[data-block-path="${JSON.stringify(cellPath)}"]`
|
|
8016
|
+
);
|
|
8017
|
+
if (td) td.classList.add("editor-cell-active");
|
|
8018
|
+
}, [inTableCellPos, state.doc]);
|
|
8019
|
+
useEffect(() => {
|
|
8020
|
+
const onSelectionChange = () => {
|
|
8021
|
+
if (isRenderingRef.current) return;
|
|
8022
|
+
const container = containerRef.current;
|
|
8023
|
+
if (!container) return;
|
|
8024
|
+
if (!isSelectionInContainer(container)) return;
|
|
8025
|
+
const captured = captureSelection(container);
|
|
8026
|
+
if (!captured) return;
|
|
8027
|
+
const currentSelection = engine.getState().selection;
|
|
8028
|
+
if (currentSelection && JSON.stringify(currentSelection.anchor) === JSON.stringify(captured.anchor) && JSON.stringify(currentSelection.focus) === JSON.stringify(captured.focus)) {
|
|
8029
|
+
return;
|
|
8030
|
+
}
|
|
8031
|
+
skipRestoreSelectionRef.current = true;
|
|
8032
|
+
const tr = createTransaction();
|
|
8033
|
+
tr.steps.push(tr_setSelection(captured));
|
|
8034
|
+
engine.dispatch(tr);
|
|
8035
|
+
};
|
|
8036
|
+
document.addEventListener(
|
|
8037
|
+
"selectionchange",
|
|
8038
|
+
onSelectionChange
|
|
8039
|
+
);
|
|
8040
|
+
return () => {
|
|
8041
|
+
document.removeEventListener(
|
|
8042
|
+
"selectionchange",
|
|
8043
|
+
onSelectionChange
|
|
8044
|
+
);
|
|
8045
|
+
};
|
|
8046
|
+
}, [engine]);
|
|
8047
|
+
const handleFocus = useCallback(() => {
|
|
8048
|
+
const currentState = engine.getState();
|
|
8049
|
+
if (!currentState.selection) {
|
|
8050
|
+
const tr = createTransaction();
|
|
8051
|
+
tr.steps.push(
|
|
8052
|
+
tr_setSelection(
|
|
8053
|
+
makeCollapsedSelection({ path: [0], offset: 0 })
|
|
8054
|
+
)
|
|
8055
|
+
);
|
|
8056
|
+
engine.dispatch(tr);
|
|
8057
|
+
}
|
|
8058
|
+
}, [engine]);
|
|
8059
|
+
const handleKeyDown = useCallback(
|
|
8060
|
+
(e) => {
|
|
8061
|
+
var _a, _b, _c;
|
|
8062
|
+
if (readOnly) return;
|
|
8063
|
+
if ((e.ctrlKey || e.metaKey) && e.key === "f") {
|
|
8064
|
+
e.preventDefault();
|
|
8065
|
+
setFindReplaceMode("find");
|
|
8066
|
+
setFindReplaceOpen(true);
|
|
8067
|
+
return;
|
|
8068
|
+
}
|
|
8069
|
+
if ((e.ctrlKey || e.metaKey) && e.key === "h") {
|
|
8070
|
+
e.preventDefault();
|
|
8071
|
+
setFindReplaceMode("replace");
|
|
8072
|
+
setFindReplaceOpen(true);
|
|
8073
|
+
return;
|
|
8074
|
+
}
|
|
8075
|
+
if ((e.ctrlKey || e.metaKey) && e.key === "k") {
|
|
8076
|
+
e.preventDefault();
|
|
8077
|
+
setLinkPopupOpen(true);
|
|
8078
|
+
onOpenLinkPopup == null ? void 0 : onOpenLinkPopup();
|
|
8079
|
+
return;
|
|
8080
|
+
}
|
|
8081
|
+
if ((e.ctrlKey || e.metaKey) && (e.key === "a" || e.key === "A")) {
|
|
8082
|
+
e.preventDefault();
|
|
8083
|
+
const container = containerRef.current;
|
|
8084
|
+
if (container) {
|
|
8085
|
+
const spans = container.querySelectorAll("[data-path]");
|
|
8086
|
+
const first = spans[0];
|
|
8087
|
+
const last = spans[spans.length - 1];
|
|
8088
|
+
if (first && last) {
|
|
8089
|
+
const sel = window.getSelection();
|
|
8090
|
+
const range = document.createRange();
|
|
8091
|
+
range.setStart((_a = leafTextNode(first, "first")) != null ? _a : first, 0);
|
|
8092
|
+
const lastLeaf = leafTextNode(last, "last");
|
|
8093
|
+
range.setEnd(lastLeaf != null ? lastLeaf : last, (_c = (_b = lastLeaf == null ? void 0 : lastLeaf.textContent) == null ? void 0 : _b.length) != null ? _c : 0);
|
|
8094
|
+
sel == null ? void 0 : sel.removeAllRanges();
|
|
8095
|
+
sel == null ? void 0 : sel.addRange(range);
|
|
8096
|
+
}
|
|
8097
|
+
}
|
|
8098
|
+
return;
|
|
8099
|
+
}
|
|
8100
|
+
if (selectedImagePath && (e.key === "Delete" || e.key === "Backspace")) {
|
|
8101
|
+
e.preventDefault();
|
|
8102
|
+
deleteImageAtPath(selectedImagePath)(engine);
|
|
8103
|
+
setSelectedImagePath(null);
|
|
8104
|
+
return;
|
|
8105
|
+
}
|
|
8106
|
+
if (selectedImagePath) setSelectedImagePath(null);
|
|
8107
|
+
if (e.key === "Tab") {
|
|
8108
|
+
const { doc, selection: sel } = engine.getState();
|
|
8109
|
+
if (sel) {
|
|
8110
|
+
const bp = findContentBlockPath(doc, sel.anchor.path);
|
|
8111
|
+
const block = bp ? getNodeAtPath(doc, bp) : null;
|
|
8112
|
+
if ((block == null ? void 0 : block.type) === "code_block") {
|
|
8113
|
+
e.preventDefault();
|
|
8114
|
+
insertText(" ")(engine);
|
|
8115
|
+
return;
|
|
8116
|
+
}
|
|
8117
|
+
if ((block == null ? void 0 : block.type) === "list_item") {
|
|
8118
|
+
e.preventDefault();
|
|
8119
|
+
if (e.shiftKey) {
|
|
8120
|
+
outdentListItem(engine);
|
|
8121
|
+
} else {
|
|
8122
|
+
indentListItem(engine);
|
|
8123
|
+
}
|
|
8124
|
+
return;
|
|
8125
|
+
}
|
|
8126
|
+
if (findCellPosition(doc, sel.anchor.path)) {
|
|
8127
|
+
e.preventDefault();
|
|
8128
|
+
}
|
|
8129
|
+
}
|
|
8130
|
+
}
|
|
8131
|
+
const handled = engine.handleKeyDown(
|
|
8132
|
+
e.nativeEvent
|
|
8133
|
+
);
|
|
8134
|
+
if (handled) return;
|
|
8135
|
+
if (e.key === "Enter" && !e.shiftKey) {
|
|
8136
|
+
e.preventDefault();
|
|
8137
|
+
handleEnter(engine);
|
|
8138
|
+
return;
|
|
8139
|
+
}
|
|
8140
|
+
if (e.key === "Backspace") {
|
|
8141
|
+
e.preventDefault();
|
|
8142
|
+
handleBackspace(engine);
|
|
8143
|
+
return;
|
|
8144
|
+
}
|
|
8145
|
+
if (e.key === "Delete") {
|
|
8146
|
+
e.preventDefault();
|
|
8147
|
+
handleDelete(engine);
|
|
8148
|
+
return;
|
|
8149
|
+
}
|
|
8150
|
+
if (e.key === "Enter" && e.shiftKey) {
|
|
8151
|
+
e.preventDefault();
|
|
8152
|
+
insertText("\n")(engine);
|
|
8153
|
+
return;
|
|
8154
|
+
}
|
|
8155
|
+
},
|
|
8156
|
+
[engine, readOnly, onOpenLinkPopup, selectedImagePath]
|
|
8157
|
+
);
|
|
8158
|
+
useEffect(() => {
|
|
8159
|
+
const container = containerRef.current;
|
|
8160
|
+
if (!container || readOnly) return;
|
|
8161
|
+
const onBeforeInput = (e) => {
|
|
8162
|
+
if (e.isComposing || isComposingRef.current) return;
|
|
8163
|
+
switch (e.inputType) {
|
|
8164
|
+
case "insertText":
|
|
8165
|
+
case "insertReplacementText": {
|
|
8166
|
+
if (!e.data) return;
|
|
8167
|
+
e.preventDefault();
|
|
8168
|
+
insertText(e.data)(engine);
|
|
8169
|
+
return;
|
|
8170
|
+
}
|
|
8171
|
+
case "insertParagraph": {
|
|
8172
|
+
e.preventDefault();
|
|
8173
|
+
handleEnter(engine);
|
|
8174
|
+
return;
|
|
8175
|
+
}
|
|
8176
|
+
case "insertLineBreak": {
|
|
8177
|
+
e.preventDefault();
|
|
8178
|
+
insertText("\n")(engine);
|
|
8179
|
+
return;
|
|
8180
|
+
}
|
|
8181
|
+
case "deleteContentBackward":
|
|
8182
|
+
case "deleteWordBackward":
|
|
8183
|
+
case "deleteSoftLineBackward": {
|
|
8184
|
+
e.preventDefault();
|
|
8185
|
+
handleBackspace(engine);
|
|
8186
|
+
return;
|
|
8187
|
+
}
|
|
8188
|
+
case "insertFromPaste":
|
|
8189
|
+
case "insertFromDrop":
|
|
8190
|
+
return;
|
|
8191
|
+
default:
|
|
8192
|
+
e.preventDefault();
|
|
8193
|
+
}
|
|
8194
|
+
};
|
|
8195
|
+
container.addEventListener("beforeinput", onBeforeInput);
|
|
8196
|
+
return () => container.removeEventListener("beforeinput", onBeforeInput);
|
|
8197
|
+
}, [engine, readOnly]);
|
|
8198
|
+
useEffect(() => {
|
|
8199
|
+
const container = containerRef.current;
|
|
8200
|
+
if (!container || readOnly) return;
|
|
8201
|
+
const onCompositionStart = () => {
|
|
8202
|
+
isComposingRef.current = true;
|
|
8203
|
+
};
|
|
8204
|
+
const onCompositionEnd = (e) => {
|
|
8205
|
+
isComposingRef.current = false;
|
|
8206
|
+
if (e.data) insertText(e.data)(engine);
|
|
8207
|
+
};
|
|
8208
|
+
container.addEventListener("compositionstart", onCompositionStart);
|
|
8209
|
+
container.addEventListener("compositionend", onCompositionEnd);
|
|
8210
|
+
return () => {
|
|
8211
|
+
container.removeEventListener("compositionstart", onCompositionStart);
|
|
8212
|
+
container.removeEventListener("compositionend", onCompositionEnd);
|
|
8213
|
+
};
|
|
8214
|
+
}, [engine, readOnly]);
|
|
8215
|
+
useEffect(() => {
|
|
8216
|
+
const container = containerRef.current;
|
|
8217
|
+
if (!container) return;
|
|
8218
|
+
const onMouseOver = (e) => {
|
|
8219
|
+
var _a;
|
|
8220
|
+
const anchor = e.target.closest("a.editor-link");
|
|
8221
|
+
if (!anchor) return;
|
|
8222
|
+
const href = (_a = anchor.getAttribute("href")) != null ? _a : "";
|
|
8223
|
+
if (!href) return;
|
|
8224
|
+
if (linkTooltipTimerRef.current) clearTimeout(linkTooltipTimerRef.current);
|
|
8225
|
+
linkTooltipTimerRef.current = setTimeout(() => {
|
|
8226
|
+
const rect = anchor.getBoundingClientRect();
|
|
8227
|
+
const containerRect = container.getBoundingClientRect();
|
|
8228
|
+
setLinkTooltip({
|
|
8229
|
+
href,
|
|
8230
|
+
x: rect.left - containerRect.left,
|
|
8231
|
+
y: rect.bottom - containerRect.top + 6
|
|
8232
|
+
});
|
|
8233
|
+
}, 200);
|
|
8234
|
+
};
|
|
8235
|
+
const onMouseOut = (e) => {
|
|
8236
|
+
const anchor = e.target.closest("a.editor-link");
|
|
8237
|
+
if (!anchor) return;
|
|
8238
|
+
if (linkTooltipTimerRef.current) clearTimeout(linkTooltipTimerRef.current);
|
|
8239
|
+
setLinkTooltip(null);
|
|
8240
|
+
};
|
|
8241
|
+
const onClick = (e) => {
|
|
8242
|
+
if (!(e.ctrlKey || e.metaKey)) return;
|
|
8243
|
+
const anchor = e.target.closest("a.editor-link");
|
|
8244
|
+
if (!anchor) return;
|
|
8245
|
+
e.preventDefault();
|
|
8246
|
+
const href = anchor.getAttribute("href");
|
|
8247
|
+
if (href) window.open(href, "_blank", "noopener,noreferrer");
|
|
8248
|
+
};
|
|
8249
|
+
container.addEventListener("mouseover", onMouseOver);
|
|
8250
|
+
container.addEventListener("mouseout", onMouseOut);
|
|
8251
|
+
container.addEventListener("click", onClick);
|
|
8252
|
+
return () => {
|
|
8253
|
+
container.removeEventListener("mouseover", onMouseOver);
|
|
8254
|
+
container.removeEventListener("mouseout", onMouseOut);
|
|
8255
|
+
container.removeEventListener("click", onClick);
|
|
8256
|
+
if (linkTooltipTimerRef.current) clearTimeout(linkTooltipTimerRef.current);
|
|
8257
|
+
};
|
|
8258
|
+
}, []);
|
|
8259
|
+
useEffect(() => {
|
|
8260
|
+
const onMouseMove = (e) => {
|
|
8261
|
+
const drag = colResizeRef.current;
|
|
8262
|
+
if (!drag) return;
|
|
8263
|
+
const delta = e.clientX - drag.startX;
|
|
8264
|
+
const newWidth = Math.max(40, drag.startWidth + delta);
|
|
8265
|
+
const container = containerRef.current;
|
|
8266
|
+
if (container) {
|
|
8267
|
+
const colEl = container.querySelector(
|
|
8268
|
+
`col[data-col-index="${drag.colIndex}"]`
|
|
8269
|
+
);
|
|
8270
|
+
if (colEl) colEl.style.width = `${newWidth}px`;
|
|
8271
|
+
}
|
|
8272
|
+
};
|
|
8273
|
+
const onMouseUp = (e) => {
|
|
8274
|
+
const drag = colResizeRef.current;
|
|
8275
|
+
if (!drag) return;
|
|
8276
|
+
const delta = e.clientX - drag.startX;
|
|
8277
|
+
const newWidth = Math.max(40, drag.startWidth + delta);
|
|
8278
|
+
colResizeRef.current = null;
|
|
8279
|
+
setColumnWidth(drag.tablePath, drag.colIndex, newWidth)(engine);
|
|
8280
|
+
};
|
|
8281
|
+
document.addEventListener("mousemove", onMouseMove);
|
|
8282
|
+
document.addEventListener("mouseup", onMouseUp);
|
|
8283
|
+
return () => {
|
|
8284
|
+
document.removeEventListener("mousemove", onMouseMove);
|
|
8285
|
+
document.removeEventListener("mouseup", onMouseUp);
|
|
8286
|
+
};
|
|
8287
|
+
}, [engine]);
|
|
8288
|
+
useEffect(() => {
|
|
8289
|
+
const onMouseMove = (e) => {
|
|
8290
|
+
var _a, _b;
|
|
8291
|
+
const drag = cellDragRef.current;
|
|
8292
|
+
if (!drag) return;
|
|
8293
|
+
const td = e.target.closest("[data-cell-row]");
|
|
8294
|
+
if (!td) return;
|
|
8295
|
+
const row = parseInt((_a = td.dataset.cellRow) != null ? _a : "0");
|
|
8296
|
+
const col = parseInt((_b = td.dataset.cellCol) != null ? _b : "0");
|
|
8297
|
+
const tdTablePath = td.dataset.cellTablePath ? JSON.parse(td.dataset.cellTablePath) : null;
|
|
8298
|
+
if (!tdTablePath || JSON.stringify(tdTablePath) !== JSON.stringify(drag.tablePath)) return;
|
|
8299
|
+
setTableSelection({
|
|
8300
|
+
tablePath: drag.tablePath,
|
|
8301
|
+
anchorCell: [drag.startRow, drag.startCol],
|
|
8302
|
+
focusCell: [row, col]
|
|
8303
|
+
});
|
|
8304
|
+
};
|
|
8305
|
+
const onMouseUp = () => {
|
|
8306
|
+
cellDragRef.current = null;
|
|
8307
|
+
};
|
|
8308
|
+
document.addEventListener("mousemove", onMouseMove);
|
|
8309
|
+
document.addEventListener("mouseup", onMouseUp);
|
|
8310
|
+
return () => {
|
|
8311
|
+
document.removeEventListener("mousemove", onMouseMove);
|
|
8312
|
+
document.removeEventListener("mouseup", onMouseUp);
|
|
8313
|
+
};
|
|
8314
|
+
}, []);
|
|
8315
|
+
const handleContextMenu = useCallback(
|
|
8316
|
+
(e) => {
|
|
8317
|
+
var _a, _b, _c, _d, _e, _f;
|
|
8318
|
+
const td = e.target.closest("[data-cell-row]");
|
|
8319
|
+
if (!td) return;
|
|
8320
|
+
e.preventDefault();
|
|
8321
|
+
const row = parseInt((_a = td.dataset.cellRow) != null ? _a : "0");
|
|
8322
|
+
const col = parseInt((_b = td.dataset.cellCol) != null ? _b : "0");
|
|
8323
|
+
const tablePath = td.dataset.cellTablePath ? JSON.parse(td.dataset.cellTablePath) : null;
|
|
8324
|
+
if (!tablePath) return;
|
|
8325
|
+
const cellNode = getNodeAtPath(
|
|
8326
|
+
engine.getState().doc,
|
|
8327
|
+
[...tablePath, row, col]
|
|
8328
|
+
);
|
|
8329
|
+
const isMerged = !!cellNode && (((_d = (_c = cellNode.attrs) == null ? void 0 : _c.colspan) != null ? _d : 1) > 1 || ((_f = (_e = cellNode.attrs) == null ? void 0 : _e.rowspan) != null ? _f : 1) > 1);
|
|
8330
|
+
setTableContextMenu({ x: e.clientX, y: e.clientY, tablePath, row, col, isMerged });
|
|
8331
|
+
},
|
|
8332
|
+
[engine]
|
|
8333
|
+
);
|
|
8334
|
+
const handleMouseDown = useCallback(
|
|
8335
|
+
(e) => {
|
|
8336
|
+
var _a, _b, _c, _d;
|
|
8337
|
+
if (readOnly) return;
|
|
8338
|
+
const target = e.target;
|
|
8339
|
+
if (target.dataset.resizeImagePath) {
|
|
8340
|
+
e.preventDefault();
|
|
8341
|
+
const imagePath = JSON.parse(target.dataset.resizeImagePath);
|
|
8342
|
+
const corner = (_a = target.dataset.resizeImagePos) != null ? _a : "se";
|
|
8343
|
+
const container = containerRef.current;
|
|
8344
|
+
let startWidth = 300;
|
|
8345
|
+
if (container) {
|
|
8346
|
+
const fig2 = container.querySelector(`[data-image-path="${JSON.stringify(imagePath)}"]`);
|
|
8347
|
+
const img = fig2 == null ? void 0 : fig2.querySelector("img");
|
|
8348
|
+
if (img) startWidth = img.getBoundingClientRect().width || 300;
|
|
8349
|
+
}
|
|
8350
|
+
imageResizeRef.current = {
|
|
8351
|
+
imagePath,
|
|
8352
|
+
corner,
|
|
8353
|
+
startX: e.clientX,
|
|
8354
|
+
startY: e.clientY,
|
|
8355
|
+
startWidth,
|
|
8356
|
+
startHeight: 0
|
|
8357
|
+
};
|
|
8358
|
+
return;
|
|
8359
|
+
}
|
|
8360
|
+
const fig = target.closest("[data-image-path]");
|
|
8361
|
+
if (fig == null ? void 0 : fig.dataset.imagePath) {
|
|
8362
|
+
e.preventDefault();
|
|
8363
|
+
setSelectedImagePath(JSON.parse(fig.dataset.imagePath));
|
|
8364
|
+
return;
|
|
8365
|
+
}
|
|
8366
|
+
setSelectedImagePath(null);
|
|
8367
|
+
if (target.dataset.resizeTable) {
|
|
8368
|
+
e.preventDefault();
|
|
8369
|
+
const tablePath = JSON.parse(target.dataset.resizeTable);
|
|
8370
|
+
const colIndex = parseInt((_b = target.dataset.resizeCol) != null ? _b : "0");
|
|
8371
|
+
const container = containerRef.current;
|
|
8372
|
+
let startWidth = 120;
|
|
8373
|
+
if (container) {
|
|
8374
|
+
const colEl = container.querySelector(
|
|
8375
|
+
`col[data-col-index="${colIndex}"]`
|
|
8376
|
+
);
|
|
8377
|
+
if (colEl) startWidth = colEl.getBoundingClientRect().width || 120;
|
|
8378
|
+
}
|
|
8379
|
+
colResizeRef.current = { tablePath, colIndex, startX: e.clientX, startWidth };
|
|
8380
|
+
return;
|
|
8381
|
+
}
|
|
8382
|
+
const td = target.closest("[data-cell-row]");
|
|
8383
|
+
if (!td) setTableSelection(null);
|
|
8384
|
+
if (td && !readOnly) {
|
|
8385
|
+
const row = parseInt((_c = td.dataset.cellRow) != null ? _c : "0");
|
|
8386
|
+
const col = parseInt((_d = td.dataset.cellCol) != null ? _d : "0");
|
|
8387
|
+
const tdTablePath = td.dataset.cellTablePath ? JSON.parse(td.dataset.cellTablePath) : null;
|
|
8388
|
+
if (tdTablePath) {
|
|
8389
|
+
cellDragRef.current = { tablePath: tdTablePath, startRow: row, startCol: col };
|
|
8390
|
+
setTableSelection({
|
|
8391
|
+
tablePath: tdTablePath,
|
|
8392
|
+
anchorCell: [row, col],
|
|
8393
|
+
focusCell: [row, col]
|
|
8394
|
+
});
|
|
8395
|
+
}
|
|
8396
|
+
}
|
|
8397
|
+
const checkTarget = target.dataset.checkPath ? target : target.closest("[data-check-path]");
|
|
8398
|
+
if (checkTarget == null ? void 0 : checkTarget.dataset.checkPath) {
|
|
8399
|
+
e.preventDefault();
|
|
8400
|
+
toggleCheckItemAt(JSON.parse(checkTarget.dataset.checkPath))(engine);
|
|
8401
|
+
}
|
|
8402
|
+
},
|
|
8403
|
+
[engine, readOnly]
|
|
8404
|
+
);
|
|
8405
|
+
const handleClick = useCallback(() => {
|
|
8406
|
+
const container = containerRef.current;
|
|
8407
|
+
if (!container) return;
|
|
8408
|
+
const selection = captureSelection(container);
|
|
8409
|
+
if (!selection) return;
|
|
8410
|
+
skipRestoreSelectionRef.current = true;
|
|
8411
|
+
const tr = createTransaction();
|
|
8412
|
+
tr.steps.push(
|
|
8413
|
+
tr_setSelection(selection)
|
|
8414
|
+
);
|
|
8415
|
+
engine.dispatch(tr);
|
|
8416
|
+
}, [engine]);
|
|
8417
|
+
const imageResizeRef = useRef(null);
|
|
8418
|
+
useEffect(() => {
|
|
8419
|
+
const onMouseMove = (e) => {
|
|
8420
|
+
const drag = imageResizeRef.current;
|
|
8421
|
+
if (!drag) return;
|
|
8422
|
+
const deltaX = e.clientX - drag.startX;
|
|
8423
|
+
const isRight = drag.corner === "ne" || drag.corner === "se";
|
|
8424
|
+
const newWidth = Math.max(60, drag.startWidth + (isRight ? deltaX : -deltaX));
|
|
8425
|
+
const container = containerRef.current;
|
|
8426
|
+
if (container) {
|
|
8427
|
+
const fig = container.querySelector(
|
|
8428
|
+
`[data-image-path="${JSON.stringify(drag.imagePath)}"]`
|
|
8429
|
+
);
|
|
8430
|
+
const img = fig == null ? void 0 : fig.querySelector("img");
|
|
8431
|
+
if (img) img.style.width = `${newWidth}px`;
|
|
8432
|
+
}
|
|
8433
|
+
};
|
|
8434
|
+
const onMouseUp = (e) => {
|
|
8435
|
+
const drag = imageResizeRef.current;
|
|
8436
|
+
if (!drag) return;
|
|
8437
|
+
const deltaX = e.clientX - drag.startX;
|
|
8438
|
+
const isRight = drag.corner === "ne" || drag.corner === "se";
|
|
8439
|
+
const newWidth = Math.max(60, drag.startWidth + (isRight ? deltaX : -deltaX));
|
|
8440
|
+
imageResizeRef.current = null;
|
|
8441
|
+
setImageAttr(drag.imagePath, { width: newWidth })(engine);
|
|
8442
|
+
};
|
|
8443
|
+
document.addEventListener("mousemove", onMouseMove);
|
|
8444
|
+
document.addEventListener("mouseup", onMouseUp);
|
|
8445
|
+
return () => {
|
|
8446
|
+
document.removeEventListener("mousemove", onMouseMove);
|
|
8447
|
+
document.removeEventListener("mouseup", onMouseUp);
|
|
8448
|
+
};
|
|
8449
|
+
}, [engine]);
|
|
8450
|
+
useEffect(() => {
|
|
8451
|
+
const container = containerRef.current;
|
|
8452
|
+
if (!container) return;
|
|
8453
|
+
container.querySelectorAll("[data-image-path]").forEach((el) => {
|
|
8454
|
+
el.removeAttribute("data-selected");
|
|
8455
|
+
});
|
|
8456
|
+
if (selectedImagePath) {
|
|
8457
|
+
const fig = container.querySelector(
|
|
8458
|
+
`[data-image-path="${JSON.stringify(selectedImagePath)}"]`
|
|
8459
|
+
);
|
|
8460
|
+
if (fig) fig.setAttribute("data-selected", "true");
|
|
8461
|
+
}
|
|
8462
|
+
}, [selectedImagePath, state.doc]);
|
|
8463
|
+
useEffect(() => {
|
|
8464
|
+
const container = containerRef.current;
|
|
8465
|
+
if (!container || readOnly) return;
|
|
8466
|
+
const onDragOver = (e) => {
|
|
8467
|
+
var _a;
|
|
8468
|
+
if ((_a = e.dataTransfer) == null ? void 0 : _a.types.includes("Files")) e.preventDefault();
|
|
8469
|
+
};
|
|
8470
|
+
const onDrop = (e) => {
|
|
8471
|
+
var _a;
|
|
8472
|
+
e.preventDefault();
|
|
8473
|
+
const file = (_a = e.dataTransfer) == null ? void 0 : _a.files[0];
|
|
8474
|
+
if (!file || !file.type.startsWith("image/")) return;
|
|
8475
|
+
const blobUrl = URL.createObjectURL(file);
|
|
8476
|
+
insertImage(blobUrl, file.name.replace(/\.[^.]+$/, ""))(engine);
|
|
8477
|
+
};
|
|
8478
|
+
container.addEventListener("dragover", onDragOver);
|
|
8479
|
+
container.addEventListener("drop", onDrop);
|
|
8480
|
+
return () => {
|
|
8481
|
+
container.removeEventListener("dragover", onDragOver);
|
|
8482
|
+
container.removeEventListener("drop", onDrop);
|
|
8483
|
+
};
|
|
8484
|
+
}, [engine, readOnly]);
|
|
8485
|
+
const isVisualEmpty = state.doc.children.length === 1 && state.doc.children[0].type === "paragraph" && state.doc.children[0].children.length === 0;
|
|
8486
|
+
return /* @__PURE__ */ jsxs(
|
|
8487
|
+
"div",
|
|
8488
|
+
{
|
|
8489
|
+
ref: editorRootRef,
|
|
8490
|
+
className: `editor-root relative flex flex-col ${className}`,
|
|
8491
|
+
style: editorHeight ? { minHeight: editorHeight } : void 0,
|
|
8492
|
+
children: [
|
|
8493
|
+
!readOnly && /* @__PURE__ */ jsx(
|
|
8494
|
+
Toolbar,
|
|
8495
|
+
{
|
|
8496
|
+
engine,
|
|
8497
|
+
onFindReplace: (mode) => {
|
|
8498
|
+
setFindReplaceMode(mode);
|
|
8499
|
+
setFindReplaceOpen(true);
|
|
8500
|
+
},
|
|
8501
|
+
linkPopupOpen,
|
|
8502
|
+
onLinkPopupClose: () => setLinkPopupOpen(false),
|
|
8503
|
+
isSourceMode,
|
|
8504
|
+
onToggleSource: handleToggleSource
|
|
8505
|
+
}
|
|
8506
|
+
),
|
|
8507
|
+
/* @__PURE__ */ jsx("div", { className: "relative", children: isSourceMode ? /* @__PURE__ */ jsx(
|
|
8508
|
+
"textarea",
|
|
8509
|
+
{
|
|
8510
|
+
"aria-label": "HTML source editor",
|
|
8511
|
+
value: sourceHTML,
|
|
8512
|
+
onChange: (e) => {
|
|
8513
|
+
setSourceHTML(e.target.value);
|
|
8514
|
+
const t = e.target;
|
|
8515
|
+
t.style.height = "auto";
|
|
8516
|
+
t.style.height = t.scrollHeight + "px";
|
|
8517
|
+
},
|
|
8518
|
+
ref: (el) => {
|
|
8519
|
+
if (el) {
|
|
8520
|
+
el.style.height = "auto";
|
|
8521
|
+
el.style.height = el.scrollHeight + "px";
|
|
8522
|
+
}
|
|
8523
|
+
},
|
|
8524
|
+
spellCheck: false,
|
|
8525
|
+
className: "w-full block px-6 py-4 font-mono text-sm leading-relaxed bg-gray-950 text-green-300 outline-none resize-none border-0 overflow-hidden"
|
|
8526
|
+
}
|
|
8527
|
+
) : /* @__PURE__ */ jsxs(Fragment, { children: [
|
|
8528
|
+
isVisualEmpty && /* @__PURE__ */ jsx(
|
|
8529
|
+
"div",
|
|
8530
|
+
{
|
|
8531
|
+
className: "\r\n absolute\r\n top-6\r\n left-6\r\n pointer-events-none\r\n select-none\r\n text-base\r\n text-gray-400\r\n dark:text-gray-500\r\n ",
|
|
8532
|
+
"aria-hidden": "true",
|
|
8533
|
+
children: placeholder
|
|
8534
|
+
}
|
|
8535
|
+
),
|
|
8536
|
+
/* @__PURE__ */ jsx(
|
|
8537
|
+
"div",
|
|
8538
|
+
{
|
|
8539
|
+
ref: containerRef,
|
|
8540
|
+
contentEditable: !readOnly,
|
|
8541
|
+
suppressContentEditableWarning: true,
|
|
8542
|
+
role: "textbox",
|
|
8543
|
+
"aria-multiline": "true",
|
|
8544
|
+
"aria-label": "Rich text editor",
|
|
8545
|
+
spellCheck: true,
|
|
8546
|
+
onMouseDown: handleMouseDown,
|
|
8547
|
+
onContextMenu: handleContextMenu,
|
|
8548
|
+
onFocus: handleFocus,
|
|
8549
|
+
onClick: handleClick,
|
|
8550
|
+
onKeyDown: handleKeyDown,
|
|
8551
|
+
className: [
|
|
8552
|
+
"editor-canvas",
|
|
8553
|
+
"min-h-[300px]",
|
|
8554
|
+
"px-6",
|
|
8555
|
+
"py-4",
|
|
8556
|
+
"outline-none",
|
|
8557
|
+
"text-base",
|
|
8558
|
+
"leading-relaxed",
|
|
8559
|
+
"text-gray-900",
|
|
8560
|
+
"dark:text-gray-100",
|
|
8561
|
+
readOnly ? "cursor-default" : "cursor-text"
|
|
8562
|
+
].join(" ")
|
|
8563
|
+
}
|
|
8564
|
+
),
|
|
8565
|
+
linkTooltip && /* @__PURE__ */ jsxs(
|
|
8566
|
+
"div",
|
|
8567
|
+
{
|
|
8568
|
+
role: "tooltip",
|
|
8569
|
+
style: { left: linkTooltip.x, top: linkTooltip.y },
|
|
8570
|
+
className: "absolute z-50 pointer-events-none flex items-center gap-1.5 bg-gray-900 dark:bg-gray-700 text-white text-xs px-2.5 py-1.5 rounded shadow-lg max-w-xs",
|
|
8571
|
+
children: [
|
|
8572
|
+
/* @__PURE__ */ jsxs("svg", { width: "11", height: "11", viewBox: "0 0 16 16", fill: "none", className: "shrink-0 opacity-70", "aria-hidden": "true", children: [
|
|
8573
|
+
/* @__PURE__ */ jsx("path", { d: "M6.5 3.5H4A2.5 2.5 0 0 0 4 8.5h2", stroke: "currentColor", strokeWidth: "1.6", strokeLinecap: "round" }),
|
|
8574
|
+
/* @__PURE__ */ jsx("path", { d: "M9.5 3.5H12A2.5 2.5 0 0 1 12 8.5h-2", stroke: "currentColor", strokeWidth: "1.6", strokeLinecap: "round" }),
|
|
8575
|
+
/* @__PURE__ */ jsx("path", { d: "M5.5 6h5", stroke: "currentColor", strokeWidth: "1.6", strokeLinecap: "round" })
|
|
8576
|
+
] }),
|
|
8577
|
+
/* @__PURE__ */ jsx("span", { className: "truncate", children: linkTooltip.href }),
|
|
8578
|
+
/* @__PURE__ */ jsx("span", { className: "shrink-0 opacity-50 ml-1", children: "Ctrl+click to open" })
|
|
8579
|
+
]
|
|
8580
|
+
}
|
|
8581
|
+
)
|
|
8582
|
+
] }) }),
|
|
8583
|
+
inTableCellPos && !readOnly && /* @__PURE__ */ jsx(
|
|
8584
|
+
TableToolbar,
|
|
8585
|
+
{
|
|
8586
|
+
engine,
|
|
8587
|
+
tablePath: inTableCellPos.tablePath,
|
|
8588
|
+
cellPos: { row: inTableCellPos.row, col: inTableCellPos.col },
|
|
8589
|
+
tableSelection,
|
|
8590
|
+
editorContainer: containerRef
|
|
8591
|
+
}
|
|
8592
|
+
),
|
|
8593
|
+
tableContextMenu && /* @__PURE__ */ jsx(
|
|
8594
|
+
TableContextMenu,
|
|
8595
|
+
{
|
|
8596
|
+
x: tableContextMenu.x,
|
|
8597
|
+
y: tableContextMenu.y,
|
|
8598
|
+
tablePath: tableContextMenu.tablePath,
|
|
8599
|
+
row: tableContextMenu.row,
|
|
8600
|
+
col: tableContextMenu.col,
|
|
8601
|
+
isMerged: tableContextMenu.isMerged,
|
|
8602
|
+
tableSelection,
|
|
8603
|
+
engine,
|
|
8604
|
+
onClose: () => setTableContextMenu(null)
|
|
8605
|
+
}
|
|
8606
|
+
),
|
|
8607
|
+
findReplaceOpen && /* @__PURE__ */ jsx(
|
|
8608
|
+
FindReplaceModal,
|
|
8609
|
+
{
|
|
8610
|
+
engine,
|
|
8611
|
+
editorContainer: containerRef,
|
|
8612
|
+
initialMode: findReplaceMode,
|
|
8613
|
+
onClose: () => setFindReplaceOpen(false)
|
|
8614
|
+
}
|
|
8615
|
+
),
|
|
8616
|
+
selectedImagePath && !readOnly && /* @__PURE__ */ jsx(
|
|
8617
|
+
ImageToolbar,
|
|
8618
|
+
{
|
|
8619
|
+
engine,
|
|
8620
|
+
imagePath: selectedImagePath,
|
|
8621
|
+
editorContainer: containerRef,
|
|
8622
|
+
onClose: () => setSelectedImagePath(null)
|
|
8623
|
+
}
|
|
8624
|
+
),
|
|
8625
|
+
!readOnly && /* @__PURE__ */ jsx(
|
|
8626
|
+
"div",
|
|
8627
|
+
{
|
|
8628
|
+
title: "Drag to resize",
|
|
8629
|
+
onMouseDown: (e) => {
|
|
8630
|
+
e.preventDefault();
|
|
8631
|
+
const rootEl = editorRootRef.current;
|
|
8632
|
+
if (!rootEl) return;
|
|
8633
|
+
resizeDragRef.current = {
|
|
8634
|
+
startY: e.clientY,
|
|
8635
|
+
startHeight: rootEl.getBoundingClientRect().height
|
|
8636
|
+
};
|
|
8637
|
+
},
|
|
8638
|
+
className: "absolute bottom-0 right-0 w-4 h-4 cursor-s-resize flex items-end justify-end pr-0.5 pb-0.5 select-none",
|
|
8639
|
+
"aria-hidden": "true",
|
|
8640
|
+
children: /* @__PURE__ */ jsx("svg", { width: "8", height: "8", viewBox: "0 0 8 8", fill: "none", children: /* @__PURE__ */ jsx("path", { d: "M7 1L1 7M7 4L4 7M7 7L7 7", stroke: "#9ca3af", strokeWidth: "1.2", strokeLinecap: "round" }) })
|
|
8641
|
+
}
|
|
8642
|
+
)
|
|
8643
|
+
]
|
|
8644
|
+
}
|
|
8645
|
+
);
|
|
8646
|
+
}
|
|
8647
|
+
var BLOCK_TAGS = "p|h[1-6]|ul|ol|li|blockquote|pre|figure|table|thead|tbody|tr|th|td";
|
|
8648
|
+
var _OPEN_ONLY_RE = new RegExp(`^<(${BLOCK_TAGS})(?:\\s[^>]*)?>$`, "i");
|
|
8649
|
+
var _CLOSE_ONLY_RE = new RegExp(`^<\\/(${BLOCK_TAGS})>$`, "i");
|
|
8650
|
+
var _HR_RE = /^<hr(\s[^>]*)?\/?>$/i;
|
|
8651
|
+
function leafTextNode(el, end) {
|
|
8652
|
+
let node = el;
|
|
8653
|
+
while (node.hasChildNodes()) {
|
|
8654
|
+
node = end === "first" ? node.firstChild : node.lastChild;
|
|
8655
|
+
}
|
|
8656
|
+
return node.nodeType === Node.TEXT_NODE ? node : null;
|
|
8657
|
+
}
|
|
8658
|
+
function prettyPrintHTML(html) {
|
|
8659
|
+
const blockOpen = new RegExp(`(<(?:${BLOCK_TAGS})(?:\\s[^>]*)?>)`, "gi");
|
|
8660
|
+
const blockClose = new RegExp(`(<\\/(?:${BLOCK_TAGS})>)`, "gi");
|
|
8661
|
+
const spread = html.replace(blockOpen, "\n$1").replace(blockClose, "$1\n").replace(/<hr(\s[^>]*)?\/?>(\s*)/gi, "\n<hr />\n");
|
|
8662
|
+
let depth = 0;
|
|
8663
|
+
const result = [];
|
|
8664
|
+
for (const raw of spread.split("\n")) {
|
|
8665
|
+
const line = raw.trim();
|
|
8666
|
+
if (!line) continue;
|
|
8667
|
+
if (_HR_RE.test(line)) {
|
|
8668
|
+
result.push(" ".repeat(depth) + line);
|
|
8669
|
+
continue;
|
|
8670
|
+
}
|
|
8671
|
+
if (_CLOSE_ONLY_RE.test(line)) {
|
|
8672
|
+
depth = Math.max(0, depth - 1);
|
|
8673
|
+
result.push(" ".repeat(depth) + line);
|
|
8674
|
+
continue;
|
|
8675
|
+
}
|
|
8676
|
+
if (_OPEN_ONLY_RE.test(line)) {
|
|
8677
|
+
result.push(" ".repeat(depth) + line);
|
|
8678
|
+
depth++;
|
|
8679
|
+
continue;
|
|
8680
|
+
}
|
|
8681
|
+
result.push(" ".repeat(depth) + line);
|
|
8682
|
+
}
|
|
8683
|
+
return result.join("\n");
|
|
8684
|
+
}
|
|
7834
8685
|
function EditorCore({
|
|
7835
8686
|
engine,
|
|
7836
8687
|
placeholder = "Start writing...",
|
|
@@ -7900,7 +8751,7 @@ function EditorCore({
|
|
|
7900
8751
|
const handleToggleSource = useCallback(() => {
|
|
7901
8752
|
if (!isSourceMode) {
|
|
7902
8753
|
const html = htmlSerializer.serialize(engine.getState().doc);
|
|
7903
|
-
setSourceHTML(
|
|
8754
|
+
setSourceHTML(prettyPrintHTML2(html));
|
|
7904
8755
|
setIsSourceMode(true);
|
|
7905
8756
|
} else {
|
|
7906
8757
|
const newDoc = htmlSerializer.deserialize(sourceHTML);
|
|
@@ -8088,8 +8939,8 @@ function EditorCore({
|
|
|
8088
8939
|
if (first && last) {
|
|
8089
8940
|
const sel = window.getSelection();
|
|
8090
8941
|
const range = document.createRange();
|
|
8091
|
-
range.setStart((_a =
|
|
8092
|
-
const lastLeaf =
|
|
8942
|
+
range.setStart((_a = leafTextNode2(first, "first")) != null ? _a : first, 0);
|
|
8943
|
+
const lastLeaf = leafTextNode2(last, "last");
|
|
8093
8944
|
range.setEnd(lastLeaf != null ? lastLeaf : last, (_c = (_b = lastLeaf == null ? void 0 : lastLeaf.textContent) == null ? void 0 : _b.length) != null ? _c : 0);
|
|
8094
8945
|
sel == null ? void 0 : sel.removeAllRanges();
|
|
8095
8946
|
sel == null ? void 0 : sel.addRange(range);
|
|
@@ -8645,36 +9496,36 @@ function EditorCore({
|
|
|
8645
9496
|
}
|
|
8646
9497
|
);
|
|
8647
9498
|
}
|
|
8648
|
-
var
|
|
8649
|
-
var
|
|
8650
|
-
var
|
|
8651
|
-
var
|
|
8652
|
-
function
|
|
9499
|
+
var BLOCK_TAGS2 = "p|h[1-6]|ul|ol|li|blockquote|pre|figure|table|thead|tbody|tr|th|td";
|
|
9500
|
+
var _OPEN_ONLY_RE2 = new RegExp(`^<(${BLOCK_TAGS2})(?:\\s[^>]*)?>$`, "i");
|
|
9501
|
+
var _CLOSE_ONLY_RE2 = new RegExp(`^<\\/(${BLOCK_TAGS2})>$`, "i");
|
|
9502
|
+
var _HR_RE2 = /^<hr(\s[^>]*)?\/?>$/i;
|
|
9503
|
+
function leafTextNode2(el, end) {
|
|
8653
9504
|
let node = el;
|
|
8654
9505
|
while (node.hasChildNodes()) {
|
|
8655
9506
|
node = end === "first" ? node.firstChild : node.lastChild;
|
|
8656
9507
|
}
|
|
8657
9508
|
return node.nodeType === Node.TEXT_NODE ? node : null;
|
|
8658
9509
|
}
|
|
8659
|
-
function
|
|
8660
|
-
const blockOpen = new RegExp(`(<(?:${
|
|
8661
|
-
const blockClose = new RegExp(`(<\\/(?:${
|
|
9510
|
+
function prettyPrintHTML2(html) {
|
|
9511
|
+
const blockOpen = new RegExp(`(<(?:${BLOCK_TAGS2})(?:\\s[^>]*)?>)`, "gi");
|
|
9512
|
+
const blockClose = new RegExp(`(<\\/(?:${BLOCK_TAGS2})>)`, "gi");
|
|
8662
9513
|
const spread = html.replace(blockOpen, "\n$1").replace(blockClose, "$1\n").replace(/<hr(\s[^>]*)?\/?>(\s*)/gi, "\n<hr />\n");
|
|
8663
9514
|
let depth = 0;
|
|
8664
9515
|
const result = [];
|
|
8665
9516
|
for (const raw of spread.split("\n")) {
|
|
8666
9517
|
const line = raw.trim();
|
|
8667
9518
|
if (!line) continue;
|
|
8668
|
-
if (
|
|
9519
|
+
if (_HR_RE2.test(line)) {
|
|
8669
9520
|
result.push(" ".repeat(depth) + line);
|
|
8670
9521
|
continue;
|
|
8671
9522
|
}
|
|
8672
|
-
if (
|
|
9523
|
+
if (_CLOSE_ONLY_RE2.test(line)) {
|
|
8673
9524
|
depth = Math.max(0, depth - 1);
|
|
8674
9525
|
result.push(" ".repeat(depth) + line);
|
|
8675
9526
|
continue;
|
|
8676
9527
|
}
|
|
8677
|
-
if (
|
|
9528
|
+
if (_OPEN_ONLY_RE2.test(line)) {
|
|
8678
9529
|
result.push(" ".repeat(depth) + line);
|
|
8679
9530
|
depth++;
|
|
8680
9531
|
continue;
|
|
@@ -9146,151 +9997,6 @@ function execCommand(engine, command, ...args) {
|
|
|
9146
9997
|
function registerCommand(name, fn) {
|
|
9147
9998
|
REGISTRY[name] = fn;
|
|
9148
9999
|
}
|
|
9149
|
-
var Editor = forwardRef(function Editor2(props, ref) {
|
|
9150
|
-
var _a;
|
|
9151
|
-
const {
|
|
9152
|
-
value,
|
|
9153
|
-
defaultValue,
|
|
9154
|
-
onChange,
|
|
9155
|
-
placeholder,
|
|
9156
|
-
readOnly = false,
|
|
9157
|
-
toolbar = DEFAULT_TOOLBAR,
|
|
9158
|
-
theme = "light",
|
|
9159
|
-
plugins,
|
|
9160
|
-
onFocus,
|
|
9161
|
-
onBlur,
|
|
9162
|
-
onReady,
|
|
9163
|
-
onUploadImage,
|
|
9164
|
-
className,
|
|
9165
|
-
style,
|
|
9166
|
-
minHeight,
|
|
9167
|
-
maxHeight
|
|
9168
|
-
} = props;
|
|
9169
|
-
const engine = useEditorEngine();
|
|
9170
|
-
const pluginsRegisteredRef = useRef(false);
|
|
9171
|
-
if (!pluginsRegisteredRef.current && (plugins == null ? void 0 : plugins.length)) {
|
|
9172
|
-
pluginsRegisteredRef.current = true;
|
|
9173
|
-
for (const p of plugins) {
|
|
9174
|
-
engine.registerPlugin(p);
|
|
9175
|
-
}
|
|
9176
|
-
}
|
|
9177
|
-
const initializedRef = useRef(false);
|
|
9178
|
-
useEffect(() => {
|
|
9179
|
-
if (initializedRef.current) return;
|
|
9180
|
-
initializedRef.current = true;
|
|
9181
|
-
const initHTML = value != null ? value : defaultValue;
|
|
9182
|
-
if (!initHTML) return;
|
|
9183
|
-
const doc = htmlSerializer.deserialize(initHTML);
|
|
9184
|
-
const tr = createTransaction();
|
|
9185
|
-
tr.steps.push(tr_replaceDoc(doc));
|
|
9186
|
-
engine.dispatch(tr);
|
|
9187
|
-
}, []);
|
|
9188
|
-
const lastEmittedRef = useRef("");
|
|
9189
|
-
useEffect(() => {
|
|
9190
|
-
if (value === void 0) return;
|
|
9191
|
-
if (value === lastEmittedRef.current) return;
|
|
9192
|
-
const currentHTML = htmlSerializer.serialize(engine.getState().doc);
|
|
9193
|
-
if (value === currentHTML) return;
|
|
9194
|
-
const doc = htmlSerializer.deserialize(value);
|
|
9195
|
-
const tr = createTransaction();
|
|
9196
|
-
tr.steps.push(tr_replaceDoc(doc));
|
|
9197
|
-
engine.dispatch(tr);
|
|
9198
|
-
}, [value, engine]);
|
|
9199
|
-
const handleHTMLChange = useCallback((html) => {
|
|
9200
|
-
lastEmittedRef.current = html;
|
|
9201
|
-
onChange == null ? void 0 : onChange(html);
|
|
9202
|
-
}, [onChange]);
|
|
9203
|
-
const rootRef = useRef(null);
|
|
9204
|
-
const api = useMemo(() => ({
|
|
9205
|
-
getHTML: () => htmlSerializer.serialize(engine.getState().doc),
|
|
9206
|
-
setHTML: (html) => {
|
|
9207
|
-
const doc = htmlSerializer.deserialize(html);
|
|
9208
|
-
const tr = createTransaction();
|
|
9209
|
-
tr.steps.push(tr_replaceDoc(doc));
|
|
9210
|
-
engine.dispatch(tr);
|
|
9211
|
-
},
|
|
9212
|
-
getJSON: () => engine.getState().doc,
|
|
9213
|
-
getMarkdown: () => markdownSerializer.serialize(engine.getState().doc),
|
|
9214
|
-
focus: () => {
|
|
9215
|
-
var _a2;
|
|
9216
|
-
const ce = (_a2 = rootRef.current) == null ? void 0 : _a2.querySelector("[contenteditable]");
|
|
9217
|
-
ce == null ? void 0 : ce.focus();
|
|
9218
|
-
},
|
|
9219
|
-
blur: () => {
|
|
9220
|
-
var _a2;
|
|
9221
|
-
const ce = (_a2 = rootRef.current) == null ? void 0 : _a2.querySelector("[contenteditable]");
|
|
9222
|
-
ce == null ? void 0 : ce.blur();
|
|
9223
|
-
},
|
|
9224
|
-
clear: () => {
|
|
9225
|
-
const tr = createTransaction();
|
|
9226
|
-
tr.steps.push(tr_replaceDoc(createEmptyDocument()));
|
|
9227
|
-
engine.dispatch(tr);
|
|
9228
|
-
},
|
|
9229
|
-
undo: () => {
|
|
9230
|
-
engine.handleKeyDown(
|
|
9231
|
-
new KeyboardEvent("keydown", { key: "z", ctrlKey: true, bubbles: true })
|
|
9232
|
-
);
|
|
9233
|
-
},
|
|
9234
|
-
redo: () => {
|
|
9235
|
-
engine.handleKeyDown(
|
|
9236
|
-
new KeyboardEvent("keydown", { key: "y", ctrlKey: true, bubbles: true })
|
|
9237
|
-
);
|
|
9238
|
-
},
|
|
9239
|
-
execCommand: (command, ...args) => {
|
|
9240
|
-
return execCommand(engine, command, ...args);
|
|
9241
|
-
},
|
|
9242
|
-
registerCommand: (name, fn) => {
|
|
9243
|
-
registerCommand(name, fn);
|
|
9244
|
-
},
|
|
9245
|
-
getEngine: () => engine
|
|
9246
|
-
}), [engine]);
|
|
9247
|
-
useImperativeHandle(ref, () => api, [api]);
|
|
9248
|
-
useEffect(() => {
|
|
9249
|
-
onReady == null ? void 0 : onReady(api);
|
|
9250
|
-
}, []);
|
|
9251
|
-
const themeMode = typeof theme === "string" ? theme : (_a = theme.mode) != null ? _a : "light";
|
|
9252
|
-
const themeTokenOverrides = typeof theme === "object" && theme.tokens ? theme.tokens : void 0;
|
|
9253
|
-
const rootStyle = useMemo(() => {
|
|
9254
|
-
const base = { ...style };
|
|
9255
|
-
if (minHeight !== void 0)
|
|
9256
|
-
base.minHeight = typeof minHeight === "number" ? `${minHeight}px` : minHeight;
|
|
9257
|
-
if (maxHeight !== void 0) {
|
|
9258
|
-
base.maxHeight = typeof maxHeight === "number" ? `${maxHeight}px` : maxHeight;
|
|
9259
|
-
base.overflow = "auto";
|
|
9260
|
-
}
|
|
9261
|
-
if (themeTokenOverrides) {
|
|
9262
|
-
for (const [key, val] of Object.entries(themeTokenOverrides)) {
|
|
9263
|
-
const cssVar = `--editor-${key.replace(/([A-Z])/g, (m) => `-${m.toLowerCase()}`)}`;
|
|
9264
|
-
base[cssVar] = val;
|
|
9265
|
-
}
|
|
9266
|
-
}
|
|
9267
|
-
return base;
|
|
9268
|
-
}, [style, minHeight, maxHeight, themeTokenOverrides]);
|
|
9269
|
-
return /* @__PURE__ */ jsx(
|
|
9270
|
-
"div",
|
|
9271
|
-
{
|
|
9272
|
-
ref: rootRef,
|
|
9273
|
-
"data-editor-theme": themeMode,
|
|
9274
|
-
"data-traffica-editor": "",
|
|
9275
|
-
className,
|
|
9276
|
-
style: rootStyle,
|
|
9277
|
-
children: /* @__PURE__ */ jsx(
|
|
9278
|
-
EditorCore,
|
|
9279
|
-
{
|
|
9280
|
-
engine,
|
|
9281
|
-
placeholder,
|
|
9282
|
-
readOnly,
|
|
9283
|
-
hideToolbar: toolbar === false,
|
|
9284
|
-
toolbarConfig: toolbar !== false ? toolbar : void 0,
|
|
9285
|
-
onHTMLChange: handleHTMLChange,
|
|
9286
|
-
onFocus,
|
|
9287
|
-
onBlur,
|
|
9288
|
-
onUploadImage
|
|
9289
|
-
}
|
|
9290
|
-
)
|
|
9291
|
-
}
|
|
9292
|
-
);
|
|
9293
|
-
});
|
|
9294
10000
|
|
|
9295
10001
|
export { BASIC_TOOLBAR, DEFAULT_TOOLBAR, Editor, EditorCore, EditorEngine, MINIMAL_TOOLBAR, TablePlugin, Toolbar, addColumnLeft, addColumnRight, addMarkToNode, addRowAbove, addRowBelow, applyMarkToRange, applySearchHighlights, applyTransaction, attachClipboardHandlers, attachPasteHandler, captureSelection, clearSearchHighlights, collectText, comparePaths, comparePositions, createEmptyDocument, createHistoryManager, createHistoryPlugin, createParagraph, createPastePlugin, createTableCell, createTableNode, createTransaction, deleteColumn, deleteImageAtPath, deleteRange, deleteRow, deleteTable, deleteTableColumn, deleteTableRow, deleteTextAtPath, execCommand, findCellPosition, findContentBlockPath, findMatches, findTablePath, getActiveAlignment, getActiveBlockType, getActiveFontFamily, getActiveFontSize, getActiveHighlightColor, getActiveLinkHref, getActiveLinkRange, getActiveMarks, getActiveTextColor, getCellFirstPosition, getCellLastPosition, getDocumentLength, getDocumentMarkAttrValues, getNodeAtPath, getTableDimensions, getTextNodesBetween, htmlSerializer, insertImage, insertLink, insertTable, insertTableColumnAfter, insertTableColumnBefore, insertTableRowAfter, insertTableRowBefore, insertText, insertTextAtPath, isBlockNode, isContainerBlock, isSelectionInContainer, isTextNode, joinBlocks, jsonSerializer, makeCollapsedSelection, makePosition, markdownSerializer, marksEqual, mergeAdjacentTextNodesInDoc, mergeCells, mergeTableCells, normalizeRange, redo, registerCommand, removeMarkFromNode, removeMarkFromRange, renderDocument, replaceAllMatches, replaceMatch, restoreSelection, scrollMatchIntoView, setAlignment, setBlockType2 as setBlockType, setCodeBlockLanguage, setColumnWidth, setFontFamily, setFontSize, setHighlightColor, setImageAttr, setMarkOnRange, setTextColor, splitBlock, splitCell, splitTableCell, toggleCheckItemAt, toggleHeaderRow, toggleMark, undo, updateColumnWidth, useEditorEngine, useEditorState, walkDocument };
|
|
9296
10002
|
//# sourceMappingURL=index.mjs.map
|