@aicupa/plugin-cut 1.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.
@@ -0,0 +1,21 @@
1
+ name: Npm Publish
2
+
3
+ on:
4
+ push:
5
+ tags:
6
+ - 'v*.*.*'
7
+
8
+ jobs:
9
+ Npm-Publish:
10
+ runs-on: ubuntu-latest
11
+ steps:
12
+ - name: Checkout
13
+ uses: actions/checkout@v2.3.4
14
+
15
+ - name: Install Deps
16
+ run: yarn install
17
+
18
+ - name: publish with latest tag
19
+ uses: JS-DevTools/npm-publish@v1
20
+ with:
21
+ token: ${{ secrets.NPM_AUTH_TOKEN }}
package/package.json ADDED
@@ -0,0 +1,30 @@
1
+ {
2
+ "name": "@aicupa/plugin-cut",
3
+ "version": "1.0.1",
4
+ "description": "Cut and paste tree nodes",
5
+ "main": "./service",
6
+ "view": "./view",
7
+ "pluginContributes": {
8
+ "contextMenus": [
9
+ {
10
+ "title": "Cut",
11
+ "title_zh": "剪切",
12
+ "command": "cutNode"
13
+ }
14
+ ],
15
+ "events": {
16
+ "paste": {
17
+ "check": "getCutBuffer",
18
+ "handler": "pasteNode"
19
+ }
20
+ }
21
+ },
22
+ "publishConfig": {
23
+ "registry": "https://registry.npmjs.org/",
24
+ "access": "public"
25
+ },
26
+ "license": "MIT",
27
+ "dependencies": {
28
+ "@aicupa/api": "^1.0.1"
29
+ }
30
+ }
package/service.js ADDED
@@ -0,0 +1,79 @@
1
+ const { createPlugin } = require('@aicupa/api')
2
+
3
+ module.exports = createPlugin((api) => {
4
+ let cutBuffer = null
5
+
6
+ return {
7
+ cutNode({ node, filePath }) {
8
+ cutBuffer = { node, filePath }
9
+ return { ok: true }
10
+ },
11
+
12
+ async pasteNode({ targetNode, filePath }) {
13
+ if (!cutBuffer) {
14
+ return { ok: false, error: 'No node in cut buffer' }
15
+ }
16
+
17
+ const source = cutBuffer
18
+ cutBuffer = null
19
+
20
+ const tree = await api.getTree(filePath)
21
+ if (!tree) {
22
+ return { ok: false, error: 'Failed to read tree' }
23
+ }
24
+
25
+ const treeData = api.getArray(tree.tree || tree)
26
+
27
+ const removed = removeNodeByKey(treeData, source.node.key)
28
+ if (!removed) {
29
+ return { ok: false, error: 'Source node not found' }
30
+ }
31
+
32
+ const target = findNode(treeData, targetNode.key)
33
+ if (!target) {
34
+ return { ok: false, error: 'Target node not found' }
35
+ }
36
+
37
+ if (!Array.isArray(target.children)) {
38
+ target.children = []
39
+ }
40
+ target.children.push(removed)
41
+
42
+ await api.store('todotree', treeData, filePath)
43
+ api.reload(filePath)
44
+
45
+ return { ok: true }
46
+ },
47
+
48
+ getCutBuffer() {
49
+ return { ok: true, node: cutBuffer?.node || null }
50
+ },
51
+
52
+ clearCutBuffer() {
53
+ cutBuffer = null
54
+ return { ok: true }
55
+ },
56
+ }
57
+ })
58
+
59
+ function findNode(tree, key) {
60
+ if (!Array.isArray(tree)) return null
61
+ for (const node of tree) {
62
+ if (node.key == key) return node
63
+ const found = findNode(node.children, key)
64
+ if (found) return found
65
+ }
66
+ return null
67
+ }
68
+
69
+ function removeNodeByKey(tree, key) {
70
+ if (!Array.isArray(tree)) return null
71
+ for (let i = 0; i < tree.length; i++) {
72
+ if (tree[i].key == key) {
73
+ return tree.splice(i, 1)[0]
74
+ }
75
+ const found = removeNodeByKey(tree[i].children, key)
76
+ if (found) return found
77
+ }
78
+ return null
79
+ }
@@ -0,0 +1,5 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head><meta charset="UTF-8"><title>Cut Plugin</title></head>
4
+ <body><p>Cut plugin has no standalone view.</p></body>
5
+ </html>