@jxsuite/studio 0.6.2 → 0.8.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.
@@ -3,6 +3,26 @@
3
3
  import { html, render as litRender, nothing } from "lit-html";
4
4
  import { activityBar, update, getState, renderOnly } from "../store.js";
5
5
 
6
+ const gitBranchIcon = (/** @type {any} */ s) => html`
7
+ <svg
8
+ slot="icon"
9
+ xmlns="http://www.w3.org/2000/svg"
10
+ width=${s === "m" ? 20 : 16}
11
+ height=${s === "m" ? 20 : 16}
12
+ viewBox="0 0 24 24"
13
+ fill="none"
14
+ stroke="currentColor"
15
+ stroke-width="2"
16
+ stroke-linecap="round"
17
+ stroke-linejoin="round"
18
+ >
19
+ <line x1="6" y1="3" x2="6" y2="15"></line>
20
+ <circle cx="18" cy="6" r="3"></circle>
21
+ <circle cx="6" cy="18" r="3"></circle>
22
+ <path d="M18 9a9 9 0 0 1-9 9"></path>
23
+ </svg>
24
+ `;
25
+
6
26
  /**
7
27
  * @param {any} tag
8
28
  * @param {any} size
@@ -32,6 +52,7 @@ export function tabIcon(tag, size) {
32
52
  html`<sp-icon-artboard slot="icon" size=${s}></sp-icon-artboard>`,
33
53
  "sp-icon-box": (/** @type {any} */ s) =>
34
54
  html`<sp-icon-box slot="icon" size=${s}></sp-icon-box>`,
55
+ "sp-icon-git-branch": gitBranchIcon,
35
56
  };
36
57
  const fn = m[tag];
37
58
  return fn ? fn(size || "s") : nothing;
@@ -47,6 +68,7 @@ export function renderActivityBar(S) {
47
68
  { value: "state", icon: "sp-icon-brackets", label: "State" },
48
69
  { value: "data", icon: "sp-icon-data", label: "Data" },
49
70
  { value: "head", icon: "sp-icon-file-single-web-page", label: "Head" },
71
+ { value: "git", icon: "sp-icon-git-branch", label: "Source Control" },
50
72
  ];
