@blankdotpage/cake 0.1.45 → 0.1.46

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.
@@ -1 +1 @@
1
- {"version":3,"file":"italic.d.ts","sourceRoot":"","sources":["../../../../src/cake/extensions/italic/italic.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,aAAa,EAInB,MAAM,oBAAoB,CAAC;AAS5B,eAAO,MAAM,eAAe,EAAE,aA0H7B,CAAC"}
1
+ {"version":3,"file":"italic.d.ts","sourceRoot":"","sources":["../../../../src/cake/extensions/italic/italic.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,aAAa,EAInB,MAAM,oBAAoB,CAAC;AAsC5B,eAAO,MAAM,eAAe,EAAE,aA+H7B,CAAC"}
@@ -1,5 +1,25 @@
1
1
  import { CursorSourceBuilder } from "../../core/mapping/cursor-source-map";
2
2
  const ITALIC_KIND = "italic";
3
+ function findItalicClose(source, start, end, marker) {
4
+ if (marker === "_") {
5
+ return source.indexOf("_", start + 1);
6
+ }
7
+ let fallback = -1;
8
+ for (let i = start + 1; i < end; i += 1) {
9
+ if (source[i] !== "*") {
10
+ continue;
11
+ }
12
+ const prevIsStar = source[i - 1] === "*";
13
+ const nextIsStar = source[i + 1] === "*";
14
+ if (!prevIsStar && !nextIsStar) {
15
+ return i;
16
+ }
17
+ if (fallback === -1) {
18
+ fallback = i;
19
+ }
20
+ }
21
+ return fallback;
22
+ }
3
23
  export const italicExtension = (editor) => {
4
24
  const disposers = [];
5
25
  disposers.push(editor.registerToggleInline({ kind: ITALIC_KIND, markers: ["*", "_"] }));
@@ -34,7 +54,7 @@ export const italicExtension = (editor) => {
34
54
  if (char === "*" && start > 0 && source[start - 1] === "*") {
35
55
  return null;
36
56
  }
37
- const close = source.indexOf(char, start + 1);
57
+ const close = findItalicClose(source, start, end, char);
38
58
  if (close === -1 || close >= end) {
39
59
  return null;
40
60
  }
@@ -1 +1 @@
1
- {"version":3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E,MAAM,CAAC;CACf,CAAC;AAEF,MAAM,MAAM,oBAAoB,GAAG;IACjC,kBAAkB,CAAC,EAAE,kBAAkB,CAAC;IACxC,MAAM,CAAC,EAAE,mBAAmB,CAAC;CAC9B,CAAC;AAkYF,wBAAgB,aAAa,CAAC,MAAM,EAAE,UAAU,GAAG,IAAI,GAAG,CAAC,MAAM,IAAI,CAAC,CAAC;AACvE,wBAAgB,aAAa,CAAC,OAAO,CAAC,EAAE,oBAAoB,GAAG,aAAa,CAAC"}
@@ -5,6 +5,73 @@ import { getDocLines } from "../../editor/selection/selection-layout";
5
5
  import { cursorOffsetToVisibleOffset, getVisibleText, } from "../../editor/selection/visible-text";
6
6
  import { ensureHttpsProtocol, isUrl } from "../../shared/url";
7
7
  const LINK_KIND = "link";
8
+ function countBoundaryWrappers(fragment) {
9
+ let count = 0;
10
+ let index = 0;
11
+ while (index < fragment.length) {
12
+ if (fragment.startsWith("</u>", index)) {
13
+ count += 1;
14
+ index += 4;
15
+ continue;
16
+ }
17
+ if (fragment.startsWith("<u>", index)) {
18
+ count += 1;
19
+ index += 3;
20
+ continue;
21
+ }
22
+ if (fragment.startsWith("**", index)) {
23
+ count += 1;
24
+ index += 2;
25
+ continue;
26
+ }
27
+ if (fragment.startsWith("~~", index)) {
28
+ count += 1;
29
+ index += 2;
30
+ continue;
31
+ }
32
+ const char = fragment[index] ?? "";
33
+ if (char === "*" || char === "_") {
34
+ count += 1;
35
+ }
36
+ index += 1;
37
+ }
38
+ return count;
39
+ }
40
+ function findEnclosingLinkRange(source, from, to) {
41
+ if (source.length === 0) {
42
+ return null;
43
+ }
44
+ const start = Math.max(0, Math.min(from, source.length));
45
+ const end = Math.max(0, Math.min(to, source.length));
46
+ if (start >= end) {
47
+ return null;
48
+ }
49
+ let linkStart = source.lastIndexOf("[", start);
50
+ while (linkStart !== -1) {
51
+ // Skip image syntax ![...](...)
52
+ if (linkStart > 0 && source[linkStart - 1] === "!") {
53
+ linkStart = source.lastIndexOf("[", linkStart - 1);
54
+ continue;
55
+ }
56
+ const labelClose = source.indexOf("](", linkStart + 1);
57
+ if (labelClose === -1) {
58
+ linkStart = source.lastIndexOf("[", linkStart - 1);
59
+ continue;
60
+ }
61
+ const urlClose = source.indexOf(")", labelClose + 2);
62
+ if (urlClose === -1) {
63
+ linkStart = source.lastIndexOf("[", linkStart - 1);
64
+ continue;
65
+ }
66
+ const labelStart = linkStart + 1;
67
+ const labelEnd = labelClose;
68
+ if (labelStart <= start && end <= labelEnd) {
69
+ return { linkStart, labelStart, labelEnd, urlClose };
70
+ }
71
+ linkStart = source.lastIndexOf("[", linkStart - 1);
72
+ }
73
+ return null;
74
+ }
8
75
  function isDomSelectionInsideLink(editor) {
9
76
  if (typeof window === "undefined") {
10
77
  return false;
@@ -149,11 +216,39 @@ function installLinkExtension(editor, options) {
149
216
  }
150
217
  return null;
151
218
  }
152
- const from = state.map.cursorToSource(cursorStart, "forward");
153
- const to = state.map.cursorToSource(cursorEnd, "backward");
154
- if (from === to) {
219
+ const innerFrom = state.map.cursorToSource(cursorStart, "forward");
220
+ const innerTo = state.map.cursorToSource(cursorEnd, "backward");
221
+ if (innerFrom === innerTo) {
155
222
  return null;
156
223
  }
224
+ // Treat wrap-link as a toggle when the selection is already within the
225
+ // same link label. This matches toolbar expectations for active link.
226
+ const existingLink = findEnclosingLinkRange(state.source, innerFrom, innerTo);
227
+ if (existingLink) {
228
+ const label = state.source.slice(existingLink.labelStart, existingLink.labelEnd);
229
+ const nextSource = state.source.slice(0, existingLink.linkStart) +
230
+ label +
231
+ state.source.slice(existingLink.urlClose + 1);
232
+ const nextState = state.runtime.createState(nextSource);
233
+ const nextStart = nextState.map.sourceToCursor(existingLink.linkStart, "forward");
234
+ const nextEnd = nextState.map.sourceToCursor(existingLink.linkStart + label.length, "backward");
235
+ return {
236
+ source: nextSource,
237
+ selection: {
238
+ start: nextStart.cursorOffset,
239
+ end: nextEnd.cursorOffset,
240
+ affinity: "forward",
241
+ },
242
+ };
243
+ }
244
+ const outerFrom = state.map.cursorToSource(cursorStart, "backward");
245
+ const outerTo = state.map.cursorToSource(cursorEnd, "forward");
246
+ const leftBoundary = state.source.slice(outerFrom, innerFrom);
247
+ const rightBoundary = state.source.slice(innerTo, outerTo);
248
+ const hasNestedBoundaryWrappers = countBoundaryWrappers(leftBoundary) > 1 ||
249
+ countBoundaryWrappers(rightBoundary) > 1;
250
+ const from = hasNestedBoundaryWrappers ? outerFrom : innerFrom;
251
+ const to = hasNestedBoundaryWrappers ? outerTo : innerTo;
157
252
  const label = state.source.slice(from, to);
158
253
  const url = command.url ?? "";
159
254
  const linkMarkdown = `[${label}](${url})`;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@blankdotpage/cake",
3
- "version": "0.1.45",
3
+ "version": "0.1.46",
4
4
  "type": "module",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",