@cyberyozh/text-editor 0.1.7 → 0.1.8

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
@@ -36,7 +36,7 @@ module.exports = __toCommonJS(editor_exports);
36
36
 
37
37
  // src/editor/editor.tsx
38
38
  var import_react36 = require("react");
39
- var import_state5 = require("@tiptap/pm/state");
39
+ var import_state6 = require("@tiptap/pm/state");
40
40
  var import_model = require("@tiptap/pm/model");
41
41
  var import_react37 = require("@tiptap/react");
42
42
  var import_menus = require("@tiptap/react/menus");
@@ -5165,36 +5165,30 @@ function getEditorExtensions(options) {
5165
5165
  }
5166
5166
 
5167
5167
  // src/editor/handlers/dialog-handlers.ts
5168
+ var import_state5 = require("@tiptap/pm/state");
5168
5169
  var createHandleImageAdd = (editor, closeDialogCommand, savedPos) => {
5169
5170
  return (attrs) => {
5170
5171
  if (!editor) return;
5171
5172
  const { state } = editor;
5172
- const { $from } = state.selection;
5173
- const text = $from.parent.textContent;
5174
- if (text.lastIndexOf("/") !== -1) {
5175
- editor.chain().focus().command(({ tr }) => {
5176
- const node = editor.schema.nodes.image.create({ ...attrs });
5177
- tr.replaceWith($from.before(), $from.after(), node);
5178
- return true;
5179
- }).scrollIntoView().run();
5180
- } else {
5181
- const insertPos = savedPos ?? state.selection.from;
5182
- editor.chain().focus().command(({ tr, state: state2 }) => {
5183
- const $pos = state2.doc.resolve(insertPos);
5184
- const figureNode = state2.schema.nodes.figure.create({ ...attrs });
5185
- if ($pos.parent.type.name === "paragraph" && $pos.parent.textContent === "") {
5186
- tr.replaceWith($pos.before(), $pos.after(), figureNode);
5187
- } else {
5188
- tr.insert(insertPos, figureNode);
5189
- }
5190
- return true;
5191
- }).scrollIntoView().run();
5192
- }
5173
+ const insertPos = savedPos ?? state.selection.from;
5174
+ editor.chain().focus().command(({ tr, state: state2 }) => {
5175
+ const $pos = state2.doc.resolve(insertPos);
5176
+ const imageNode = state2.schema.nodes.image.create({ ...attrs });
5177
+ if ($pos.parent.type.name === "paragraph" && $pos.parent.textContent === "") {
5178
+ tr.replaceWith($pos.before(), $pos.after(), imageNode);
5179
+ tr.setSelection(import_state5.TextSelection.near(tr.doc.resolve($pos.before())));
5180
+ } else {
5181
+ tr.insert(insertPos, imageNode);
5182
+ tr.setSelection(import_state5.TextSelection.near(tr.doc.resolve(insertPos)));
5183
+ }
5184
+ return true;
5185
+ }).run();
5193
5186
  closeDialogCommand();
5194
5187
  };
5195
5188
  };
5196
5189
  var createHandleImageEdit = (editor, closeDialogCommand, pos) => {
5197
5190
  return (attrs) => {
5191
+ console.log("\u{1F440} ~ createHandleImageEdit ~ attrs:", attrs);
5198
5192
  if (!editor || pos === void 0) return;
5199
5193
  editor.chain().focus().setNodeSelection(pos).updateFigure(attrs).run();
5200
5194
  closeDialogCommand();
@@ -5204,30 +5198,19 @@ var createHandleVideoAdd = (editor, closeDialogCommand, savedPos) => {
5204
5198
  return (url) => {
5205
5199
  if (!editor) return;
5206
5200
  const { state } = editor;
5207
- const { $from } = state.selection;
5208
- const text = $from.parent.textContent;
5209
- if (text.lastIndexOf("/") !== -1) {
5210
- editor.chain().focus().command(({ tr }) => {
5211
- tr.replaceWith(
5212
- $from.before(),
5213
- $from.after(),
5214
- editor.schema.nodes.video.create({ src: url })
5215
- );
5216
- return true;
5217
- }).scrollIntoView().run();
5218
- } else {
5219
- const insertPos = savedPos ?? state.selection.from;
5220
- editor.chain().focus().command(({ tr, state: state2 }) => {
5221
- const $pos = state2.doc.resolve(insertPos);
5222
- const videoNode = state2.schema.nodes.video.create({ src: url });
5223
- if ($pos.parent.type.name === "paragraph" && $pos.parent.textContent === "") {
5224
- tr.replaceWith($pos.before(), $pos.after(), videoNode);
5225
- } else {
5226
- tr.insert(insertPos, videoNode);
5227
- }
5228
- return true;
5229
- }).scrollIntoView().run();
5230
- }
5201
+ const insertPos = savedPos ?? state.selection.from;
5202
+ editor.chain().focus().command(({ tr, state: state2 }) => {
5203
+ const $pos = state2.doc.resolve(insertPos);
5204
+ const videoNode = state2.schema.nodes.video.create({ src: url });
5205
+ if ($pos.parent.type.name === "paragraph" && $pos.parent.textContent === "") {
5206
+ tr.replaceWith($pos.before(), $pos.after(), videoNode);
5207
+ tr.setSelection(import_state5.TextSelection.near(tr.doc.resolve($pos.before())));
5208
+ } else {
5209
+ tr.insert(insertPos, videoNode);
5210
+ tr.setSelection(import_state5.TextSelection.near(tr.doc.resolve(insertPos)));
5211
+ }
5212
+ return true;
5213
+ }).run();
5231
5214
  closeDialogCommand();
5232
5215
  };
5233
5216
  };
@@ -5244,25 +5227,14 @@ var createHandleLinkAdd = (editor, closeDialogCommand, savedPos) => {
5244
5227
  }).run();
5245
5228
  } else {
5246
5229
  const { state } = editor;
5247
- const { $from } = state.selection;
5248
- const text = $from.parent.textContent;
5249
- const nodeStart = $from.start();
5250
- const beforeCursor = text.substring(0, $from.pos - nodeStart);
5251
- const slashIndex = beforeCursor.lastIndexOf("/");
5252
- if (slashIndex !== -1) {
5253
- editor.chain().focus().command(({ tr }) => {
5254
- tr.delete(nodeStart + slashIndex, $from.pos);
5255
- return true;
5256
- }).extendMarkRange("link").setLink({ href: url }).insertContent(title).scrollIntoView().run();
5257
- } else {
5258
- const insertPos = savedPos ?? state.selection.from;
5259
- editor.chain().focus().command(({ tr }) => {
5260
- const linkMark = editor.schema.marks.link.create({ href: url });
5261
- const textNode = editor.schema.text(title, [linkMark]);
5262
- tr.insert(insertPos, textNode);
5263
- return true;
5264
- }).scrollIntoView().run();
5265
- }
5230
+ const insertPos = savedPos ?? state.selection.from;
5231
+ editor.chain().focus().command(({ tr }) => {
5232
+ const linkMark = editor.schema.marks.link.create({ href: url });
5233
+ const textNode = editor.schema.text(title, [linkMark]);
5234
+ tr.insert(insertPos, textNode);
5235
+ tr.setSelection(import_state5.TextSelection.create(tr.doc, insertPos, insertPos + title.length));
5236
+ return true;
5237
+ }).run();
5266
5238
  }
