@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.mjs CHANGED
@@ -5129,36 +5129,30 @@ function getEditorExtensions(options) {
5129
5129
  }
5130
5130
 
5131
5131
  // src/editor/handlers/dialog-handlers.ts
5132
+ import { TextSelection as TextSelection5 } from "@tiptap/pm/state";
5132
5133
  var createHandleImageAdd = (editor, closeDialogCommand, savedPos) => {
5133
5134
  return (attrs) => {
5134
5135
  if (!editor) return;
5135
5136
  const { state } = editor;
5136
- const { $from } = state.selection;
5137
- const text = $from.parent.textContent;
5138
- if (text.lastIndexOf("/") !== -1) {
5139
- editor.chain().focus().command(({ tr }) => {
5140
- const node = editor.schema.nodes.image.create({ ...attrs });
5141
- tr.replaceWith($from.before(), $from.after(), node);
5142
- return true;
5143
- }).scrollIntoView().run();
5144
- } else {
5145
- const insertPos = savedPos ?? state.selection.from;
5146
- editor.chain().focus().command(({ tr, state: state2 }) => {
5147
- const $pos = state2.doc.resolve(insertPos);
5148
- const figureNode = state2.schema.nodes.figure.create({ ...attrs });
5149
- if ($pos.parent.type.name === "paragraph" && $pos.parent.textContent === "") {
5150
- tr.replaceWith($pos.before(), $pos.after(), figureNode);
5151
- } else {
5152
- tr.insert(insertPos, figureNode);
5153
- }
5154
- return true;
5155
- }).scrollIntoView().run();
5156
- }
5137
+ const insertPos = savedPos ?? state.selection.from;
5138
+ editor.chain().focus().command(({ tr, state: state2 }) => {
5139
+ const $pos = state2.doc.resolve(insertPos);
5140
+ const imageNode = state2.schema.nodes.image.create({ ...attrs });
5141
+ if ($pos.parent.type.name === "paragraph" && $pos.parent.textContent === "") {
5142
+ tr.replaceWith($pos.before(), $pos.after(), imageNode);
5143
+ tr.setSelection(TextSelection5.near(tr.doc.resolve($pos.before())));
5144
+ } else {
5145
+ tr.insert(insertPos, imageNode);
5146
+ tr.setSelection(TextSelection5.near(tr.doc.resolve(insertPos)));
5147
+ }
5148
+ return true;
5149
+ }).run();
5157
5150
  closeDialogCommand();
5158
5151
  };
5159
5152
  };
5160
5153
  var createHandleImageEdit = (editor, closeDialogCommand, pos) => {
5161
5154
  return (attrs) => {
5155
+ console.log("\u{1F440} ~ createHandleImageEdit ~ attrs:", attrs);
5162
5156
  if (!editor || pos === void 0) return;
5163
5157
  editor.chain().focus().setNodeSelection(pos).updateFigure(attrs).run();
5164
5158
  closeDialogCommand();
@@ -5168,30 +5162,19 @@ var createHandleVideoAdd = (editor, closeDialogCommand, savedPos) => {
5168
5162
  return (url) => {
5169
5163
  if (!editor) return;
5170
5164
  const { state } = editor;
5171
- const { $from } = state.selection;
5172
- const text = $from.parent.textContent;
5173
- if (text.lastIndexOf("/") !== -1) {
5174
- editor.chain().focus().command(({ tr }) => {
5175
- tr.replaceWith(
5176
- $from.before(),
5177
- $from.after(),
5178
- editor.schema.nodes.video.create({ src: url })
5179
- );
5180
- return true;
5181
- }).scrollIntoView().run();
5182
- } else {
5183
- const insertPos = savedPos ?? state.selection.from;
5184
- editor.chain().focus().command(({ tr, state: state2 }) => {
5185
- const $pos = state2.doc.resolve(insertPos);
5186
- const videoNode = state2.schema.nodes.video.create({ src: url });
5187
- if ($pos.parent.type.name === "paragraph" && $pos.parent.textContent === "") {
5188
- tr.replaceWith($pos.before(), $pos.after(), videoNode);
5189
- } else {
5190
- tr.insert(insertPos, videoNode);
5191
- }
5192
- return true;
5193
- }).scrollIntoView().run();
5194
- }
5165
+ const insertPos = savedPos ?? state.selection.from;
5166
+ editor.chain().focus().command(({ tr, state: state2 }) => {
5167
+ const $pos = state2.doc.resolve(insertPos);
5168
+ const videoNode = state2.schema.nodes.video.create({ src: url });
5169
+ if ($pos.parent.type.name === "paragraph" && $pos.parent.textContent === "") {
5170
+ tr.replaceWith($pos.before(), $pos.after(), videoNode);
5171
+ tr.setSelection(TextSelection5.near(tr.doc.resolve($pos.before())));
5172
+ } else {
5173
+ tr.insert(insertPos, videoNode);
5174
+ tr.setSelection(TextSelection5.near(tr.doc.resolve(insertPos)));
5175
+ }
5176
+ return true;
5177
+ }).run();
5195
5178
  closeDialogCommand();
5196
5179
  };
5197
5180
  };
@@ -5208,25 +5191,14 @@ var createHandleLinkAdd = (editor, closeDialogCommand, savedPos) => {
5208
5191
  }).run();
5209
5192
  } else {
5210
5193
  const { state } = editor;
5211
- const { $from } = state.selection;
5212
- const text = $from.parent.textContent;
5213
- const nodeStart = $from.start();
5214
- const beforeCursor = text.substring(0, $from.pos - nodeStart);
5215
- const slashIndex = beforeCursor.lastIndexOf("/");
5216
- if (slashIndex !== -1) {
5217
- editor.chain().focus().command(({ tr }) => {
5218
- tr.delete(nodeStart + slashIndex, $from.pos);
5219
- return true;
5220
- }).extendMarkRange("link").setLink({ href: url }).insertContent(title).scrollIntoView().run();
5221
- } else {
5222
- const insertPos = savedPos ?? state.selection.from;
5223
- editor.chain().focus().command(({ tr }) => {
5224
- const linkMark = editor.schema.marks.link.create({ href: url });
5225
- const textNode = editor.schema.text(title, [linkMark]);
5226
- tr.insert(insertPos, textNode);
5227
- return true;
5228
- }).scrollIntoView().run();
5229
- }
5194
+ const insertPos = savedPos ?? state.selection.from;
5195
+ editor.chain().focus().command(({ tr }) => {
5196
+ const linkMark = editor.schema.marks.link.create({ href: url });
5197
+ const textNode = editor.schema.text(title, [linkMark]);
5198
+ tr.insert(insertPos, textNode);
5199
+ tr.setSelection(TextSelection5.create(tr.doc, insertPos, insertPos + title.length));
5200
+ return true;
5201
+ }).run();
5230
5202
  }
