@trafica/editor 1.0.54 → 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":
@@ -8009,6 +8024,9 @@ function EditorCore({
8009
8024
  }) {
8010
8025
  const containerRef = react.useRef(null);
8011
8026
  const isRenderingRef = react.useRef(false);
8027
+ const prevDocRef = react.useRef(null);
8028
+ const htmlDebounceRef = react.useRef(null);
8029
+ const jsonDebounceRef = react.useRef(null);
8012
8030
  const skipRestoreSelectionRef = react.useRef(false);
8013
8031
  const scrollCaretIntoView = react.useCallback(() => {
8014
8032
  var _a, _b;
@@ -8104,7 +8122,13 @@ function EditorCore({
8104
8122
  const container = containerRef.current;
8105
8123
  if (!container) return;
8106
8124
  isRenderingRef.current = true;
8107
- 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;
8108
8132
  if (!container.contains(document.activeElement)) {
8109
8133
  container.focus({ preventScroll: true });
8110
8134
  }
@@ -8114,10 +8138,16 @@ function EditorCore({
8114
8138
  }
8115
8139
  isRenderingRef.current = false;
8116
8140
  if (onHTMLChange) {
8117
- 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);
8118
8145
  }
8119
8146
  if (onJSONChange) {
8120
- 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);
8121
8151
  }
8122
8152
  }, [state.doc]);
8123
8153
  react.useLayoutEffect(() => {
@@ -8192,7 +8222,7 @@ function EditorCore({
8192
8222
  const captured = captureSelection(container);
8193
8223
  if (!captured) return;
8194
8224
  const currentSelection = engine.getState().selection;
8195
- 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)) {
8196
8226
  return;
8197
8227
  }
8198
8228
  skipRestoreSelectionRef.current = true;
@@ -8899,6 +8929,14 @@ var BLOCK_TAGS = "p|h[1-6]|ul|ol|li|blockquote|pre|figure|table|thead|tbody|tr|t
8899
8929
  var _OPEN_ONLY_RE = new RegExp(`^<(${BLOCK_TAGS})(?:\\s[^>]*)?>$`, "i");
8900
8930
  var _CLOSE_ONLY_RE = new RegExp(`^<\\/(${BLOCK_TAGS})>$`, "i");
8901
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
+ }
8902
8940
  function leafTextNode(el, end) {
8903
8941
  let node = el;
8904
8942
  while (node.hasChildNodes()) {