@cyberyozh/text-editor 0.2.2 → 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/editor.js CHANGED
@@ -38,7 +38,7 @@ module.exports = __toCommonJS(editor_exports);
38
38
  // src/editor/editor.tsx
39
39
  var import_react37 = require("react");
40
40
  var import_state7 = require("@tiptap/pm/state");
41
- var import_model2 = require("@tiptap/pm/model");
41
+ var import_model3 = require("@tiptap/pm/model");
42
42
  var import_react38 = require("@tiptap/react");
43
43
  var import_menus = require("@tiptap/react/menus");
44
44
  var import_github_dark = require("highlight.js/styles/github-dark.css");
@@ -6788,6 +6788,9 @@ var createHandleKatexAdd = (editor, closeDialogCommand, savedPos) => {
6788
6788
  // src/editor/handlers/insertion-handlers.ts
6789
6789
  var import_react35 = require("react");
6790
6790
 
6791
+ // src/editor/handlers/markup-insertion.ts
6792
+ var import_model2 = require("@tiptap/pm/model");
6793
+
6791
6794
  // src/utils/prosemirror-iterator.ts
6792
6795
  var import_model = require("@tiptap/pm/model");
6793
6796
  function prosemirrorIterator(slice, cb) {
@@ -6828,6 +6831,34 @@ function collectRehostableImages(slice) {
6828
6831
  });
6829
6832
  return imagesToNodes;
6830
6833
  }
6834
+ function removeImagesFromFragment(fragment) {
6835
+ const newNodes = [];
6836
+ fragment.forEach((node) => {
6837
+ if (node.type.name === "image" && !node.attrs.fileId) return;
6838
+ newNodes.push(node.childCount > 0 ? node.copy(removeImagesFromFragment(node.content)) : node);
6839
+ });
6840
+ return import_model2.Fragment.from(newNodes);
6841
+ }
6842
+ function validOpenDepth(fragment, requestedDepth, fromEnd = false) {
6843
+ let depth = 0;
6844
+ let current = fragment;
6845
+ while (depth < requestedDepth) {
6846
+ if (current.childCount === 0) break;
6847
+ const node = fromEnd ? current.lastChild : current.firstChild;
6848
+ if (node.isLeaf) break;
6849
+ current = node.content;
6850
+ depth++;
6851
+ }
6852
+ return depth;
6853
+ }
6854
+ function removeImagesFromSlice(slice) {
6855
+ const newContent = removeImagesFromFragment(slice.content);
6856
+ return new import_model2.Slice(
6857
+ newContent,
6858
+ validOpenDepth(newContent, slice.openStart),
6859
+ validOpenDepth(newContent, slice.openEnd, true)
6860
+ );
6861
+ }
6831
6862
  function applyUploadResultsToNodes(imagesToNodes, results) {
6832
6863
  results.forEach((result, index) => {
6833
6864
  if (!result) return;
@@ -6840,7 +6871,7 @@ function applyUploadResultsToNodes(imagesToNodes, results) {
6840
6871
  }
6841
6872
 
6842
6873
  // src/editor/handlers/insertion-handlers.ts
6843
- function useInsertionHandlers() {
6874
+ function useInsertionHandlers(imageHandlers) {
6844
6875
  const [insertedImage, setInsertedImage] = (0, import_react35.useState)(null);
6845
6876
  const { activeDialog, openDialogCommand } = useDialogCommand();
6846
6877
  (0, import_react35.useEffect)(() => {
@@ -6852,6 +6883,7 @@ function useInsertionHandlers() {
6852
6883
  if (!file || !file.type.startsWith("image/")) return;
6853
6884
  event.preventDefault();
6854
6885
  event.stopPropagation();
6886
+ if (!imageHandlers?.upload) return;
6855
6887
  setInsertedImage(file);
6856
6888
  openDialogCommand("image");
6857
6889
  }
@@ -6863,8 +6895,7 @@ function useInsertionHandlers() {
6863
6895
  const file = event.dataTransfer?.files?.[0];
6864
6896
  genericInsertionHandler(file, event);
6865
6897
  }
6866
- function onMarkupInsertion(view, event, slice, setDisabled, uploadMultipleByURL) {
6867
- if (!uploadMultipleByURL) return;
6898
+ function onMarkupInsertion(view, event, slice, setDisabled) {
6868
6899
  let hasMarkupInsertion = false;
6869
6900
  if ("clipboardData" in event && Array.from(event.clipboardData?.items ?? []).some(
6870
6901
  (item) => item.type === "text/html"
@@ -6877,14 +6908,18 @@ function useInsertionHandlers() {
6877
6908
  hasMarkupInsertion = true;
6878
6909
  }
6879
6910
  if (hasMarkupInsertion) {
6880
- const imagesToNodes = collectRehostableImages(slice);
6881
- setDisabled(true);
6882
- uploadMultipleByURL(imagesToNodes.map(([url]) => url)).then((results) => {
6883
- applyUploadResultsToNodes(imagesToNodes, results);
6884
- view.dispatch(view.state.tr.replaceSelection(slice));
6885
- }).finally(() => {
6886
- setDisabled(false);
6887
- });
6911
+ if (imageHandlers?.uploadMultipleByURL) {
6912
+ setDisabled(true);
6913
+ const imagesToNodes = collectRehostableImages(slice);
6914
+ imageHandlers.uploadMultipleByURL(imagesToNodes.map(([url]) => url)).then((results) => {
6915
+ applyUploadResultsToNodes(imagesToNodes, results);
6916
+ view.dispatch(view.state.tr.replaceSelection(slice));
6917
+ }).finally(() => {
6918
+ setDisabled(false);
6919
+ });
6920
+ } else {
6921
+ view.dispatch(view.state.tr.replaceSelection(removeImagesFromSlice(slice)));
6922
+ }
6888
6923
  return true;
6889
6924
  }
6890
6925
  }
@@ -7679,7 +7714,7 @@ function TiptapContent({
7679
7714
  onPaste,
7680
7715
  onDrop,
7681
7716
  onMarkupInsertion
7682
- } = useInsertionHandlers();
7717
+ } = useInsertionHandlers(imageHandlers);
7683
7718
  const editor = (0, import_react38.useEditor)(
7684
7719
  {
7685
7720
  editable: isEditable && !disabled,
@@ -7710,8 +7745,7 @@ function TiptapContent({
7710
7745
  view,
7711
7746
  event,
7712
7747
  slice,
7713
- setIsDisabledInternal,
7714
- imageHandlers?.uploadMultipleByURL
7748
+ setIsDisabledInternal
7715
7749
  );
7716
7750
  if (typeof toReturn === "boolean") return toReturn;
7717
7751
  return false;
@@ -7721,8 +7755,7 @@ function TiptapContent({
7721
7755
  view,
7722
7756
  event,
7723
7757
  slice,
7724
- setIsDisabledInternal,
7725
- imageHandlers?.uploadMultipleByURL
7758
+ setIsDisabledInternal
7726
7759
  );
7727
7760
  if (typeof toReturn === "boolean") return toReturn;
7728
7761
  try {
@@ -7731,7 +7764,7 @@ function TiptapContent({
7731
7764
  const json = JSON.parse(text);
7732
7765
  if (json.type === "doc" && json.content) {
7733
7766
  const node = view.state.schema.nodeFromJSON(json);
7734
- const newSlice = new import_model2.Slice(node.content, 0, 0);
7767
+ const newSlice = new import_model3.Slice(node.content, 0, 0);
7735
7768
  view.dispatch(view.state.tr.replaceSelection(newSlice));
7736
7769
  return true;
7737
7770
  }