51
73
  const tpl = html`
52
74
  <sp-tabs
@@ -0,0 +1,436 @@
1
+ /**
2
+ * Block action bar — extracted from studio.js (Phase 4h). Floating toolbar above selected elements
3
+ * with parent selector, move arrows, drag handle, component actions, and inline formatting.
4
+ */
5
+
6
+ import { html, render as litRender, nothing } from "lit-html";
7
+ import { draggable } from "@atlaskit/pragmatic-drag-and-drop/element/adapter";
8
+
9
+ import {
10
+ getState,
11
+ update,
12
+ updateSession,
13
+ renderOnly,
14
+ selectNode,
15
+ moveNode,
16
+ getNodeAtPath,
17
+ nodeLabel,
18
+ parentElementPath,
19
+ childIndex,
20
+ } from "../store.js";
21
+ import { view } from "../view.js";
22
+ import { isEditing, getActiveElement, getInlineActions } from "../editor/inline-edit.js";
23
+ import { toggleInlineFormat, isTagActiveInSelection } from "../editor/inline-format.js";
24
+ import { componentRegistry } from "../files/components.js";
25
+ import { convertToComponent } from "../editor/convert-to-component.js";
26
+
27
+ /** @type {any} */
28
+ let _ctx = null;
29
+
30
+ /**
31
+ * Initialize the block action bar module.
32
+ *
33
+ * @param {{
34
+ * getCanvasMode: () => string;
35
+ * findCanvasElement: Function;
36
+ * getActivePanel: Function;
37
+ * navigateToComponent: Function;
38
+ * createFloatingContainer: Function;
39
+ * }} ctx
40
+ */
41
+ export function initBlockActionBar(ctx) {
42
+ _ctx = ctx;
43
+ view.linkPopoverHost = document.createElement("div");
44
+ view.linkPopoverHost.style.display = "contents";
45
+ (document.querySelector("sp-theme") || document.body).appendChild(view.linkPopoverHost);
46
+ }
47
+
48
+ /** Pre-built icon templates for inline format buttons (avoids unsafeStatic) */
49
+ const formatIconMap = /** @type {Record<string, any>} */ ({
50
+ "sp-icon-text-bold": html`<sp-icon-text-bold slot="icon"></sp-icon-text-bold>`,
51
+ "sp-icon-text-italic": html`<sp-icon-text-italic slot="icon"></sp-icon-text-italic>`,
52
+ "sp-icon-text-underline": html`<sp-icon-text-underline slot="icon"></sp-icon-text-underline>`,
53
+ "sp-icon-text-strikethrough": html`<sp-icon-text-strikethrough
54
+ slot="icon"
55
+ ></sp-icon-text-strikethrough>`,
56
+ "sp-icon-text-superscript": html`<sp-icon-text-superscript
57
+ slot="icon"
58
+ ></sp-icon-text-superscript>`,
59
+ "sp-icon-text-subscript": html`<sp-icon-text-subscript slot="icon"></sp-icon-text-subscript>`,
60
+ "sp-icon-code": html`<sp-icon-code slot="icon"></sp-icon-code>`,
61
+ "sp-icon-link": html`<sp-icon-link slot="icon"></sp-icon-link>`,
62
+ });
63
+
64
+ /**
65
+ * Prevent the bar from stealing focus from contenteditable
66
+ *
67
+ * @param {any} e
68
+ */
69
+ function onBarMousedown(e) {
70
+ if (e.target.closest("sp-textfield")) return;
71
+ if (e.target.closest(".bar-drag-handle")) return;
72
+ e.preventDefault();
73
+ }
74
+
75
+ /** Saved selection range for format button mousedown→click flow */
76
+ function captureSelectionRange() {
77
+ const sel = window.getSelection();
78
+ if (sel && sel.rangeCount) view.savedRange = sel.getRangeAt(0).cloneRange();
79
+ }
80
+
81
+ /**
82
+ * @param {any} e
83
+ * @param {any} action
84
+ */
85
+ function onFormatClick(e, action) {
86
+ e.stopPropagation();
87
+ if (action.command === "link") {
88
+ showLinkPopover(e.target.closest("sp-action-button"));
89
+ } else if (view.savedRange) {
90
+ const sel = /** @type {any} */ (window.getSelection());
91
+ const anchor = view.savedRange.startContainer;
92
+ const editableRoot = (
93
+ anchor?.nodeType === Node.ELEMENT_NODE ? anchor : anchor?.parentElement
94
+ )?.closest("[contenteditable]");
95
+ if (editableRoot) {
96
+ editableRoot.focus();
97
+ sel.removeAllRanges();
98
+ sel.addRange(view.savedRange);
99
+ applyInlineFormat(action);
100
+ }
101
+ }
102
+ }
103
+
104
+ function renderParentSelector() {
105
+ const S = getState();
106
+ const pPath = parentElementPath(S.selection);
107
+ if (!pPath) return nothing;
108
+ const parentNode = getNodeAtPath(S.document, pPath);
109
+ return html`
110
+ <sp-action-button
111
+ size="xs"
112
+ quiet
113
+ title="Select parent: ${nodeLabel(parentNode)}"
114
+ @click=${(/** @type {any} */ e) => {
115
+ e.stopPropagation();
116
+ const S = getState();
117
+ update(selectNode(S, pPath));
118
+ }}
119
+ >
120
+ <sp-icon-back slot="icon"></sp-icon-back>
121
+ </sp-action-button>
122
+ `;
123
+ }
124
+
125
+ function renderMoveArrows() {
126
+ const S = getState();
127
+ const idx = /** @type {number} */ (childIndex(S.selection));
128
+ const pPath = parentElementPath(S.selection);
129
+ const parentNode = getNodeAtPath(S.document, /** @type {any} */ (pPath));
130
+ const siblings = parentNode?.children;
131
+ return html`
132
+ <sp-action-button
133
+ size="xs"
134
+ quiet
135
+ title="Move up"
136
+ ?disabled=${idx <= 0}
137
+ @click=${(/** @type {any} */ e) => {
138
+ e.stopPropagation();
139
+ moveSelectionUp();
140
+ }}
141
+ >
142
+ <sp-icon-arrow-up slot="icon"></sp-icon-arrow-up>
143
+ </sp-action-button>
144
+ <sp-action-button
145
+ size="xs"
146
+ quiet
147
+ title="Move down"
148
+ ?disabled=${!siblings || idx >= siblings.length - 1}
149
+ @click=${(/** @type {any} */ e) => {
150
+ e.stopPropagation();
151
+ moveSelectionDown();
152
+ }}
153
+ >
154
+ <sp-icon-arrow-down slot="icon"></sp-icon-arrow-down>
155
+ </sp-action-button>
156
+ `;
157
+ }
158
+
159
+ /**
160
+ * Apply an inline format action.
161
+ *
162
+ * @param {any} action
163
+ */
164
+ function applyInlineFormat(action) {
165
+ /** @type {Record<string, any>} */
166
+ const cmdToTag = {
167
+ bold: "strong",
168
+ italic: "em",
169
+ underline: "u",
170
+ strikethrough: "del",
171
+ superscript: "sup",
172
+ subscript: "sub",
173
+ code: "code",
174
+ };
175
+
176
+ const tag = cmdToTag[action.command];
177
+ if (tag) {
178
+ const editableRoot = getActiveElement();
179
+ toggleInlineFormat(tag, editableRoot);
180
+ }
181
+ requestAnimationFrame(() => renderBlockActionBar());
182
+ }
183
+
184
+ /** Dismiss the link popover if open. */
185
+ export function dismissLinkPopover() {
186
+ if (view.linkPopoverHost) litRender(nothing, view.linkPopoverHost);
187
+ }
188
+
189
+ /** @param {any} anchorBtn */
190
+ function showLinkPopover(anchorBtn) {
191
+ litRender(nothing, view.linkPopoverHost);
192
+
193
+ const sel = window.getSelection();
194
+ /** @type {any} */
195
+ let existingLink = null;
196
+ if (sel?.rangeCount) {
197
+ /** @type {any} */
198
+ let node = sel.anchorNode;
199
+ while (node && node !== document.body) {
200
+ if (node.nodeType === Node.ELEMENT_NODE && node.tagName.toLowerCase() === "a") {
201
+ existingLink = node;
202
+ break;
203
+ }
204
+ node = node.parentNode;
205
+ }
206
+ }
207
+
208
+ const rect = anchorBtn.getBoundingClientRect();
209
+
210
+ const onApply = () => {
211
+ const field = view.linkPopoverHost.querySelector("sp-textfield");
212
+ const url = /** @type {any} */ (field)?.value;
213
+ if (existingLink) {
214
+ existingLink.setAttribute("href", url);
215
+ } else if (url) {
216
+ document.execCommand("createLink", false, url);
217
+ }
218
+ litRender(nothing, view.linkPopoverHost);
219
+ renderBlockActionBar();
220
+ };
221
+
222
+ const onRemove = () => {
223
+ const frag = document.createDocumentFragment();
224
+ while (existingLink.firstChild) frag.appendChild(existingLink.firstChild);
225
+ existingLink.parentNode.replaceChild(frag, existingLink);
226
+ litRender(nothing, view.linkPopoverHost);
227
+ renderBlockActionBar();
228
+ };
229
+
230
+ const onKeydown = (/** @type {any} */ e) => {
231
+ if (e.key === "Enter") onApply();
232
+ else if (e.key === "Escape") {
233
+ litRender(nothing, view.linkPopoverHost);
234
+ }
235
+ };
236
+
237
+ litRender(
238
+ html`
239
+ <sp-popover
240
+ class="link-popover"
241
+ open
242
+ style="position:fixed; left:${rect.left}px; top:${rect.bottom + 4}px; z-index:30"
243
+ >
244
+ <sp-textfield
245
+ placeholder="https://..."
246
+ size="s"
247
+ style="width:200px"
248
+ value=${existingLink?.getAttribute("href") || ""}
249
+ @keydown=${onKeydown}
250
+ ></sp-textfield>
251
+ <sp-action-button size="xs" @click=${onApply}>
252
+ ${existingLink ? "Update" : "Apply"}
253
+ </sp-action-button>
254
+ ${existingLink
255
+ ? html` <sp-action-button size="xs" @click=${onRemove}>Remove</sp-action-button> `
256
+ : nothing}
257
+ </sp-popover>
258
+ `,
259
+ view.linkPopoverHost,
260
+ );
261
+
262
+ requestAnimationFrame(
263
+ () =>
264
+ /** @type {HTMLElement | null} */ (
265
+ view.linkPopoverHost?.querySelector("sp-textfield")
266
+ )?.focus(),
267
+ );
268
+ }
269
+
270
+ /** Move the selected node up (swap with previous sibling). */
271
+ function moveSelectionUp() {
272
+ const S = getState();
273
+ if (!S.selection || S.selection.length < 2) return;
274
+ const idx = /** @type {number} */ (childIndex(S.selection));
275
+ if (idx <= 0) return;
276
+ const pPath = /** @type {any} */ (parentElementPath(S.selection));
277
+ update(moveNode(S, S.selection, pPath, idx - 1));
278
+ updateSession({ selection: [...pPath, "children", idx - 1] });
279
+ renderOnly("overlays");
280
+ }
281
+
282
+ /** Move the selected node down (swap with next sibling). */
283
+ function moveSelectionDown() {
284
+ const S = getState();
285
+ if (!S.selection || S.selection.length < 2) return;
286
+ const idx = /** @type {number} */ (childIndex(S.selection));
287
+ const pPath = /** @type {any} */ (parentElementPath(S.selection));
288
+ const parentNode = getNodeAtPath(S.document, pPath);
289
+ const siblings = parentNode?.children;
290
+ if (!siblings || idx >= siblings.length - 1) return;
291
+ update(moveNode(S, S.selection, pPath, idx + 2));
292
+ updateSession({ selection: [...pPath, "children", idx + 1] });
293
+ renderOnly("overlays");
294
+ }
295
+
296
+ /** Render the unified block action bar above the selected element. */
297
+ export function renderBlockActionBar() {
298
+ if (!view.blockActionBarEl) {
299
+ view.blockActionBarEl = _ctx.createFloatingContainer();
300
+ }
301
+
302
+ if (view.selDragCleanup) {
303
+ view.selDragCleanup();
304
+ view.selDragCleanup = null;
305
+ }
306
+
307
+ const S = getState();
308
+ const canvasMode = _ctx.getCanvasMode();
309
+
310
+ if (!S.selection || (canvasMode !== "design" && canvasMode !== "edit")) {
311
+ litRender(nothing, view.blockActionBarEl);
312
+ return;
313
+ }
314
+
315
+ const activePanel = _ctx.getActivePanel();
316
+ if (!activePanel) {
317
+ litRender(nothing, view.blockActionBarEl);
318
+ return;
319
+ }
320
+ const el = _ctx.findCanvasElement(S.selection, activePanel.canvas);
321
+ const node = el && getNodeAtPath(S.document, S.selection);
322
+ if (!el || !node) {
323
+ litRender(nothing, view.blockActionBarEl);
324
+ return;
325
+ }
326
+
327
+ const tag = (node.tagName ?? "div").toLowerCase();
328
+ const elRect = el.getBoundingClientRect();
329
+ const topPos = elRect.top < 80 ? elRect.bottom + 4 : elRect.top - 38;
330
+
331
+ // Inline format state
332
+ const inlineEditing = isEditing() || el.contentEditable === "true";
333
+ const actions = getInlineActions(tag) || [];
334
+ const showFormat = inlineEditing && actions.length > 0;
335
+ const activeValues = showFormat
336
+ ? actions.filter((a) => isTagActiveInSelection(a.tag, el)).map((a) => a.tag)
337
+ : [];
338
+
339
+ litRender(
340
+ html`
341
+ <div
342
+ class="block-action-bar"
343
+ style="left:${elRect.left}px; top:${topPos}px"
344
+ @mousedown=${onBarMousedown}
345
+ >
346
+ ${S.selection.length >= 2 ? renderParentSelector() : nothing}
347
+
348
+ <span class="bar-tag">${node.$id || (node.tagName ?? "div")}</span>
349
+
350
+ ${S.selection.length >= 2
351
+ ? html`<span class="bar-drag-handle" title="Drag to reorder">⡇</span>`
352
+ : nothing}
353
+ ${S.selection.length >= 2 ? renderMoveArrows() : nothing}
354
+ ${S.selection.length >= 2 && node.tagName
355
+ ? (() => {
356
+ const isComp =
357
+ node.tagName.includes("-") &&
358
+ componentRegistry.some((/** @type {any} */ c) => c.tagName === node.tagName);
359
+ if (isComp) {
360
+ const comp = componentRegistry.find(
361
+ (/** @type {any} */ c) => c.tagName === node.tagName,
362
+ );
363
+ return html`<sp-action-button
364
+ size="xs"
365
+ quiet
366
+ title="Edit Component"
367
+ @click=${() => _ctx.navigateToComponent(comp.path)}
368
+ ><sp-icon-edit slot="icon" size="xs"></sp-icon-edit
369
+ ></sp-action-button>`;
370
+ }
371
+ return html`<sp-action-button
372
+ size="xs"
373
+ quiet
374
+ title="Convert to Component"
375
+ @click=${() => convertToComponent(S)}
376
+ ><sp-icon-box slot="icon" size="xs"></sp-icon-box
377
+ ></sp-action-button>`;
378
+ })()
379
+ : nothing}
380
+ ${showFormat
381
+ ? html`
382
+ <sp-divider size="s" vertical></sp-divider>
383
+ <sp-action-group
384
+ size="xs"
385
+ compact
386
+ emphasized
387
+ selects="multiple"
388
+ selected=${activeValues.length ? JSON.stringify(activeValues) : nothing}
389
+ >
390
+ ${actions.map(
391
+ (action) => html`
392
+ <sp-action-button
393
+ size="xs"
394
+ value=${action.tag}
395
+ title="${action.label}${action.shortcut ? ` (${action.shortcut})` : ""}"
396
+ @mousedown=${captureSelectionRange}
397
+ @click=${(/** @type {any} */ e) => onFormatClick(e, action)}
398
+ >
399
+ ${formatIconMap[action.icon] ?? nothing}
400
+ </sp-action-button>
401
+ `,
402
+ )}
403
+ </sp-action-group>
404
+ `
405
+ : nothing}
406
+ </div>
407
+ `,
408
+ view.blockActionBarEl,
409
+ );
410
+
411
+ // Post-render side effects
412
+ requestAnimationFrame(() => {
413
+ const bar = view.blockActionBarEl?.firstElementChild;
414
+ if (!bar) return;
415
+ // Clamp to window
416
+ const barRect = bar.getBoundingClientRect();
417
+ if (barRect.right > window.innerWidth) {
418
+ bar.style.left = `${Math.max(0, window.innerWidth - barRect.width)}px`;
419
+ }
420
+ // Attach drag handle
421
+ const currentS = getState();
422
+ if (currentS.selection?.length >= 2) {
423
+ const handle = bar.querySelector(".bar-drag-handle");
424
+ if (handle) {
425
+ if (view.selDragCleanup) {
426
+ view.selDragCleanup();
427
+ view.selDragCleanup = null;
428
+ }
429
+ view.selDragCleanup = draggable({
430
+ element: handle,
431
+ getInitialData: () => ({ type: "tree-node", path: getState().selection }),
432
+ });
433
+ }
434
+ }
435
+ });
436
+ }