5267
5239
  closeDialogCommand();
5268
5240
  };
@@ -5274,27 +5246,13 @@ var createHandleKatexAdd = (editor, closeDialogCommand, savedPos) => {
5274
5246
  editor.chain().setNodeSelection(pos).updateInlineMath({ latex }).focus().run();
5275
5247
  } else {
5276
5248
  const { state } = editor;
5277
- const { $from } = state.selection;
5278
- const text = $from.parent.textContent;
5279
- const nodeStart = $from.start();
5280
- const beforeCursor = text.substring(0, $from.pos - nodeStart);
5281
- const slashIndex = beforeCursor.lastIndexOf("/");
5282
- if (slashIndex !== -1) {
5283
- const deleteFrom = nodeStart + slashIndex;
5284
- const deleteTo = $from.pos;
5285
- editor.chain().focus().command(({ tr, state: state2 }) => {
5286
- const inlineMathNode = state2.schema.nodes.inlineMath.create({ latex });
5287
- tr.replaceRangeWith(deleteFrom, deleteTo, inlineMathNode);
5288
- return true;
5289
- }).scrollIntoView().run();
5290
- } else {
5291
- const insertPos = savedPos ?? state.selection.from;
5292
- editor.chain().focus().command(({ tr, state: state2 }) => {
5293
- const inlineMathNode = state2.schema.nodes.inlineMath.create({ latex });
5294
- tr.insert(insertPos, inlineMathNode);
5295
- return true;
5296
- }).scrollIntoView().run();
5297
- }
5249
+ const insertPos = savedPos ?? state.selection.from;
5250
+ editor.chain().focus().command(({ tr, state: state2 }) => {
5251
+ const inlineMathNode = state2.schema.nodes.inlineMath.create({ latex });
5252
+ tr.insert(insertPos, inlineMathNode);
5253
+ tr.setSelection(import_state5.TextSelection.near(tr.doc.resolve(insertPos)));
5254
+ return true;
5255
+ }).run();
5298
5256
  }
5299
5257
  closeDialogCommand();
5300
5258
  };
@@ -6119,10 +6077,16 @@ function TiptapContent({
6119
6077
  setSavedDialogCursorPos(void 0);
6120
6078
  closeDialogCommand();
6121
6079
  };
6122
- const handleImageAdd = createHandleImageAdd(editor, handleCloseDialog, savedDialogCursorPos);
6080
+ const handleImageAdd = (attrs) => {
6081
+ createHandleImageAdd(editor, handleCloseDialog, savedDialogCursorPos)(attrs);
6082
+ };
6123
6083
  const handleImageEdit = createHandleImageEdit(editor, handleCloseDialog, editFigureContext?.pos);
6124
- const handleVideoAdd = createHandleVideoAdd(editor, handleCloseDialog, savedDialogCursorPos);
6125
- const handleLinkAdd = createHandleLinkAdd(editor, handleCloseDialog, savedDialogCursorPos);
6084
+ const handleVideoAdd = (url) => {
6085
+ createHandleVideoAdd(editor, handleCloseDialog, savedDialogCursorPos)(url);
6086
+ };
6087
+ const handleLinkAdd = (title, url) => {
6088
+ createHandleLinkAdd(editor, handleCloseDialog, savedDialogCursorPos)(title, url);
6089
+ };
6126
6090
  const handleKatexAdd = (latex) => {
6127
6091
  const katexHandler = createHandleKatexAdd(editor, handleCloseDialog, savedDialogCursorPos);
6128
6092
  katexHandler(latex, katexDialogContext?.pos);
@@ -6134,7 +6098,7 @@ function TiptapContent({
6134
6098
  });
6135
6099
  }, [content, editor]);
6136
6100
  const shouldBubbleMenuShow = ({ state }) => {
6137
- if (state.selection instanceof import_state5.NodeSelection) {
6101
+ if (state.selection instanceof import_state6.NodeSelection) {
6138
6102
  return false;
6139
6103
  }
6140
6104
  if (state.selection.empty) {