@trafica/editor 1.0.54 → 1.0.56

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,30 @@ 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
+ if (container.children.length !== next.length) {
907
+ renderDocument(nextDoc, container);
908
+ return;
909
+ }
910
+ const domChildren = container.children;
911
+ for (let i = 0; i < next.length; i++) {
912
+ if (next[i] !== prev[i]) {
913
+ const oldEl = domChildren[i];
914
+ if (!oldEl) {
915
+ renderDocument(nextDoc, container);
916
+ return;
917
+ }
918
+ const newEl = renderBlock(next[i], [i]);
919
+ container.replaceChild(newEl, oldEl);
920
+ }
921
+ }
922
+ }
899
923
  function renderBlock(node, path) {
900
924
  switch (node.type) {
901
925
  case "paragraph":
@@ -8009,6 +8033,9 @@ function EditorCore({
8009
8033
  }) {
8010
8034
  const containerRef = react.useRef(null);
8011
8035
  const isRenderingRef = react.useRef(false);
8036
+ const prevDocRef = react.useRef(null);
8037
+ const htmlDebounceRef = react.useRef(null);
8038
+ const jsonDebounceRef = react.useRef(null);
8012
8039
  const skipRestoreSelectionRef = react.useRef(false);
8013
8040
  const scrollCaretIntoView = react.useCallback(() => {
8014
8041
  var _a, _b;
@@ -8104,7 +8131,13 @@ function EditorCore({
8104
8131
  const container = containerRef.current;
8105
8132
  if (!container) return;
8106
8133
  isRenderingRef.current = true;
8107
- renderDocument(state.doc, container);
8134
+ const prevDoc = prevDocRef.current;
8135
+ if (prevDoc) {
8136
+ patchDocument(prevDoc, state.doc, container);
8137
+ } else {
8138
+ renderDocument(state.doc, container);
8139
+ }
8140
+ prevDocRef.current = state.doc;
8108
8141
  if (!container.contains(document.activeElement)) {
8109
8142
  container.focus({ preventScroll: true });
8110
8143
  }
@@ -8114,10 +8147,16 @@ function EditorCore({
8114
8147
  }
8115
8148
  isRenderingRef.current = false;
8116
8149
  if (onHTMLChange) {
8117
- onHTMLChange(htmlSerializer.serialize(state.doc));
8150
+ if (htmlDebounceRef.current) clearTimeout(htmlDebounceRef.current);
8151
+ htmlDebounceRef.current = setTimeout(() => {
8152
+ onHTMLChange(htmlSerializer.serialize(engine.getState().doc));
8153
+ }, 300);
8118
8154
  }
8119
8155
  if (onJSONChange) {
8120
- onJSONChange(jsonSerializer.serialize(state.doc));
8156
+ if (jsonDebounceRef.current) clearTimeout(jsonDebounceRef.current);
8157
+ jsonDebounceRef.current = setTimeout(() => {
8158
+ onJSONChange(jsonSerializer.serialize(engine.getState().doc));
8159
+ }, 300);
8121
8160
  }
8122
8161
  }, [state.doc]);
8123
8162
  react.useLayoutEffect(() => {
@@ -8192,7 +8231,7 @@ function EditorCore({
8192
8231
  const captured = captureSelection(container);
8193
8232
  if (!captured) return;
8194
8233
  const currentSelection = engine.getState().selection;
8195
- if (currentSelection && JSON.stringify(currentSelection.anchor) === JSON.stringify(captured.anchor) && JSON.stringify(currentSelection.focus) === JSON.stringify(captured.focus)) {
8234
+ if (currentSelection && selectionEqual(currentSelection.anchor, captured.anchor) && selectionEqual(currentSelection.focus, captured.focus)) {
8196
8235
  return;
8197
8236
  }
8198
8237
  skipRestoreSelectionRef.current = true;
@@ -8899,6 +8938,14 @@ var BLOCK_TAGS = "p|h[1-6]|ul|ol|li|blockquote|pre|figure|table|thead|tbody|tr|t
8899
8938
  var _OPEN_ONLY_RE = new RegExp(`^<(${BLOCK_TAGS})(?:\\s[^>]*)?>$`, "i");
8900
8939
  var _CLOSE_ONLY_RE = new RegExp(`^<\\/(${BLOCK_TAGS})>$`, "i");
8901
8940
  var _HR_RE = /^<hr(\s[^>]*)?\/?>$/i;
8941
+ function selectionEqual(a, b) {
8942
+ if (a.offset !== b.offset) return false;
8943
+ if (a.path.length !== b.path.length) return false;
8944
+ for (let i = 0; i < a.path.length; i++) {
8945
+ if (a.path[i] !== b.path[i]) return false;
8946
+ }
8947
+ return true;
8948
+ }
8902
8949
  function leafTextNode(el, end) {
8903
8950
  let node = el;
8904
8951
  while (node.hasChildNodes()) {