terminoir 0.9.0 → 0.10.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.
|
@@ -10,6 +10,8 @@
|
|
|
10
10
|
class: className = '',
|
|
11
11
|
leading,
|
|
12
12
|
trailing,
|
|
13
|
+
expanded,
|
|
14
|
+
details,
|
|
13
15
|
children,
|
|
14
16
|
...rest
|
|
15
17
|
}: {
|
|
@@ -22,6 +24,10 @@
|
|
|
22
24
|
class?: string;
|
|
23
25
|
leading?: Snippet;
|
|
24
26
|
trailing?: Snippet;
|
|
27
|
+
/** For a row that opens `details`: whether they are shown (sets `aria-expanded`). */
|
|
28
|
+
expanded?: boolean;
|
|
29
|
+
/** Content under the row, such as the settings it opens. Shown only while `expanded` is not false. */
|
|
30
|
+
details?: Snippet;
|
|
25
31
|
/** Replaces title/description for custom content. */
|
|
26
32
|
children?: Snippet;
|
|
27
33
|
} & Record<string, unknown> = $props();
|
|
@@ -42,12 +48,15 @@
|
|
|
42
48
|
|
|
43
49
|
<li class={cn('terminoir-list-item', active && 'terminoir-list-item--active', className)} {...rest}>
|
|
44
50
|
{#if onclick}
|
|
45
|
-
<button type="button" class="terminoir-list-item__row terminoir-list-item__button terminoir-focusable" {onclick}>
|
|
51
|
+
<button type="button" class="terminoir-list-item__row terminoir-list-item__button terminoir-focusable" aria-expanded={expanded} {onclick}>
|
|
46
52
|
{@render body()}
|
|
47
53
|
</button>
|
|
48
54
|
{:else}
|
|
49
55
|
<div class="terminoir-list-item__row">{@render body()}</div>
|
|
50
56
|
{/if}
|
|
57
|
+
{#if details && expanded !== false}
|
|
58
|
+
<div class="terminoir-list-item__details">{@render details()}</div>
|
|
59
|
+
{/if}
|
|
51
60
|
</li>
|
|
52
61
|
|
|
53
62
|
<style>
|
|
@@ -103,6 +112,9 @@
|
|
|
103
112
|
text-overflow: ellipsis;
|
|
104
113
|
white-space: nowrap;
|
|
105
114
|
}
|
|
115
|
+
.terminoir-list-item__details {
|
|
116
|
+
padding: var(--space-1) 0.85rem var(--space-4) calc(0.85rem + 1.25rem + var(--space-3));
|
|
117
|
+
}
|
|
106
118
|
.terminoir-list-item__trailing {
|
|
107
119
|
display: inline-flex;
|
|
108
120
|
align-items: center;
|
|
@@ -9,6 +9,10 @@ type $$ComponentProps = {
|
|
|
9
9
|
class?: string;
|
|
10
10
|
leading?: Snippet;
|
|
11
11
|
trailing?: Snippet;
|
|
12
|
+
/** For a row that opens `details`: whether they are shown (sets `aria-expanded`). */
|
|
13
|
+
expanded?: boolean;
|
|
14
|
+
/** Content under the row, such as the settings it opens. Shown only while `expanded` is not false. */
|
|
15
|
+
details?: Snippet;
|
|
12
16
|
/** Replaces title/description for custom content. */
|
|
13
17
|
children?: Snippet;
|
|
14
18
|
} & Record<string, unknown>;
|
|
@@ -13,6 +13,7 @@
|
|
|
13
13
|
import { cn } from '../utils/cn.js';
|
|
14
14
|
import type { PickerOption } from '../utils/componentTypes.js';
|
|
15
15
|
import { moveIndex } from '../utils/listNav.js';
|
|
16
|
+
import { findDropTarget, moveBlock } from '../utils/markdownEditor/blockDrag.js';
|
|
16
17
|
import { matchBlocks, placeholderPlugin, taskListItemView, tidyMarkdown, type BlockItem } from '../utils/markdownEditor/blocks.js';
|
|
17
18
|
import { triggerPlugin, type Trigger } from '../utils/markdownEditor/triggers.js';
|
|
18
19
|
import { wikilink, wikilinkNode } from '../utils/markdownEditor/wikilink.js';
|
|
@@ -21,6 +22,8 @@
|
|
|
21
22
|
type MenuEntry = { id: string; label: string; icon?: string; iconColor?: string; description?: string; choose: () => void };
|
|
22
23
|
|
|
23
24
|
const LINK_SEARCH_DEBOUNCE_MS = 120;
|
|
25
|
+
/** How far left of the text a dragged block can still be dropped (the handle's gutter), and above or below it. */
|
|
26
|
+
const DROP_MARGIN_PX = 72;
|
|
24
27
|
|
|
25
28
|
let {
|
|
26
29
|
value,
|
|
@@ -50,7 +53,12 @@
|
|
|
50
53
|
let host = $state<HTMLDivElement>();
|
|
51
54
|
let handle = $state<HTMLDivElement>();
|
|
52
55
|
let editor: Editor | undefined;
|
|
56
|
+
let ready = $state(false);
|
|
53
57
|
let provider: BlockProvider | undefined;
|
|
58
|
+
/** Position of the block being dragged by its handle. */
|
|
59
|
+
let dragFrom: number | null = null;
|
|
60
|
+
/** Where it would land, as a line in the editor's own coordinates. */
|
|
61
|
+
let dropLine = $state<{ pos: number; top: number; left: number; width: number } | null>(null);
|
|
54
62
|
/** Normalized markdown of what the editor last loaded or emitted; changes equal to it are not edits. */
|
|
55
63
|
let known = '';
|
|
56
64
|
|
|
@@ -142,10 +150,10 @@
|
|
|
142
150
|
onchange?.(tidy);
|
|
143
151
|
}
|
|
144
152
|
|
|
145
|
-
/** Load outside changes, but never under the writer's caret. */
|
|
153
|
+
/** Load outside changes, but never under the writer's caret. Also runs once the editor exists, for a value that changed while it was being created. */
|
|
146
154
|
$effect(() => {
|
|
147
155
|
const next = value;
|
|
148
|
-
if (!
|
|
156
|
+
if (!ready || tidyMarkdown(next) === known) return;
|
|
149
157
|
const focused = act((ctx) => ctx.get(editorViewCtx).hasFocus());
|
|
150
158
|
if (focused) return;
|
|
151
159
|
act(replaceAll(next));
|
|
@@ -166,6 +174,62 @@
|
|
|
166
174
|
});
|
|
167
175
|
}
|
|
168
176
|
|
|
177
|
+
function nearEditor(event: DragEvent): boolean {
|
|
178
|
+
const rect = root!.getBoundingClientRect();
|
|
179
|
+
return (
|
|
180
|
+
event.clientX >= rect.left - DROP_MARGIN_PX &&
|
|
181
|
+
event.clientX <= rect.right &&
|
|
182
|
+
event.clientY >= rect.top - DROP_MARGIN_PX &&
|
|
183
|
+
event.clientY <= rect.bottom + DROP_MARGIN_PX
|
|
184
|
+
);
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/** Blocks land between their siblings, wherever along the line the pointer is (the gutter included). */
|
|
188
|
+
function onBlockDragOver(event: DragEvent) {
|
|
189
|
+
if (dragFrom === null) return;
|
|
190
|
+
if (!nearEditor(event)) {
|
|
191
|
+
dropLine = null;
|
|
192
|
+
return;
|
|
193
|
+
}
|
|
194
|
+
event.preventDefault();
|
|
195
|
+
event.stopPropagation();
|
|
196
|
+
if (event.dataTransfer) event.dataTransfer.dropEffect = 'move';
|
|
197
|
+
const from = dragFrom;
|
|
198
|
+
const target = act((ctx) => findDropTarget(ctx.get(editorViewCtx), from, event.clientY));
|
|
199
|
+
const origin = root!.getBoundingClientRect();
|
|
200
|
+
dropLine = target ? { ...target, top: target.top - origin.top, left: target.left - origin.left } : null;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
function onBlockDrop(event: DragEvent) {
|
|
204
|
+
const from = dragFrom;
|
|
205
|
+
const line = dropLine;
|
|
206
|
+
if (from === null || !line) return;
|
|
207
|
+
event.preventDefault();
|
|
208
|
+
event.stopPropagation();
|
|
209
|
+
act((ctx) => {
|
|
210
|
+
const view = ctx.get(editorViewCtx);
|
|
211
|
+
const tr = moveBlock(view.state, from, line.pos);
|
|
212
|
+
if (tr) view.dispatch(tr.scrollIntoView());
|
|
213
|
+
view.dragging = null;
|
|
214
|
+
view.focus();
|
|
215
|
+
});
|
|
216
|
+
endBlockDrag();
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
function startBlockDrag() {
|
|
220
|
+
dragFrom = provider?.active?.$pos.pos ?? null;
|
|
221
|
+
if (dragFrom === null) return;
|
|
222
|
+
document.addEventListener('dragover', onBlockDragOver, true);
|
|
223
|
+
document.addEventListener('drop', onBlockDrop, true);
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
function endBlockDrag() {
|
|
227
|
+
dragFrom = null;
|
|
228
|
+
dropLine = null;
|
|
229
|
+
document.removeEventListener('dragover', onBlockDragOver, true);
|
|
230
|
+
document.removeEventListener('drop', onBlockDrop, true);
|
|
231
|
+
}
|
|
232
|
+
|
|
169
233
|
function onHostClick(event: MouseEvent) {
|
|
170
234
|
const link = (event.target as HTMLElement).closest<HTMLElement>('[data-wikilink]');
|
|
171
235
|
if (link && onlinkopen) onlinkopen(link.dataset.wikilink ?? '');
|
|
@@ -204,10 +268,12 @@
|
|
|
204
268
|
known = tidyMarkdown(act(getMarkdown()) ?? value);
|
|
205
269
|
provider = new BlockProvider({ ctx: created.ctx, content: handle!, root, getOffset: () => 6 });
|
|
206
270
|
provider.update();
|
|
271
|
+
ready = true;
|
|
207
272
|
});
|
|
208
273
|
|
|
209
274
|
return () => {
|
|
210
275
|
disposed = true;
|
|
276
|
+
endBlockDrag();
|
|
211
277
|
provider?.destroy();
|
|
212
278
|
void editor?.destroy();
|
|
213
279
|
};
|
|
@@ -218,12 +284,15 @@
|
|
|
218
284
|
<!-- Link chips are also reachable as text; the click only adds a shortcut to open them. -->
|
|
219
285
|
<!-- svelte-ignore a11y_click_events_have_key_events, a11y_no_static_element_interactions -->
|
|
220
286
|
<div bind:this={host} onclick={onHostClick}></div>
|
|
221
|
-
<div class="terminoir-md__handle" bind:this={handle}>
|
|
287
|
+
<div class="terminoir-md__handle" bind:this={handle} ondragstart={startBlockDrag} ondragend={endBlockDrag} role="presentation">
|
|
222
288
|
<button type="button" class="terminoir-md__handle-button" aria-label="Add block below" onmousedown={(e) => e.preventDefault()} onclick={addBlockBelow}>
|
|
223
289
|
<NavIcon name="plus" />
|
|
224
290
|
</button>
|
|
225
291
|
<span class="terminoir-md__handle-grip" aria-hidden="true" title="Drag to move"><NavIcon name="grip-vertical" /></span>
|
|
226
292
|
</div>
|
|
293
|
+
{#if dropLine}
|
|
294
|
+
<div class="terminoir-md__drop-line" aria-hidden="true" style:top={`${dropLine.top}px`} style:left={`${dropLine.left}px`} style:width={`${dropLine.width}px`}></div>
|
|
295
|
+
{/if}
|
|
227
296
|
</div>
|
|
228
297
|
|
|
229
298
|
{#if trigger && (entries.length || trigger.kind === 'link')}
|
|
@@ -336,6 +405,15 @@
|
|
|
336
405
|
.terminoir-md__handle-button:hover,
|
|
337
406
|
.terminoir-md__handle-grip:hover { background: color-mix(in srgb, var(--foreground) 8%, transparent); color: var(--foreground); }
|
|
338
407
|
|
|
408
|
+
.terminoir-md__drop-line {
|
|
409
|
+
position: absolute;
|
|
410
|
+
height: 2px;
|
|
411
|
+
margin-top: -1px;
|
|
412
|
+
border-radius: 1px;
|
|
413
|
+
background: var(--primary);
|
|
414
|
+
pointer-events: none;
|
|
415
|
+
}
|
|
416
|
+
|
|
339
417
|
.terminoir-md__menu {
|
|
340
418
|
position: fixed;
|
|
341
419
|
z-index: 60;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/** Moving a block by its drag handle: where it lands, and the transaction that moves it. */
|
|
2
|
+
import type { EditorState, Transaction } from '@milkdown/kit/prose/state';
|
|
3
|
+
import type { EditorView } from '@milkdown/kit/prose/view';
|
|
4
|
+
type Span = {
|
|
5
|
+
top: number;
|
|
6
|
+
bottom: number;
|
|
7
|
+
};
|
|
8
|
+
/** A place a dragged block can land: document position, and the line drawn there. */
|
|
9
|
+
export type DropTarget = {
|
|
10
|
+
pos: number;
|
|
11
|
+
top: number;
|
|
12
|
+
left: number;
|
|
13
|
+
width: number;
|
|
14
|
+
};
|
|
15
|
+
/** Index of the gap (0 = before the first block, n = after the last) nearest to `y`. */
|
|
16
|
+
export declare function dropGap(blocks: readonly Span[], y: number): number;
|
|
17
|
+
/** Moves the node at `from` to document position `to`; null when that would leave it in place. */
|
|
18
|
+
export declare function moveBlock(state: EditorState, from: number, to: number): Transaction | null;
|
|
19
|
+
/** The gap among the dragged block's siblings closest to `y`, in viewport coordinates. */
|
|
20
|
+
export declare function findDropTarget(view: EditorView, from: number, y: number): DropTarget | null;
|
|
21
|
+
export {};
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/** Index of the gap (0 = before the first block, n = after the last) nearest to `y`. */
|
|
2
|
+
export function dropGap(blocks, y) {
|
|
3
|
+
const index = blocks.findIndex((block) => y < (block.top + block.bottom) / 2);
|
|
4
|
+
return index === -1 ? blocks.length : index;
|
|
5
|
+
}
|
|
6
|
+
/** Moves the node at `from` to document position `to`; null when that would leave it in place. */
|
|
7
|
+
export function moveBlock(state, from, to) {
|
|
8
|
+
const node = state.doc.nodeAt(from);
|
|
9
|
+
if (!node || (to >= from && to <= from + node.nodeSize))
|
|
10
|
+
return null;
|
|
11
|
+
const tr = state.tr.delete(from, from + node.nodeSize);
|
|
12
|
+
return tr.insert(tr.mapping.map(to), node);
|
|
13
|
+
}
|
|
14
|
+
/** The gap among the dragged block's siblings closest to `y`, in viewport coordinates. */
|
|
15
|
+
export function findDropTarget(view, from, y) {
|
|
16
|
+
const $from = view.state.doc.resolve(from);
|
|
17
|
+
const start = $from.start();
|
|
18
|
+
const siblings = [];
|
|
19
|
+
$from.parent.forEach((_, offset) => {
|
|
20
|
+
const dom = view.nodeDOM(start + offset);
|
|
21
|
+
if (dom instanceof HTMLElement)
|
|
22
|
+
siblings.push({ pos: start + offset, rect: dom.getBoundingClientRect() });
|
|
23
|
+
});
|
|
24
|
+
if (!siblings.length)
|
|
25
|
+
return null;
|
|
26
|
+
const gap = dropGap(siblings.map((s) => s.rect), y);
|
|
27
|
+
const { left, width } = siblings[0].rect;
|
|
28
|
+
if (gap < siblings.length) {
|
|
29
|
+
const { pos, rect } = siblings[gap];
|
|
30
|
+
const above = siblings[gap - 1]?.rect.bottom ?? rect.top;
|
|
31
|
+
return { pos, top: (above + rect.top) / 2, left, width };
|
|
32
|
+
}
|
|
33
|
+
return { pos: start + $from.parent.content.size, top: siblings[siblings.length - 1].rect.bottom, left, width };
|
|
34
|
+
}
|