@trafica/editor 1.0.53 → 1.0.55

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
@@ -896,6 +896,21 @@ function renderDocument(doc, container) {
896
896
  container.appendChild(blockEl);
897
897
  }
898
898
  }
899
+ function patchDocument(prevDoc, nextDoc, container) {
900
+ const prev = prevDoc.children;
901
+ const next = nextDoc.children;
902
+ if (prev.length !== next.length) {
903
+ renderDocument(nextDoc, container);
904
+ return;
905
+ }
906
+ const domChildren = container.children;
907
+ for (let i = 0; i < next.length; i++) {
908
+ if (next[i] !== prev[i]) {
909
+ const newEl = renderBlock(next[i], [i]);
910
+ container.replaceChild(newEl, domChildren[i]);
911
+ }
912
+ }
913
+ }
899
914
  function renderBlock(node, path) {
900
915
  switch (node.type) {
901
916
  case "paragraph":
@@ -7739,7 +7754,6 @@ function ImageToolbar({ engine, imagePath, editorContainer, onClose }) {
7739
7754
  const top = Math.max(4, rect.top - toolbarH - 6);
7740
7755
  toolbar.style.top = `${top}px`;
7741
7756
  toolbar.style.left = `${rect.left}px`;
7742
- toolbar.style.minWidth = `${rect.width}px`;
7743
7757
  toolbar.style.visibility = "visible";
7744
7758
  });
7745
7759
  const run = (attrs) => {
@@ -8010,6 +8024,9 @@ function EditorCore({
8010
8024
  }) {
8011
8025
  const containerRef = react.useRef(null);
8012
8026
  const isRenderingRef = react.useRef(false);
8027
+ const prevDocRef = react.useRef(null);
8028
+ const htmlDebounceRef = react.useRef(null);
8029
+ const jsonDebounceRef = react.useRef(null);
8013
8030
  const skipRestoreSelectionRef = react.useRef(false);
8014
8031
  const scrollCaretIntoView = react.useCallback(() => {
8015
8032
  var _a, _b;
@@ -8105,7 +8122,13 @@ function EditorCore({
8105
8122
  const container = containerRef.current;
8106
8123
  if (!container) return;
8107
8124
  isRenderingRef.current = true;
8108
- renderDocument(state.doc, container);
8125
+ const prevDoc = prevDocRef.current;
8126
+ if (prevDoc) {
8127
+ patchDocument(prevDoc, state.doc, container);
8128
+ } else {
8129
+ renderDocument(state.doc, container);
8130
+ }
8131
+ prevDocRef.current = state.doc;
8109
8132
  if (!container.contains(document.activeElement)) {
8110
8133
  container.focus({ preventScroll: true });
8111
8134
  }
@@ -8115,10 +8138,16 @@ function EditorCore({
8115
8138
  }
8116
8139
  isRenderingRef.current = false;
8117
8140
  if (onHTMLChange) {
8118
- onHTMLChange(htmlSerializer.serialize(state.doc));
8141
+ if (htmlDebounceRef.current) clearTimeout(htmlDebounceRef.current);
8142
+ htmlDebounceRef.current = setTimeout(() => {
8143
+ onHTMLChange(htmlSerializer.serialize(engine.getState().doc));
8144
+ }, 300);
8119
8145
  }
8120
8146
  if (onJSONChange) {
8121
- onJSONChange(jsonSerializer.serialize(state.doc));
8147
+ if (jsonDebounceRef.current) clearTimeout(jsonDebounceRef.current);
8148
+ jsonDebounceRef.current = setTimeout(() => {
8149
+ onJSONChange(jsonSerializer.serialize(engine.getState().doc));
8150
+ }, 300);
8122
8151
  }
8123
8152
  }, [state.doc]);
8124
8153
  react.useLayoutEffect(() => {
@@ -8193,7 +8222,7 @@ function EditorCore({
8193
8222
  const captured = captureSelection(container);
8194
8223
  if (!captured) return;
8195
8224
  const currentSelection = engine.getState().selection;
8196
- if (currentSelection && JSON.stringify(currentSelection.anchor) === JSON.stringify(captured.anchor) && JSON.stringify(currentSelection.focus) === JSON.stringify(captured.focus)) {
8225
+ if (currentSelection && selectionEqual(currentSelection.anchor, captured.anchor) && selectionEqual(currentSelection.focus, captured.focus)) {
8197
8226
  return;
8198
8227
  }
8199
8228
  skipRestoreSelectionRef.current = true;
@@ -8900,6 +8929,14 @@ var BLOCK_TAGS = "p|h[1-6]|ul|ol|li|blockquote|pre|figure|table|thead|tbody|tr|t
8900
8929
  var _OPEN_ONLY_RE = new RegExp(`^<(${BLOCK_TAGS})(?:\\s[^>]*)?>$`, "i");
8901
8930
  var _CLOSE_ONLY_RE = new RegExp(`^<\\/(${BLOCK_TAGS})>$`, "i");
8902
8931
  var _HR_RE = /^<hr(\s[^>]*)?\/?>$/i;
8932
+ function selectionEqual(a, b) {
8933
+ if (a.offset !== b.offset) return false;
8934
+ if (a.path.length !== b.path.length) return false;
8935
+ for (let i = 0; i < a.path.length; i++) {
8936
+ if (a.path[i] !== b.path[i]) return false;
8937
+ }
8938
+ return true;
8939
+ }
8903
8940
  function leafTextNode(el, end) {
8904
8941
  let node = el;
8905
8942
  while (node.hasChildNodes()) {