svelte-tiler 0.0.1
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/LICENSE +7 -0
- package/README.md +17 -0
- package/dist/context.svelte.d.ts +50 -0
- package/dist/context.svelte.js +57 -0
- package/dist/dnd.d.ts +6 -0
- package/dist/dnd.js +8 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +5 -0
- package/dist/model.d.ts +20 -0
- package/dist/model.js +1 -0
- package/dist/panel.svelte +14 -0
- package/dist/panel.svelte.d.ts +7 -0
- package/dist/render.svelte +35 -0
- package/dist/render.svelte.d.ts +9 -0
- package/dist/shared/constraints.d.ts +19 -0
- package/dist/shared/constraints.js +26 -0
- package/dist/shared/dnd.svelte.d.ts +74 -0
- package/dist/shared/dnd.svelte.js +247 -0
- package/dist/shared/math.d.ts +1 -0
- package/dist/shared/math.js +3 -0
- package/dist/shared/registry.d.ts +9 -0
- package/dist/shared/registry.js +14 -0
- package/dist/shared/spatial.d.ts +7 -0
- package/dist/shared/spatial.js +22 -0
- package/dist/tiler.svelte +18 -0
- package/dist/tiler.svelte.d.ts +8 -0
- package/dist/tiles/leaf.svelte +43 -0
- package/dist/tiles/leaf.svelte.d.ts +17 -0
- package/dist/tiles/split.svelte +334 -0
- package/dist/tiles/split.svelte.d.ts +42 -0
- package/dist/tiles/tabs.svelte +389 -0
- package/dist/tiles/tabs.svelte.d.ts +53 -0
- package/package.json +100 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
Copyright (c) 2026 Roman Krasilnikov
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
4
|
+
|
|
5
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
6
|
+
|
|
7
|
+
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
# svelte-tiler
|
|
2
|
+
|
|
3
|
+
A small, unstyled library for building tiling user interfaces.
|
|
4
|
+
|
|
5
|
+
```sh
|
|
6
|
+
npm i svelte-tiler
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
**Features**:
|
|
10
|
+
|
|
11
|
+
- Serializable state
|
|
12
|
+
- Type-safe model extension
|
|
13
|
+
- No external dependencies
|
|
14
|
+
|
|
15
|
+
## License
|
|
16
|
+
|
|
17
|
+
MIT
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { DndContext } from './shared/dnd.svelte.ts';
|
|
2
|
+
import type { Tile, TileComponent, Tiles, TileType } from './model.ts';
|
|
3
|
+
export declare const getTilerContext: () => TilerContext, setTilerContext: (context: TilerContext) => TilerContext;
|
|
4
|
+
export interface TileDefinition<T extends TileType> {
|
|
5
|
+
default: TileComponent<T>;
|
|
6
|
+
onRemoveChild: (ctx: TilerContext, tile: Tiles[T], index: number) => void;
|
|
7
|
+
onClear: (ctx: TilerContext, tile: Tiles[T]) => void;
|
|
8
|
+
}
|
|
9
|
+
export type TileDefinitions = {
|
|
10
|
+
[T in TileType]: TileDefinition<T>;
|
|
11
|
+
};
|
|
12
|
+
export type TileEffects = {
|
|
13
|
+
[T in TileType]?: (tile: Tiles[T]) => void | (() => void);
|
|
14
|
+
};
|
|
15
|
+
export interface TilerContextOptions {
|
|
16
|
+
tiles: TileDefinitions;
|
|
17
|
+
parents?: WeakMap<Tile, Tile>;
|
|
18
|
+
dnd?: DndContext<Tile>;
|
|
19
|
+
effects?: TileEffects;
|
|
20
|
+
}
|
|
21
|
+
export declare class TilerContext {
|
|
22
|
+
protected definitions: TileDefinitions;
|
|
23
|
+
protected parents: WeakMap<Tile, Tile>;
|
|
24
|
+
protected effects: TileEffects;
|
|
25
|
+
protected updateRootFn: ((tile: Tile) => void) | undefined;
|
|
26
|
+
readonly dnd: DndContext<Tile>;
|
|
27
|
+
constructor(options: TilerContextOptions);
|
|
28
|
+
registerParent(tile: Tile, parent: Tile): void;
|
|
29
|
+
setUpdateRoot(tile: Tile, update: (tile: Tile) => void): void;
|
|
30
|
+
getTileEffect(tile: Tile): ((tile: import("./model.ts").TileBase<"leaf"> & {
|
|
31
|
+
name: string;
|
|
32
|
+
}) => void | (() => void)) | ((tile: import("./model.ts").TileBase<"split"> & {
|
|
33
|
+
constraints: Array<import("./shared/constraints.ts").Constraint[]>;
|
|
34
|
+
weights: number[];
|
|
35
|
+
direction: import("./shared/spatial.ts").Direction;
|
|
36
|
+
resizer?: string;
|
|
37
|
+
gapPx: number;
|
|
38
|
+
}) => void | (() => void)) | ((tile: import("./model.ts").TileBase<"tabs"> & {
|
|
39
|
+
titles: string[];
|
|
40
|
+
selectedTab: number;
|
|
41
|
+
headersDirection: import("./tiles/tabs.svelte.ts").HeadersDirection;
|
|
42
|
+
actions?: string;
|
|
43
|
+
tabHeader?: string;
|
|
44
|
+
empty?: string;
|
|
45
|
+
}) => void | (() => void)) | undefined;
|
|
46
|
+
getTileComponent(tile: Tile): TileComponent<"leaf"> | TileComponent<"split"> | TileComponent<"tabs">;
|
|
47
|
+
replaceWith(tile: Tile, replace: Tile): void;
|
|
48
|
+
removeChild(tile: Tile, index: number): void;
|
|
49
|
+
destroy(tile: Tile): void;
|
|
50
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { createContext } from 'svelte';
|
|
2
|
+
import { DndContext } from "./shared/dnd.svelte.js";
|
|
3
|
+
export const [getTilerContext, setTilerContext] = createContext();
|
|
4
|
+
export class TilerContext {
|
|
5
|
+
definitions;
|
|
6
|
+
parents;
|
|
7
|
+
effects;
|
|
8
|
+
updateRootFn;
|
|
9
|
+
dnd;
|
|
10
|
+
constructor(options) {
|
|
11
|
+
this.definitions = $derived(options.tiles);
|
|
12
|
+
this.dnd = $derived(options.dnd ?? new DndContext());
|
|
13
|
+
this.parents = $derived(options.parents ?? new WeakMap());
|
|
14
|
+
this.effects = $derived(options.effects ?? {});
|
|
15
|
+
}
|
|
16
|
+
registerParent(tile, parent) {
|
|
17
|
+
this.parents.set(tile, parent);
|
|
18
|
+
}
|
|
19
|
+
setUpdateRoot(tile, update) {
|
|
20
|
+
this.parents.delete(tile);
|
|
21
|
+
this.updateRootFn = update;
|
|
22
|
+
}
|
|
23
|
+
getTileEffect(tile) {
|
|
24
|
+
return this.effects[tile.type];
|
|
25
|
+
}
|
|
26
|
+
getTileComponent(tile) {
|
|
27
|
+
return this.definitions[tile.type].default;
|
|
28
|
+
}
|
|
29
|
+
replaceWith(tile, replace) {
|
|
30
|
+
const parent = this.parents.get(tile);
|
|
31
|
+
if (parent) {
|
|
32
|
+
const index = parent.children.findIndex((c) => c.id === tile.id);
|
|
33
|
+
if (index < 0) {
|
|
34
|
+
throw new Error(`Invalid parent for ${JSON.stringify($state.snapshot(tile))} tile`);
|
|
35
|
+
}
|
|
36
|
+
parent.children[index] = replace;
|
|
37
|
+
}
|
|
38
|
+
else {
|
|
39
|
+
this.updateRootFn?.(replace);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
removeChild(tile, index) {
|
|
43
|
+
return this.definitions[tile.type].onRemoveChild(this, tile, index);
|
|
44
|
+
}
|
|
45
|
+
destroy(tile) {
|
|
46
|
+
const parent = this.parents.get(tile);
|
|
47
|
+
if (parent === undefined) {
|
|
48
|
+
this.definitions[tile.type].onClear(this, tile);
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
const index = parent.children.findIndex((c) => c.id === tile.id);
|
|
52
|
+
if (index < 0) {
|
|
53
|
+
throw new Error(`Invalid parent for ${JSON.stringify($state.snapshot(tile))} tile`);
|
|
54
|
+
}
|
|
55
|
+
this.removeChild(parent, index);
|
|
56
|
+
}
|
|
57
|
+
}
|
package/dist/dnd.d.ts
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { DndContext, Droppable } from './shared/dnd.svelte.js';
|
|
2
|
+
import type { Tile } from './model.js';
|
|
3
|
+
export declare class TileDroppable<T extends Tile> extends Droppable<Tile, T> {
|
|
4
|
+
readonly targetTileId: string;
|
|
5
|
+
constructor(ctx: DndContext<Tile>, targetTileId: string);
|
|
6
|
+
}
|
package/dist/dnd.js
ADDED
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
package/dist/model.d.ts
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { Component, Snippet } from 'svelte';
|
|
2
|
+
export interface TileBase<T extends TileType> {
|
|
3
|
+
id: string;
|
|
4
|
+
type: T;
|
|
5
|
+
children: Tile[];
|
|
6
|
+
}
|
|
7
|
+
export interface TileRegistry {
|
|
8
|
+
}
|
|
9
|
+
export type TileType = keyof TileRegistry;
|
|
10
|
+
export type Tiles = {
|
|
11
|
+
[T in TileType]: TileBase<T> & TileRegistry[T];
|
|
12
|
+
};
|
|
13
|
+
export type Tile = Tiles[TileType];
|
|
14
|
+
export type TileProps<T extends TileType> = {
|
|
15
|
+
tile: Tiles[T];
|
|
16
|
+
parent: Tile | undefined;
|
|
17
|
+
index: number;
|
|
18
|
+
child: Snippet<[number]>;
|
|
19
|
+
};
|
|
20
|
+
export type TileComponent<T extends TileType> = Component<TileProps<T>, {}, 'tile' | 'parent'>;
|
package/dist/model.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import Render from './render.svelte';
|
|
3
|
+
import type { Tile } from './model.js';
|
|
4
|
+
|
|
5
|
+
let { layout = $bindable() }: { layout: Tile | undefined } = $props();
|
|
6
|
+
</script>
|
|
7
|
+
|
|
8
|
+
{#if layout}
|
|
9
|
+
<Render
|
|
10
|
+
bind:tile={layout}
|
|
11
|
+
bind:parent={() => undefined, (t) => (layout = t)}
|
|
12
|
+
index={0}
|
|
13
|
+
/>
|
|
14
|
+
{/if}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import { getTilerContext } from './context.svelte.js';
|
|
3
|
+
import type { Tile } from './model.js';
|
|
4
|
+
import Self from './render.svelte';
|
|
5
|
+
|
|
6
|
+
let {
|
|
7
|
+
tile = $bindable(),
|
|
8
|
+
parent = $bindable(),
|
|
9
|
+
index,
|
|
10
|
+
}: {
|
|
11
|
+
tile: Tile;
|
|
12
|
+
parent: Tile | undefined;
|
|
13
|
+
index: number;
|
|
14
|
+
} = $props();
|
|
15
|
+
|
|
16
|
+
const ctx = getTilerContext();
|
|
17
|
+
|
|
18
|
+
$effect(() => {
|
|
19
|
+
if (parent) {
|
|
20
|
+
ctx.registerParent(tile, parent);
|
|
21
|
+
} else {
|
|
22
|
+
ctx.setUpdateRoot(tile, (t) => (parent = t));
|
|
23
|
+
}
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
$effect(() => ctx.getTileEffect(tile)?.(tile as never));
|
|
27
|
+
|
|
28
|
+
const TileComponent = $derived(ctx.getTileComponent(tile));
|
|
29
|
+
</script>
|
|
30
|
+
|
|
31
|
+
{#snippet child(index: number)}
|
|
32
|
+
<Self bind:parent={tile} bind:tile={tile.children[index]} {index} />
|
|
33
|
+
{/snippet}
|
|
34
|
+
|
|
35
|
+
<TileComponent bind:parent bind:tile={tile as never} {index} {child} />
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { Tile } from './model.ts';
|
|
2
|
+
type $$ComponentProps = {
|
|
3
|
+
tile: Tile;
|
|
4
|
+
parent: Tile | undefined;
|
|
5
|
+
index: number;
|
|
6
|
+
};
|
|
7
|
+
declare const Render: import("svelte").Component<$$ComponentProps, {}, "tile" | "parent">;
|
|
8
|
+
type Render = ReturnType<typeof Render>;
|
|
9
|
+
export default Render;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
export type ConstraintUnit = 'px' | 'weight' | '%';
|
|
2
|
+
export type ConstraintType = 'maxSize' | 'minSize';
|
|
3
|
+
export interface Constraint {
|
|
4
|
+
type: ConstraintType;
|
|
5
|
+
unit: ConstraintUnit;
|
|
6
|
+
value: number;
|
|
7
|
+
}
|
|
8
|
+
export type NormalizedConstraints = Record<ConstraintType, number>;
|
|
9
|
+
interface Sizes {
|
|
10
|
+
totalWeight: number;
|
|
11
|
+
totalSizePx: number;
|
|
12
|
+
totalSizePercent: number;
|
|
13
|
+
}
|
|
14
|
+
export interface NormalizeOptions extends Sizes {
|
|
15
|
+
constraints: Constraint[];
|
|
16
|
+
targetUnit: ConstraintUnit;
|
|
17
|
+
}
|
|
18
|
+
export declare function normalize(options: NormalizeOptions): NormalizedConstraints;
|
|
19
|
+
export {};
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
const UNIT_TO_TOTAL_SIZE = {
|
|
2
|
+
px: 'totalSizePx',
|
|
3
|
+
'%': 'totalSizePercent',
|
|
4
|
+
weight: 'totalWeight',
|
|
5
|
+
};
|
|
6
|
+
const CONSTRAINT_COMBINATOR = {
|
|
7
|
+
maxSize: Math.min,
|
|
8
|
+
minSize: Math.max,
|
|
9
|
+
};
|
|
10
|
+
// TODO: Handle 0 total sizes
|
|
11
|
+
export function normalize(options) {
|
|
12
|
+
const result = {
|
|
13
|
+
maxSize: Infinity,
|
|
14
|
+
minSize: 0,
|
|
15
|
+
};
|
|
16
|
+
const targetTotal = options[UNIT_TO_TOTAL_SIZE[options.targetUnit]];
|
|
17
|
+
for (const constraint of options.constraints) {
|
|
18
|
+
// (constraint.value * targetTotal) / sourceTotal;
|
|
19
|
+
const normalizedValue = options.targetUnit === constraint.unit
|
|
20
|
+
? constraint.value
|
|
21
|
+
: (constraint.value * targetTotal) /
|
|
22
|
+
options[UNIT_TO_TOTAL_SIZE[constraint.unit]];
|
|
23
|
+
result[constraint.type] = CONSTRAINT_COMBINATOR[constraint.type](result[constraint.type], normalizedValue);
|
|
24
|
+
}
|
|
25
|
+
return result;
|
|
26
|
+
}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import type { MutableRegistry } from './registry.ts';
|
|
2
|
+
declare const ON_ENTER: unique symbol;
|
|
3
|
+
declare const ON_MOVE: unique symbol;
|
|
4
|
+
declare const ON_LEAVE: unique symbol;
|
|
5
|
+
declare const ON_DROP: unique symbol;
|
|
6
|
+
export type FeedbackFactory = (e: PointerEvent, el: HTMLElement) => {
|
|
7
|
+
onMove: (e: PointerEvent) => void;
|
|
8
|
+
onStop: (e: StopEvent) => void;
|
|
9
|
+
} | undefined;
|
|
10
|
+
export interface DndContextOptions {
|
|
11
|
+
feedback?: FeedbackFactory;
|
|
12
|
+
}
|
|
13
|
+
export declare class DndContext<D = unknown> {
|
|
14
|
+
#private;
|
|
15
|
+
readonly feedback: FeedbackFactory;
|
|
16
|
+
constructor(options?: DndContextOptions);
|
|
17
|
+
sourceId: string | undefined;
|
|
18
|
+
targetId: string | undefined;
|
|
19
|
+
get droppables(): MutableRegistry<string, Droppable<D, any> | undefined>;
|
|
20
|
+
findDroppable(x: number, y: number, draggable: Draggable<D>): Droppable<D, any> | undefined;
|
|
21
|
+
}
|
|
22
|
+
export type StopReason = 'drop' | 'cancel';
|
|
23
|
+
export interface StopEvent {
|
|
24
|
+
reason: StopReason;
|
|
25
|
+
}
|
|
26
|
+
export interface DraggableOptions<D> {
|
|
27
|
+
data?: D;
|
|
28
|
+
}
|
|
29
|
+
export declare class Draggable<D = unknown> {
|
|
30
|
+
#private;
|
|
31
|
+
protected readonly ctx: DndContext<D>;
|
|
32
|
+
protected readonly options: DraggableOptions<D>;
|
|
33
|
+
readonly id: `${string}-${string}-${string}-${string}-${string}`;
|
|
34
|
+
constructor(ctx: DndContext<D>, options?: DraggableOptions<D>);
|
|
35
|
+
get isDragged(): boolean;
|
|
36
|
+
get data(): D | undefined;
|
|
37
|
+
register(el: HTMLElement): () => void;
|
|
38
|
+
registerHandel(el: HTMLElement): () => void;
|
|
39
|
+
[Symbol.dispose](): void;
|
|
40
|
+
protected onStart(_e: PointerEvent, _el: HTMLElement): void;
|
|
41
|
+
protected onMove(_e: PointerEvent): void;
|
|
42
|
+
protected onStop(_e: StopEvent): void;
|
|
43
|
+
protected addEventHandlers(el: HTMLElement): void;
|
|
44
|
+
protected pointerDownHandler(event: PointerEvent & {
|
|
45
|
+
currentTarget: HTMLElement;
|
|
46
|
+
}): void;
|
|
47
|
+
}
|
|
48
|
+
export declare class Droppable<D = unknown, T extends D = D> {
|
|
49
|
+
#private;
|
|
50
|
+
protected readonly ctx: DndContext<D>;
|
|
51
|
+
readonly id: `${string}-${string}-${string}-${string}-${string}`;
|
|
52
|
+
constructor(ctx: DndContext<D>);
|
|
53
|
+
get element(): HTMLElement | undefined;
|
|
54
|
+
get isOver(): boolean;
|
|
55
|
+
[Symbol.dispose](): void;
|
|
56
|
+
register(el: HTMLElement): () => void;
|
|
57
|
+
accepts(draggable: Draggable<D>): draggable is Draggable<T>;
|
|
58
|
+
[ON_ENTER](): void;
|
|
59
|
+
[ON_MOVE](e: PointerEvent): void;
|
|
60
|
+
[ON_LEAVE](): void;
|
|
61
|
+
[ON_DROP](data: T, draggable: Draggable<T>): void;
|
|
62
|
+
protected onEnter(): void;
|
|
63
|
+
protected onMove(_e: PointerEvent): void;
|
|
64
|
+
protected onLeave(): void;
|
|
65
|
+
protected onDrop(_data: T, _draggable: Draggable<T>): void;
|
|
66
|
+
}
|
|
67
|
+
export declare class ClonedGhost {
|
|
68
|
+
#private;
|
|
69
|
+
constructor(el: HTMLElement, e: PointerEvent);
|
|
70
|
+
attach(root: ShadowRoot | Document | Node): this;
|
|
71
|
+
onMove(e: PointerEvent): void;
|
|
72
|
+
onStop(): void;
|
|
73
|
+
}
|
|
74
|
+
export {};
|
|
@@ -0,0 +1,247 @@
|
|
|
1
|
+
import { flushSync } from 'svelte';
|
|
2
|
+
import { SvelteMap } from 'svelte/reactivity';
|
|
3
|
+
import { on } from 'svelte/events';
|
|
4
|
+
const ON_ENTER = Symbol('on-enter-key');
|
|
5
|
+
const ON_MOVE = Symbol('on-move-key');
|
|
6
|
+
const ON_LEAVE = Symbol('on-leave-key');
|
|
7
|
+
const ON_DROP = Symbol('on-drop-key');
|
|
8
|
+
export class DndContext {
|
|
9
|
+
feedback;
|
|
10
|
+
constructor(options = {}) {
|
|
11
|
+
this.feedback = options.feedback ?? (() => undefined);
|
|
12
|
+
}
|
|
13
|
+
#droppables = new SvelteMap();
|
|
14
|
+
sourceId = $state.raw();
|
|
15
|
+
targetId = $state.raw();
|
|
16
|
+
get droppables() {
|
|
17
|
+
return this.#droppables;
|
|
18
|
+
}
|
|
19
|
+
findDroppable(x, y, draggable) {
|
|
20
|
+
for (const d of this.#droppables.values()) {
|
|
21
|
+
if (d.element === undefined) {
|
|
22
|
+
continue;
|
|
23
|
+
}
|
|
24
|
+
const r = d.element.getBoundingClientRect();
|
|
25
|
+
if (x < r.left || x > r.right || y < r.top || y > r.bottom) {
|
|
26
|
+
continue;
|
|
27
|
+
}
|
|
28
|
+
if (d.accepts(draggable)) {
|
|
29
|
+
return d;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
export class Draggable {
|
|
35
|
+
ctx;
|
|
36
|
+
options;
|
|
37
|
+
#disposePointerDownHandler;
|
|
38
|
+
#disposeClickHandler;
|
|
39
|
+
#didDrag = false;
|
|
40
|
+
#baseElement;
|
|
41
|
+
#hasHandel = false;
|
|
42
|
+
id = crypto.randomUUID();
|
|
43
|
+
constructor(ctx, options = {}) {
|
|
44
|
+
this.ctx = ctx;
|
|
45
|
+
this.options = options;
|
|
46
|
+
this.register = this.register.bind(this);
|
|
47
|
+
this.registerHandel = this.registerHandel.bind(this);
|
|
48
|
+
}
|
|
49
|
+
get isDragged() {
|
|
50
|
+
return this.ctx.sourceId === this.id;
|
|
51
|
+
}
|
|
52
|
+
get data() {
|
|
53
|
+
return this.options.data;
|
|
54
|
+
}
|
|
55
|
+
register(el) {
|
|
56
|
+
if (!this.#hasHandel) {
|
|
57
|
+
this[Symbol.dispose]();
|
|
58
|
+
this.addEventHandlers(el);
|
|
59
|
+
}
|
|
60
|
+
this.#baseElement = el;
|
|
61
|
+
return () => {
|
|
62
|
+
this.#baseElement = undefined;
|
|
63
|
+
if (!this.#hasHandel) {
|
|
64
|
+
this[Symbol.dispose]();
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
registerHandel(el) {
|
|
69
|
+
this[Symbol.dispose]();
|
|
70
|
+
this.addEventHandlers(el);
|
|
71
|
+
this.#hasHandel = true;
|
|
72
|
+
return () => {
|
|
73
|
+
this.#hasHandel = false;
|
|
74
|
+
this[Symbol.dispose]();
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
[Symbol.dispose]() {
|
|
78
|
+
this.#disposeClickHandler?.();
|
|
79
|
+
this.#disposePointerDownHandler?.();
|
|
80
|
+
}
|
|
81
|
+
onStart(_e, _el) { }
|
|
82
|
+
onMove(_e) { }
|
|
83
|
+
onStop(_e) { }
|
|
84
|
+
addEventHandlers(el) {
|
|
85
|
+
this.#disposePointerDownHandler = on(el, 'pointerdown', (e) => this.pointerDownHandler(e));
|
|
86
|
+
this.#disposeClickHandler = on(el, 'click', (e) => {
|
|
87
|
+
if (!this.#didDrag) {
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
e.stopImmediatePropagation();
|
|
91
|
+
e.preventDefault();
|
|
92
|
+
this.#didDrag = false;
|
|
93
|
+
}, { capture: true });
|
|
94
|
+
}
|
|
95
|
+
// NOTE: I believe this logic should be in `DndContext`, but it is difficult to
|
|
96
|
+
// separate and there is no specific reason to do so right now.
|
|
97
|
+
pointerDownHandler(event) {
|
|
98
|
+
if (event.button !== 0 ||
|
|
99
|
+
!event.isPrimary ||
|
|
100
|
+
event.composedPath().some((n) => n instanceof HTMLButtonElement)) {
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
const el = event.currentTarget;
|
|
104
|
+
el.setPointerCapture(event.pointerId);
|
|
105
|
+
const abortController = new AbortController();
|
|
106
|
+
let feedback;
|
|
107
|
+
let activeDroppable;
|
|
108
|
+
const handleMove = (e) => {
|
|
109
|
+
if (e.pointerId !== event.pointerId) {
|
|
110
|
+
return;
|
|
111
|
+
}
|
|
112
|
+
if (!this.#didDrag) {
|
|
113
|
+
const dx = e.clientX - event.clientX;
|
|
114
|
+
const dy = e.clientY - event.clientY;
|
|
115
|
+
if (dx * dx + dy * dy < 16) {
|
|
116
|
+
return;
|
|
117
|
+
}
|
|
118
|
+
this.#didDrag = true;
|
|
119
|
+
this.ctx.sourceId = this.id;
|
|
120
|
+
feedback = this.ctx.feedback(e, this.#baseElement ?? el);
|
|
121
|
+
this.onStart(e, el);
|
|
122
|
+
}
|
|
123
|
+
this.onMove(e);
|
|
124
|
+
feedback?.onMove(e);
|
|
125
|
+
const nextDroppable = this.ctx.findDroppable(e.clientX, e.clientY, this);
|
|
126
|
+
if (activeDroppable !== nextDroppable) {
|
|
127
|
+
activeDroppable?.[ON_LEAVE]();
|
|
128
|
+
nextDroppable?.[ON_ENTER]();
|
|
129
|
+
activeDroppable = nextDroppable;
|
|
130
|
+
this.ctx.targetId = activeDroppable?.id;
|
|
131
|
+
}
|
|
132
|
+
activeDroppable?.[ON_MOVE](e);
|
|
133
|
+
};
|
|
134
|
+
const handleStop = (ev) => {
|
|
135
|
+
el.releasePointerCapture(event.pointerId);
|
|
136
|
+
abortController.abort();
|
|
137
|
+
if (!this.#didDrag) {
|
|
138
|
+
return;
|
|
139
|
+
}
|
|
140
|
+
const snap = ev.reason === 'drop' ? $state.snapshot(this.options.data) : undefined;
|
|
141
|
+
activeDroppable?.[ON_LEAVE]();
|
|
142
|
+
feedback?.onStop(ev);
|
|
143
|
+
this.onStop(ev);
|
|
144
|
+
if (ev.reason === 'drop') {
|
|
145
|
+
flushSync(() => {
|
|
146
|
+
activeDroppable?.[ON_DROP](snap, this);
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
this.ctx.targetId = undefined;
|
|
150
|
+
this.ctx.sourceId = undefined;
|
|
151
|
+
};
|
|
152
|
+
function onKeydown(e) {
|
|
153
|
+
if (e.key === 'Escape') {
|
|
154
|
+
handleStop({ reason: 'cancel' });
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
window.addEventListener('pointermove', handleMove, abortController);
|
|
158
|
+
window.addEventListener('pointerup', (e) => {
|
|
159
|
+
if (e.pointerId !== event.pointerId) {
|
|
160
|
+
return;
|
|
161
|
+
}
|
|
162
|
+
handleStop({
|
|
163
|
+
reason: activeDroppable === undefined ? 'cancel' : 'drop',
|
|
164
|
+
});
|
|
165
|
+
}, abortController);
|
|
166
|
+
window.addEventListener('keydown', onKeydown, abortController);
|
|
167
|
+
window.addEventListener('contextmenu', () => handleStop({ reason: 'cancel' }), abortController);
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
export class Droppable {
|
|
171
|
+
ctx;
|
|
172
|
+
#element = $state.raw();
|
|
173
|
+
id = crypto.randomUUID();
|
|
174
|
+
constructor(ctx) {
|
|
175
|
+
this.ctx = ctx;
|
|
176
|
+
this.register = this.register.bind(this);
|
|
177
|
+
}
|
|
178
|
+
get element() {
|
|
179
|
+
return this.#element;
|
|
180
|
+
}
|
|
181
|
+
get isOver() {
|
|
182
|
+
return this.ctx.targetId === this.id;
|
|
183
|
+
}
|
|
184
|
+
[Symbol.dispose]() {
|
|
185
|
+
this.#element = undefined;
|
|
186
|
+
this.ctx.droppables.delete(this.id);
|
|
187
|
+
}
|
|
188
|
+
register(el) {
|
|
189
|
+
this[Symbol.dispose]();
|
|
190
|
+
this.ctx.droppables.set(this.id, this);
|
|
191
|
+
this.#element = el;
|
|
192
|
+
return () => {
|
|
193
|
+
this[Symbol.dispose]();
|
|
194
|
+
};
|
|
195
|
+
}
|
|
196
|
+
accepts(draggable) {
|
|
197
|
+
return true;
|
|
198
|
+
}
|
|
199
|
+
[ON_ENTER]() {
|
|
200
|
+
this.onEnter();
|
|
201
|
+
}
|
|
202
|
+
[ON_MOVE](e) {
|
|
203
|
+
this.onMove(e);
|
|
204
|
+
}
|
|
205
|
+
[ON_LEAVE]() {
|
|
206
|
+
this.onLeave();
|
|
207
|
+
}
|
|
208
|
+
[ON_DROP](data, draggable) {
|
|
209
|
+
this.onDrop(data, draggable);
|
|
210
|
+
}
|
|
211
|
+
onEnter() { }
|
|
212
|
+
onMove(_e) { }
|
|
213
|
+
onLeave() { }
|
|
214
|
+
onDrop(_data, _draggable) { }
|
|
215
|
+
}
|
|
216
|
+
export class ClonedGhost {
|
|
217
|
+
#element;
|
|
218
|
+
#offsetX;
|
|
219
|
+
#offsetY;
|
|
220
|
+
constructor(el, e) {
|
|
221
|
+
const rect = el.getBoundingClientRect();
|
|
222
|
+
this.#element = el.cloneNode(true);
|
|
223
|
+
this.#offsetX = e.clientX - rect.left;
|
|
224
|
+
this.#offsetY = e.clientY - rect.top;
|
|
225
|
+
this.#element.style.position = 'fixed';
|
|
226
|
+
this.#element.style.left = '0';
|
|
227
|
+
this.#element.style.top = '0';
|
|
228
|
+
this.#element.style.width = `${rect.width}px`;
|
|
229
|
+
this.#element.style.height = `${rect.height}px`;
|
|
230
|
+
this.#element.style.pointerEvents = 'none';
|
|
231
|
+
this.#element.style.zIndex = '9999';
|
|
232
|
+
this.#element.style.opacity = '0.85';
|
|
233
|
+
this.onMove(e);
|
|
234
|
+
}
|
|
235
|
+
attach(root) {
|
|
236
|
+
root.appendChild(this.#element);
|
|
237
|
+
return this;
|
|
238
|
+
}
|
|
239
|
+
onMove(e) {
|
|
240
|
+
const x = e.clientX - this.#offsetX;
|
|
241
|
+
const y = e.clientY - this.#offsetY;
|
|
242
|
+
this.#element.style.transform = `translate3d(${x}px, ${y}px, 0)`;
|
|
243
|
+
}
|
|
244
|
+
onStop() {
|
|
245
|
+
this.#element.remove();
|
|
246
|
+
}
|
|
247
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function almostEqual(a: number, b: number, eps?: number): boolean;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export interface Registry<K, V> {
|
|
2
|
+
get(key: K): V;
|
|
3
|
+
}
|
|
4
|
+
export interface MutableRegistry<K, V> extends Registry<K, V> {
|
|
5
|
+
set(key: K, val: V): void;
|
|
6
|
+
delete(key: K): void;
|
|
7
|
+
}
|
|
8
|
+
export declare function fromRecord<R extends Record<string, unknown>>(record: R): Registry<keyof R, R[keyof R]>;
|
|
9
|
+
export declare function fromConstant<V>(value: V): Registry<any, V>;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export type Direction = 'row' | 'column';
|
|
2
|
+
export type EdgePart = 'start' | 'center' | 'end';
|
|
3
|
+
export declare function getEdgePart(a: number, b: number, x: number, ratio: number): EdgePart | undefined;
|
|
4
|
+
export declare function getRectParts(rect: DOMRect, x: number, y: number, ratio: number): {
|
|
5
|
+
hpart: EdgePart | undefined;
|
|
6
|
+
vpart: EdgePart | undefined;
|
|
7
|
+
};
|