5231
5203
  closeDialogCommand();
5232
5204
  };
@@ -5238,27 +5210,13 @@ var createHandleKatexAdd = (editor, closeDialogCommand, savedPos) => {
5238
5210
  editor.chain().setNodeSelection(pos).updateInlineMath({ latex }).focus().run();
5239
5211
  } else {
5240
5212
  const { state } = editor;
5241
- const { $from } = state.selection;
5242
- const text = $from.parent.textContent;
5243
- const nodeStart = $from.start();
5244
- const beforeCursor = text.substring(0, $from.pos - nodeStart);
5245
- const slashIndex = beforeCursor.lastIndexOf("/");
5246
- if (slashIndex !== -1) {
5247
- const deleteFrom = nodeStart + slashIndex;
5248
- const deleteTo = $from.pos;
5249
- editor.chain().focus().command(({ tr, state: state2 }) => {
5250
- const inlineMathNode = state2.schema.nodes.inlineMath.create({ latex });
5251
- tr.replaceRangeWith(deleteFrom, deleteTo, inlineMathNode);
5252
- return true;
5253
- }).scrollIntoView().run();
5254
- } else {
5255
- const insertPos = savedPos ?? state.selection.from;
5256
- editor.chain().focus().command(({ tr, state: state2 }) => {
5257
- const inlineMathNode = state2.schema.nodes.inlineMath.create({ latex });
5258
- tr.insert(insertPos, inlineMathNode);
5259
- return true;
5260
- }).scrollIntoView().run();
5261
- }
5213
+ const insertPos = savedPos ?? state.selection.from;
5214
+ editor.chain().focus().command(({ tr, state: state2 }) => {
5215
+ const inlineMathNode = state2.schema.nodes.inlineMath.create({ latex });
5216
+ tr.insert(insertPos, inlineMathNode);
5217
+ tr.setSelection(TextSelection5.near(tr.doc.resolve(insertPos)));
5218
+ return true;
5219
+ }).run();
5262
5220
  }
5263
5221
  closeDialogCommand();
5264
5222
  };
@@ -6083,10 +6041,16 @@ function TiptapContent({
6083
6041
  setSavedDialogCursorPos(void 0);
6084
6042
  closeDialogCommand();
6085
6043
  };
6086
- const handleImageAdd = createHandleImageAdd(editor, handleCloseDialog, savedDialogCursorPos);
6044
+ const handleImageAdd = (attrs) => {
6045
+ createHandleImageAdd(editor, handleCloseDialog, savedDialogCursorPos)(attrs);
6046
+ };
6087
6047
  const handleImageEdit = createHandleImageEdit(editor, handleCloseDialog, editFigureContext?.pos);
6088
- const handleVideoAdd = createHandleVideoAdd(editor, handleCloseDialog, savedDialogCursorPos);
6089
- const handleLinkAdd = createHandleLinkAdd(editor, handleCloseDialog, savedDialogCursorPos);
6048
+ const handleVideoAdd = (url) => {
6049
+ createHandleVideoAdd(editor, handleCloseDialog, savedDialogCursorPos)(url);
6050
+ };
6051
+ const handleLinkAdd = (title, url) => {
6052
+ createHandleLinkAdd(editor, handleCloseDialog, savedDialogCursorPos)(title, url);
6053
+ };
6090
6054
  const handleKatexAdd = (latex) => {
6091
6055
  const katexHandler = createHandleKatexAdd(editor, handleCloseDialog, savedDialogCursorPos);
6092
6056
  katexHandler(latex, katexDialogContext?.pos);