@splicetree/plugin-selectable 0.1.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/CHANGELOG.md ADDED
@@ -0,0 +1,10 @@
1
+ # @splicetree/plugin-selectable
2
+
3
+ ## 0.1.0
4
+
5
+ ### Minor Changes
6
+
7
+ - ### @splicetree/plugin-selectable
8
+ - 新增行为插件,消费 `input:node-click` 与 `input:direction`
9
+ - 支持单选/多选与 Shift 范围选择
10
+ - 配置聚合到 `configuration.selectable`:`multiple/defaultSelected`
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Michael Cocova
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,51 @@
1
+ # @splicetree/plugin-selectable
2
+
3
+ 提供选择与激活能力,支持单选/多选与 Shift 范围选择;消费 `input:node-click` 与 `input:direction` 语义事件。
4
+
5
+ [![version](https://img.shields.io/npm/v/@splicetree/plugin-selectable.svg?label=version)](https://www.npmjs.com/package/@splicetree/plugin-selectable)
6
+ [![downloads](https://img.shields.io/npm/dm/@splicetree/plugin-selectable.svg)](https://npmcharts.com/compare/%40splicetree%2Fplugin-selectable?minimal=true)
7
+ [![license](https://img.shields.io/npm/l/@splicetree/plugin-selectable.svg)](https://www.npmjs.com/package/@splicetree/plugin-selectable)
8
+ [![Website](https://img.shields.io/static/v1?label=Website&message=splicetree.dev&color=blue)](https://www.splicetree.dev)
9
+ [![GitHub](https://img.shields.io/static/v1?label=GitHub&message=splicetree%2Fsplicetree&logo=github)](https://github.com/michaelcocova/splicetree)
10
+
11
+ ## 安装
12
+
13
+ `pnpm add @splicetree/plugin-selectable`
14
+
15
+ ## 使用
16
+
17
+ ```ts
18
+ import { createSpliceTree } from '@splicetree/core'
19
+ import keyboard from '@splicetree/plugin-keyboard'
20
+ import pointer from '@splicetree/plugin-pointer'
21
+ import selectable from '@splicetree/plugin-selectable'
22
+
23
+ const tree = createSpliceTree(data, {
24
+ plugins: [keyboard, pointer, selectable],
25
+ configuration: {
26
+ selectable: {
27
+ multiple: true,
28
+ defaultSelected: ['a'],
29
+ },
30
+ keyboard: {
31
+ autoListen: true,
32
+ target: '.keyboard-wrap',
33
+ },
34
+ },
35
+ })
36
+ ```
37
+
38
+ ## Api
39
+
40
+ - 实例
41
+ - `selectedKeys: Set<string>` 当前选中集合
42
+ - `lastSelectedKey?: string` 最近一次选中的 id
43
+ - `activeId?: string` 当前激活 id(用于键盘导航)
44
+ - `isSelected(id: string): boolean`
45
+ - `toggleSelect(id: string, selected?: boolean): void`
46
+ - 节点
47
+ - `isSelected(): boolean`
48
+ - `toggleSelect(selected?: boolean): void`
49
+ - 配置(`configuration.selectable`)
50
+ - `multiple?: boolean` 是否多选
51
+ - `defaultSelected?: string[]` 默认选中集合
@@ -0,0 +1,42 @@
1
+ import { SpliceTreePlugin } from "@splicetree/core";
2
+
3
+ //#region src/index.d.ts
4
+ interface Modifiers {
5
+ shift: boolean;
6
+ ctrl: boolean;
7
+ meta: boolean;
8
+ alt: boolean;
9
+ }
10
+ interface SelectableOptions {
11
+ multiple?: boolean;
12
+ defaultSelected?: string[];
13
+ }
14
+ declare module '@splicetree/core' {
15
+ interface SpliceTreeConfiguration {
16
+ selectable?: SelectableOptions;
17
+ }
18
+ interface SpliceTreeEventPayloadMap {
19
+ 'input:direction': {
20
+ direction: 'up' | 'down' | 'left' | 'right';
21
+ modifiers: Modifiers;
22
+ };
23
+ 'input:node-click': {
24
+ nodeId: string;
25
+ modifiers: Modifiers;
26
+ };
27
+ }
28
+ interface SpliceTreeInstance {
29
+ selectedKeys: Set<string>;
30
+ lastSelectedKey?: string;
31
+ activeId?: string;
32
+ isSelected: (id: string) => boolean;
33
+ toggleSelect: (id: string, selected?: boolean) => void;
34
+ }
35
+ interface SpliceTreeNode {
36
+ isSelected: () => boolean;
37
+ toggleSelect: (selected?: boolean) => void;
38
+ }
39
+ }
40
+ declare const selectablePlugin: SpliceTreePlugin;
41
+ //#endregion
42
+ export { SelectableOptions, selectablePlugin as default, selectablePlugin };
package/dist/index.js ADDED
@@ -0,0 +1,108 @@
1
+ //#region src/index.ts
2
+ function getOptions(ctx) {
3
+ const o = ctx.options?.configuration?.selectable;
4
+ return {
5
+ multiple: o?.multiple ?? false,
6
+ defaultSelected: o?.defaultSelected ?? []
7
+ };
8
+ }
9
+ const selectablePlugin = {
10
+ name: "selectable",
11
+ setup(ctx) {
12
+ const { multiple, defaultSelected } = getOptions(ctx);
13
+ const selectedKeys = new Set(defaultSelected);
14
+ let lastSelectedKey;
15
+ const isSelected = (id) => selectedKeys.has(id);
16
+ const toggleSelect = (id, selected) => {
17
+ const has = selectedKeys.has(id);
18
+ if (selected ?? !has) {
19
+ if (!multiple) selectedKeys.clear();
20
+ selectedKeys.add(id);
21
+ } else selectedKeys.delete(id);
22
+ lastSelectedKey = id;
23
+ ctx.tree.activeId = id;
24
+ ctx.events.emit({
25
+ name: "visibility",
26
+ keys: ctx.tree.expandedKeys()
27
+ });
28
+ };
29
+ const selectRange = (fromId, toId) => {
30
+ const items = ctx.tree.items();
31
+ const a = items.findIndex((n) => n.id === fromId);
32
+ const b = items.findIndex((n) => n.id === toId);
33
+ if (a < 0 || b < 0) return;
34
+ const [s, e] = a < b ? [a, b] : [b, a];
35
+ for (let i = s; i <= e; i++) selectedKeys.add(items[i].id);
36
+ lastSelectedKey = toId;
37
+ ctx.tree.activeId = toId;
38
+ ctx.events.emit({
39
+ name: "visibility",
40
+ keys: ctx.tree.expandedKeys()
41
+ });
42
+ };
43
+ ctx.events.on("input:node-click", (p) => {
44
+ const nodeId = p.nodeId;
45
+ const modifiers = p.modifiers;
46
+ if (!ctx.tree.getNode(nodeId)) return;
47
+ const shift = modifiers.shift;
48
+ const multiToggle = multiple && (modifiers.ctrl || modifiers.meta);
49
+ if (shift && lastSelectedKey) selectRange(lastSelectedKey, nodeId);
50
+ else if (multiToggle) toggleSelect(nodeId);
51
+ else toggleSelect(nodeId, true);
52
+ });
53
+ ctx.events.on("input:direction", (p) => {
54
+ const direction = p.direction;
55
+ const modifiers = p.modifiers;
56
+ const items = ctx.tree.items();
57
+ const activeId = ctx.tree.activeId;
58
+ if (!activeId) return;
59
+ const idx = items.findIndex((n) => n.id === activeId);
60
+ if (idx < 0) return;
61
+ if (direction === "left" || direction === "right") {
62
+ const node = ctx.tree.getNode(activeId);
63
+ if (!node) return;
64
+ if (direction === "left") if (node.isExpanded()) node.toggleExpand(false);
65
+ else {
66
+ const parent = node.getParent();
67
+ if (parent) {
68
+ ctx.tree.activeId = parent.id;
69
+ if (!(modifiers.ctrl || modifiers.meta)) toggleSelect(parent.id, true);
70
+ }
71
+ }
72
+ else if (direction === "right") {
73
+ if (node.hasChildren()) {
74
+ node.toggleExpand(true);
75
+ if (!(modifiers.ctrl || modifiers.meta)) toggleSelect(node.id, true);
76
+ }
77
+ }
78
+ return;
79
+ }
80
+ let nextIdx = idx;
81
+ if (direction === "up") nextIdx = Math.max(0, idx - 1);
82
+ else if (direction === "down") nextIdx = Math.min(items.length - 1, idx + 1);
83
+ const nextId = items[nextIdx]?.id;
84
+ if (!nextId) return;
85
+ ctx.tree.activeId = nextId;
86
+ if (modifiers.shift && multiple && lastSelectedKey) selectRange(lastSelectedKey, nextId);
87
+ else if (!(modifiers.ctrl || modifiers.meta)) toggleSelect(nextId, true);
88
+ });
89
+ for (const id of defaultSelected) {
90
+ selectedKeys.add(id);
91
+ lastSelectedKey = id;
92
+ }
93
+ return {
94
+ selectedKeys,
95
+ lastSelectedKey,
96
+ isSelected,
97
+ toggleSelect
98
+ };
99
+ },
100
+ extendNode(node, ctx) {
101
+ node.isSelected = () => ctx.tree.isSelected(node.id);
102
+ node.toggleSelect = (selected) => ctx.tree.toggleSelect(node.id, selected);
103
+ }
104
+ };
105
+ var src_default = selectablePlugin;
106
+
107
+ //#endregion
108
+ export { src_default as default, selectablePlugin };
package/package.json ADDED
@@ -0,0 +1,32 @@
1
+ {
2
+ "name": "@splicetree/plugin-selectable",
3
+ "type": "module",
4
+ "version": "0.1.1",
5
+ "author": {
6
+ "email": "michael.cocova@gmail.com",
7
+ "name": "Michael Cocova"
8
+ },
9
+ "license": "MIT",
10
+ "homepage": "https://www.splicetree.dev",
11
+ "repository": {
12
+ "type": "git",
13
+ "url": "https://github.com/michaelcocova/splicetree"
14
+ },
15
+ "exports": {
16
+ ".": "./dist/index.js",
17
+ "./package.json": "./package.json"
18
+ },
19
+ "main": "./dist/index.js",
20
+ "module": "./dist/index.js",
21
+ "types": "./dist/index.d.ts",
22
+ "publishConfig": {
23
+ "access": "public"
24
+ },
25
+ "devDependencies": {
26
+ "@splicetree/core": "0.1.1"
27
+ },
28
+ "scripts": {
29
+ "dev": "tsdown --watch",
30
+ "build": "tsdown"
31
+ }
32
+ }
package/src/index.ts ADDED
@@ -0,0 +1,183 @@
1
+ import type { SpliceTreeNode, SpliceTreePlugin, SpliceTreePluginContext } from '@splicetree/core'
2
+ import '@splicetree/core'
3
+
4
+ interface Modifiers {
5
+ shift: boolean
6
+ ctrl: boolean
7
+ meta: boolean
8
+ alt: boolean
9
+ }
10
+
11
+ export interface SelectableOptions {
12
+ multiple?: boolean
13
+ defaultSelected?: string[]
14
+ }
15
+
16
+ declare module '@splicetree/core' {
17
+ export interface SpliceTreeConfiguration {
18
+ selectable?: SelectableOptions
19
+ }
20
+ export interface SpliceTreeEventPayloadMap {
21
+ 'input:direction': {
22
+ direction: 'up' | 'down' | 'left' | 'right'
23
+ modifiers: Modifiers
24
+ }
25
+ 'input:node-click': {
26
+ nodeId: string
27
+ modifiers: Modifiers
28
+ }
29
+ }
30
+ export interface SpliceTreeInstance {
31
+ selectedKeys: Set<string>
32
+ lastSelectedKey?: string
33
+ activeId?: string
34
+ isSelected: (id: string) => boolean
35
+ toggleSelect: (id: string, selected?: boolean) => void
36
+ }
37
+ export interface SpliceTreeNode {
38
+ isSelected: () => boolean
39
+ toggleSelect: (selected?: boolean) => void
40
+ }
41
+ }
42
+
43
+ function getOptions(ctx: SpliceTreePluginContext): Required<SelectableOptions> {
44
+ const o = ctx.options?.configuration?.selectable
45
+ return {
46
+ multiple: o?.multiple ?? false,
47
+ defaultSelected: o?.defaultSelected ?? [],
48
+ }
49
+ }
50
+
51
+ export const selectablePlugin: SpliceTreePlugin = {
52
+ name: 'selectable',
53
+ setup(ctx: SpliceTreePluginContext) {
54
+ const { multiple, defaultSelected } = getOptions(ctx)
55
+ const selectedKeys = new Set<string>(defaultSelected)
56
+ let lastSelectedKey: string | undefined
57
+
58
+ const isSelected = (id: string) => selectedKeys.has(id)
59
+ const toggleSelect = (id: string, selected?: boolean) => {
60
+ const has = selectedKeys.has(id)
61
+ const next = selected ?? !has
62
+ if (next) {
63
+ if (!multiple) {
64
+ selectedKeys.clear()
65
+ }
66
+ selectedKeys.add(id)
67
+ } else {
68
+ selectedKeys.delete(id)
69
+ }
70
+ lastSelectedKey = id
71
+ ctx.tree.activeId = id
72
+ ctx.events.emit({ name: 'visibility', keys: ctx.tree.expandedKeys() })
73
+ }
74
+
75
+ const selectRange = (fromId: string, toId: string) => {
76
+ const items: SpliceTreeNode[] = ctx.tree.items()
77
+ const a = items.findIndex((n: SpliceTreeNode) => n.id === fromId)
78
+ const b = items.findIndex((n: SpliceTreeNode) => n.id === toId)
79
+ if (a < 0 || b < 0) {
80
+ return
81
+ }
82
+ const [s, e] = a < b ? [a, b] : [b, a]
83
+ for (let i = s; i <= e; i++) {
84
+ selectedKeys.add((items[i] as SpliceTreeNode).id)
85
+ }
86
+ lastSelectedKey = toId
87
+ ctx.tree.activeId = toId
88
+ ctx.events.emit({ name: 'visibility', keys: ctx.tree.expandedKeys() })
89
+ }
90
+
91
+ ctx.events.on('input:node-click', (p: any) => {
92
+ const nodeId = p.nodeId as string
93
+ const modifiers = p.modifiers as Modifiers
94
+ if (!ctx.tree.getNode(nodeId)) {
95
+ return
96
+ }
97
+ const shift = modifiers.shift
98
+ const multiToggle = multiple && (modifiers.ctrl || modifiers.meta)
99
+ if (shift && lastSelectedKey) {
100
+ selectRange(lastSelectedKey, nodeId)
101
+ } else if (multiToggle) {
102
+ toggleSelect(nodeId)
103
+ } else {
104
+ toggleSelect(nodeId, true)
105
+ }
106
+ })
107
+
108
+ ctx.events.on('input:direction', (p: any) => {
109
+ const direction = p.direction as 'up' | 'down' | 'left' | 'right'
110
+ const modifiers = p.modifiers as Modifiers
111
+ const items: SpliceTreeNode[] = ctx.tree.items()
112
+ const activeId = ctx.tree.activeId
113
+ if (!activeId) {
114
+ return
115
+ }
116
+ const idx = items.findIndex((n: SpliceTreeNode) => n.id === activeId)
117
+ if (idx < 0) {
118
+ return
119
+ }
120
+ if (direction === 'left' || direction === 'right') {
121
+ const node = ctx.tree.getNode(activeId) as SpliceTreeNode | undefined
122
+ if (!node) {
123
+ return
124
+ }
125
+ if (direction === 'left') {
126
+ if (node.isExpanded()) {
127
+ node.toggleExpand(false)
128
+ } else {
129
+ const parent = node.getParent() as SpliceTreeNode | undefined
130
+ if (parent) {
131
+ ctx.tree.activeId = parent.id
132
+ if (!(modifiers.ctrl || modifiers.meta)) {
133
+ toggleSelect(parent.id, true)
134
+ }
135
+ }
136
+ }
137
+ } else if (direction === 'right') {
138
+ if (node.hasChildren()) {
139
+ node.toggleExpand(true)
140
+ if (!(modifiers.ctrl || modifiers.meta)) {
141
+ toggleSelect(node.id, true)
142
+ }
143
+ }
144
+ }
145
+ return
146
+ }
147
+ let nextIdx = idx
148
+ if (direction === 'up') {
149
+ nextIdx = Math.max(0, idx - 1)
150
+ } else if (direction === 'down') {
151
+ nextIdx = Math.min(items.length - 1, idx + 1)
152
+ }
153
+ const nextId = (items[nextIdx] as SpliceTreeNode | undefined)?.id
154
+ if (!nextId) {
155
+ return
156
+ }
157
+ ctx.tree.activeId = nextId
158
+ if (modifiers.shift && multiple && lastSelectedKey) {
159
+ selectRange(lastSelectedKey, nextId)
160
+ } else if (!(modifiers.ctrl || modifiers.meta)) {
161
+ toggleSelect(nextId, true)
162
+ }
163
+ })
164
+
165
+ for (const id of defaultSelected) {
166
+ selectedKeys.add(id)
167
+ lastSelectedKey = id
168
+ }
169
+
170
+ return {
171
+ selectedKeys,
172
+ lastSelectedKey,
173
+ isSelected,
174
+ toggleSelect,
175
+ }
176
+ },
177
+ extendNode(node: SpliceTreeNode, ctx: SpliceTreePluginContext) {
178
+ node.isSelected = () => ctx.tree.isSelected(node.id)
179
+ node.toggleSelect = (selected?: boolean) => ctx.tree.toggleSelect(node.id, selected)
180
+ },
181
+ }
182
+
183
+ export default selectablePlugin
package/tsconfig.json ADDED
@@ -0,0 +1,9 @@
1
+ {
2
+ "extends": "../../../tsconfig.base.json",
3
+ "compilerOptions": {
4
+ "declaration": true,
5
+ "outDir": "dist"
6
+ },
7
+ "include": ["src"],
8
+ "exclude": ["dist", "node_modules"]
9
+ }
@@ -0,0 +1,11 @@
1
+ import { defineConfig } from 'tsdown'
2
+
3
+ export default defineConfig({
4
+ clean: true,
5
+ sourcemap: false,
6
+ treeshake: true,
7
+ unused: true,
8
+ unbundle: false,
9
+ platform: 'neutral',
10
+ entry: ['./src/index.ts'],
11
+ })