@sofereditor/core 0.5.0 → 0.7.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/index.cjs CHANGED
@@ -84,6 +84,7 @@ __export(index_exports, {
84
84
  insertText: () => insertText,
85
85
  isCollapsed: () => isCollapsed,
86
86
  isCommandOrigin: () => isCommandOrigin,
87
+ isEmbedAdjacentToCaret: () => isEmbedAdjacentToCaret,
87
88
  isImageEmbed: () => isImageEmbed,
88
89
  isKnownBlockType: () => isKnownBlockType,
89
90
  isLegacySerializedDocument: () => isLegacySerializedDocument,
@@ -1171,6 +1172,24 @@ function deleteForward(ctx) {
1171
1172
  }
1172
1173
  });
1173
1174
  }
1175
+ function isEmbedAdjacentToCaret(ctx, direction) {
1176
+ const sel = ctx.getSelection();
1177
+ if (!isCollapsed(sel)) return false;
1178
+ const pos = sel.focus;
1179
+ const yText = ctx.doc.textAt(pos.blockIndex, pos.cellIndex);
1180
+ if (!yText) return false;
1181
+ const delta = yText.toDelta();
1182
+ let cursor = 0;
1183
+ for (const op of delta) {
1184
+ const len = typeof op.insert === "string" ? op.insert.length : 1;
1185
+ const opStart = cursor;
1186
+ const opEnd = cursor + len;
1187
+ cursor = opEnd;
1188
+ const matches = direction === "backward" ? opEnd === pos.offset : opStart === pos.offset;
1189
+ if (matches) return isImageEmbed(op.insert);
1190
+ }
1191
+ return false;
1192
+ }
1174
1193
  var booleanMarks = /* @__PURE__ */ new Set(["bold", "italic", "underline", "strike"]);
1175
1194
  function toggleMark(ctx, name, value) {
1176
1195
  const sel = ctx.getSelection();
@@ -1317,6 +1336,34 @@ function insertSlice(ctx, slice) {
1317
1336
  const tailDelta = sliceDelta(full, offset, deltaLength(full));
1318
1337
  targetText.delete(offset, targetText.length - offset);
1319
1338
  const blocks = slice.blocks;
1339
+ if (blocks.length === 1 && slice.blockLevel) {
1340
+ const originalType2 = ctx.doc.getBlockType(blockIndex);
1341
+ const originalAttrs2 = ctx.doc.getBlockAttrs(blockIndex);
1342
+ let hostIndex = blockIndex;
1343
+ if (offset === 0) {
1344
+ const blk = ctx.doc.blocks.get(blockIndex);
1345
+ blk.set("type", blocks[0].type);
1346
+ const am = blk.get("attrs");
1347
+ am.clear();
1348
+ for (const [k, v] of Object.entries(blocks[0].attrs)) {
1349
+ if (v !== void 0) am.set(k, v);
1350
+ }
1351
+ writeDeltaInto(targetText, 0, blocks[0].delta);
1352
+ } else {
1353
+ hostIndex = blockIndex + 1;
1354
+ const b = createBlock(blocks[0].type, "", blocks[0].attrs);
1355
+ ctx.doc.blocks.insert(hostIndex, [b]);
1356
+ writeDeltaInto(b.get("text"), 0, blocks[0].delta);
1357
+ }
1358
+ const caretPos = ctx.doc.getBlockText(hostIndex).length;
1359
+ if (tailDelta.length > 0) {
1360
+ const tb = createBlock(originalType2 ?? "paragraph", "", originalAttrs2);
1361
+ ctx.doc.blocks.insert(hostIndex + 1, [tb]);
1362
+ writeDeltaInto(tb.get("text"), 0, tailDelta);
1363
+ }
1364
+ ctx.setSelection(collapsedSelection({ blockIndex: hostIndex, offset: caretPos }));
1365
+ return;
1366
+ }
1320
1367
  if (blocks.length === 1) {
1321
1368
  const afterS0 = writeDeltaInto(targetText, offset, blocks[0].delta);
1322
1369
  writeDeltaInto(targetText, afterS0, tailDelta);
@@ -2349,6 +2396,7 @@ var EditorHistory = class {
2349
2396
  insertText,
2350
2397
  isCollapsed,
2351
2398
  isCommandOrigin,
2399
+ isEmbedAdjacentToCaret,
2352
2400
  isImageEmbed,
2353
2401
  isKnownBlockType,
2354
2402
  isLegacySerializedDocument,