@blankdotpage/cake 0.1.45 → 0.1.47

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,"file":"link-popover.d.ts","sourceRoot":"","sources":["../../../../src/cake/extensions/link/link-popover.tsx"],"names":[],"mappings":"AAWA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,0BAA0B,CAAC;AAC3D,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,QAAQ,CAAC;AA0DlD,wBAAgB,eAAe,CAAC,EAC9B,MAAM,EACN,MAAM,GACP,EAAE;IACD,MAAM,EAAE,UAAU,CAAC;IACnB,MAAM,CAAC,EAAE,mBAAmB,CAAC;CAC9B,kDAkXA"}
1
+ {"version":3,"file":"link-popover.d.ts","sourceRoot":"","sources":["../../../../src/cake/extensions/link/link-popover.tsx"],"names":[],"mappings":"AAWA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,0BAA0B,CAAC;AAC3D,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,QAAQ,CAAC;AA0DlD,wBAAgB,eAAe,CAAC,EAC9B,MAAM,EACN,MAAM,GACP,EAAE;IACD,MAAM,EAAE,UAAU,CAAC;IACnB,MAAM,CAAC,EAAE,mBAAmB,CAAC;CAC9B,kDAiYA"}
@@ -155,6 +155,10 @@ export function CakeLinkPopover({ editor, styles, }) {
155
155
  useEffect(() => {
156
156
  function handleUpdate() {
157
157
  if (stateRef.current.status !== "closed") {
158
+ const anchor = anchorRef.current;
159
+ if (!anchor || !anchor.isConnected) {
160
+ close();
161
+ }
158
162
  return;
159
163
  }
160
164
  const selection = editor.getSelection();
@@ -230,6 +234,11 @@ export function CakeLinkPopover({ editor, styles, }) {
230
234
  if (state.status !== "open") {
231
235
  return;
232
236
  }
237
+ const selection = getSelection();
238
+ if (!selection) {
239
+ close();
240
+ return;
241
+ }
233
242
  const draftValue = inputRef.current?.value ?? state.draftUrl;
234
243
  const trimmed = draftValue.trim();
235
244
  if (!trimmed) {
@@ -237,8 +246,13 @@ export function CakeLinkPopover({ editor, styles, }) {
237
246
  return;
238
247
  }
239
248
  const nextUrl = ensureHttpsProtocol(trimmed);
240
- const anchor = anchorRef.current;
241
- anchor?.setAttribute("href", nextUrl);
249
+ executeCommand({
250
+ type: "set-link-url",
251
+ start: selection.start,
252
+ end: selection.end,
253
+ url: nextUrl,
254
+ selectLabel: state.url.trim() === "",
255
+ });
242
256
  setState({
243
257
  status: "open",
244
258
  url: nextUrl,
@@ -12,8 +12,16 @@ export type UnlinkCommand = {
12
12
  start: number;
13
13
  end: number;
14
14
  };
15
+ /** Command to update the URL for an existing link */
16
+ export type SetLinkUrlCommand = {
17
+ type: "set-link-url";
18
+ start: number;
19
+ end: number;
20
+ url: string;
21
+ selectLabel?: boolean;
22
+ };
15
23
  /** All link extension commands */
16
- export type LinkCommand = WrapLinkCommand | UnlinkCommand;
24
+ export type LinkCommand = WrapLinkCommand | UnlinkCommand | SetLinkUrlCommand;
17
25
  export type OnRequestLinkInput = (editor: CakeEditor) => Promise<{
18
26
  url: string;
19
27
  text: string;
@@ -1 +1 @@
1
- {"version":3,"file":"link.d.ts","sourceRoot":"","sources":["../../../../src/cake/extensions/link/link.tsx"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,aAAa,EAGnB,MAAM,oBAAoB,CAAC;AAU5B,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,0BAA0B,CAAC;AAI3D,8CAA8C;AAC9C,MAAM,MAAM,eAAe,GAAG;IAC5B,IAAI,EAAE,WAAW,CAAC;IAClB,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,WAAW,CAAC,EAAE,OAAO,CAAC;CACvB,CAAC;AAEF,wCAAwC;AACxC,MAAM,MAAM,aAAa,GAAG;IAC1B,IAAI,EAAE,QAAQ,CAAC;IACf,KAAK,EAAE,MAAM,CAAC;IACd,GAAG,EAAE,MAAM,CAAC;CACb,CAAC;AAEF,kCAAkC;AAClC,MAAM,MAAM,WAAW,GAAG,eAAe,GAAG,aAAa,CAAC;AAE1D,MAAM,MAAM,kBAAkB,GAAG,CAC/B,MAAM,EAAE,UAAU,KACf,OAAO,CAAC;IAAE,GA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;CACb,CAAC;AAEF,qDAAqD;AACrD,MAAM,MAAM,iBAAiB,GAAG;IAC9B,IAAI,EAAE,cAAc,CAAC;IACrB,KAAK,EAAE,MAAM,CAAC;IACd,GAAG,EAAE,MAAM,CAAC;IACZ,GAAG,EAAE,MAAM,CAAC;IACZ,WAAW,CAAC,EAAE,OAAO,CAAC;CACvB,CAAC;AAEF,kCAAkC;AAClC,MAAM,MAAM,WAAW,GAAG,eAAe,GAAG,aAAa,GAAG,iBAAiB,CAAC;AAE9E,MAAM,MAAM,kBAAkB,GAAG,CAC/B,MAAM,EAAE,UAAU,KACf,OAAO,CAAC;IAAE,GAAG,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,MAAM,CAAA;CAAE,GAAG,IAAI,CAAC,CAAC;AAEnD,MAAM,MAAM,mBAAmB,GAAG;IAChC,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,IAAI,CAAC,EAAE,MAAM,CAAC;CACf,CAAC;AAEF,MAAM,MAAM,oBAAoB,GAAG;IACjC,kBAAkB,CAAC,EAAE,kBAAkB,CAAC;IACxC,MAAM,CAAC,EAAE,mBAAmB,CAAC;CAC9B,CAAC;AAkcF,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;
@@ -126,6 +193,56 @@ function installLinkExtension(editor, options) {
126
193
  },
127
194
  };
128
195
  }
196
+ if (command.type === "set-link-url") {
197
+ const cursorPos = Math.min(command.start, command.end);
198
+ const sourcePos = state.map.cursorToSource(cursorPos, "forward");
199
+ const source = state.source;
200
+ // Search backwards for the opening bracket
201
+ let linkStart = sourcePos;
202
+ while (linkStart > 0 && source[linkStart] !== "[") {
203
+ linkStart--;
204
+ }
205
+ if (source[linkStart] !== "[") {
206
+ return null;
207
+ }
208
+ // Find the ]( separator
209
+ const labelClose = source.indexOf("](", linkStart + 1);
210
+ if (labelClose === -1) {
211
+ return null;
212
+ }
213
+ // Find the closing )
214
+ const urlClose = source.indexOf(")", labelClose + 2);
215
+ if (urlClose === -1) {
216
+ return null;
217
+ }
218
+ const nextSource = source.slice(0, labelClose + 2) +
219
+ command.url +
220
+ source.slice(urlClose);
221
+ const nextState = state.runtime.createState(nextSource);
222
+ if (command.selectLabel) {
223
+ const labelStartSource = linkStart + 1;
224
+ const labelEndSource = labelClose;
225
+ const startCursor = nextState.map.sourceToCursor(labelStartSource, "forward");
226
+ const endCursor = nextState.map.sourceToCursor(labelEndSource, "backward");
227
+ return {
228
+ source: nextSource,
229
+ selection: {
230
+ start: startCursor.cursorOffset,
231
+ end: endCursor.cursorOffset,
232
+ affinity: "forward",
233
+ },
234
+ };
235
+ }
236
+ const endCursor = nextState.map.sourceToCursor(labelClose, "backward");
237
+ return {
238
+ source: nextSource,
239
+ selection: {
240
+ start: endCursor.cursorOffset,
241
+ end: endCursor.cursorOffset,
242
+ affinity: "backward",
243
+ },
244
+ };
245
+ }
129
246
  if (command.type !== "wrap-link") {
130
247
  return null;
131
248
  }
@@ -149,11 +266,39 @@ function installLinkExtension(editor, options) {
149
266
  }
150
267
  return null;
151
268
  }
152
- const from = state.map.cursorToSource(cursorStart, "forward");
153
- const to = state.map.cursorToSource(cursorEnd, "backward");
154
- if (from === to) {
269
+ const innerFrom = state.map.cursorToSource(cursorStart, "forward");
270
+ const innerTo = state.map.cursorToSource(cursorEnd, "backward");
271
+ if (innerFrom === innerTo) {
155
272
  return null;
156
273
  }
274
+ // Treat wrap-link as a toggle when the selection is already within the
275
+ // same link label. This matches toolbar expectations for active link.
276
+ const existingLink = findEnclosingLinkRange(state.source, innerFrom, innerTo);
277
+ if (existingLink) {
278
+ const label = state.source.slice(existingLink.labelStart, existingLink.labelEnd);
279
+ const nextSource = state.source.slice(0, existingLink.linkStart) +
280
+ label +
281
+ state.source.slice(existingLink.urlClose + 1);
282
+ const nextState = state.runtime.createState(nextSource);
283
+ const nextStart = nextState.map.sourceToCursor(existingLink.linkStart, "forward");
284
+ const nextEnd = nextState.map.sourceToCursor(existingLink.linkStart + label.length, "backward");
285
+ return {
286
+ source: nextSource,
287
+ selection: {
288
+ start: nextStart.cursorOffset,
289
+ end: nextEnd.cursorOffset,
290
+ affinity: "forward",
291
+ },
292
+ };
293
+ }
294
+ const outerFrom = state.map.cursorToSource(cursorStart, "backward");
295
+ const outerTo = state.map.cursorToSource(cursorEnd, "forward");
296
+ const leftBoundary = state.source.slice(outerFrom, innerFrom);
297
+ const rightBoundary = state.source.slice(innerTo, outerTo);
298
+ const hasNestedBoundaryWrappers = countBoundaryWrappers(leftBoundary) > 1 ||
299
+ countBoundaryWrappers(rightBoundary) > 1;
300
+ const from = hasNestedBoundaryWrappers ? outerFrom : innerFrom;
301
+ const to = hasNestedBoundaryWrappers ? outerTo : innerTo;
157
302
  const label = state.source.slice(from, to);
158
303
  const url = command.url ?? "";
159
304
  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.47",
4
4
  "type": "module",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",