Skip to content

Commit b0c8ee6

Browse files
committed
feat: add clipboard
1 parent 01d7c27 commit b0c8ee6

File tree

5 files changed

+49
-7
lines changed

5 files changed

+49
-7
lines changed

.eslintrc.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ module.exports = {
1616
rules: {
1717
'no-console': process.env.NODE_ENV === 'production' ? 'warn' : 'off',
1818
'no-debugger': process.env.NODE_ENV === 'production' ? 'warn' : 'off',
19+
'@typescript-eslint/explicit-module-boundary-types': 'off',
1920
// 'prettier/prettier': [1, { arrowParens: 'avoid' }],
2021
},
2122
};

src/components/Tree/index.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,8 @@ export default defineComponent({
288288
state.visibleData.map(item => (
289289
<TreeNode
290290
key={item.id}
291+
data={props.data}
292+
rootPath={props.rootPath}
291293
node={item}
292294
collapsed={!!state.hiddenPaths[item.path]}
293295
theme={props.theme}

src/components/TreeNode/index.tsx

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import Brackets from 'src/components/Brackets';
33
import CheckController from 'src/components/CheckController';
44
import Carets from 'src/components/Carets';
55
import { getDataType, JSONFlattenReturnType, stringToAutoType } from 'src/utils';
6+
import { useClipboard } from 'src/hooks/useClipboard';
67
import './styles.less';
78

89
export interface NodeDataType extends JSONFlattenReturnType {
@@ -11,6 +12,16 @@ export interface NodeDataType extends JSONFlattenReturnType {
1112

1213
// The props here will be exposed to the user through the topmost component.
1314
export const treeNodePropsPass = {
15+
// JSONLike data.
16+
data: {
17+
type: [String, Number, Boolean, Array, Object] as PropType<JSONDataType>,
18+
default: null,
19+
},
20+
// Data root path.
21+
rootPath: {
22+
type: String,
23+
default: 'root',
24+
},
1425
// Whether to display the length of (array|object).
1526
showLength: {
1627
type: Boolean,
@@ -214,6 +225,16 @@ export default defineComponent({
214225
}
215226
};
216227

228+
const { copy } = useClipboard();
229+
230+
const handleCopy = () => {
231+
const { key, path } = props.node;
232+
const rootPath = props.rootPath;
233+
const content = new Function('data', `return data${path.slice(rootPath.length)}`)(props.data);
234+
const copiedData = JSON.stringify(key ? { [key]: content } : content, null, 2);
235+
copy(copiedData);
236+
}
237+
217238
return () => {
218239
const { node } = props;
219240

@@ -305,6 +326,10 @@ export default defineComponent({
305326
<span class="vjs-comment"> // {node.length} items </span>
306327
)}
307328
</span>
329+
330+
<span class="vjs-tree-node-actions">
331+
<span onClick={handleCopy}>copy</span>
332+
</span>
308333
</div>
309334
);
310335
};

src/components/TreeNode/styles.less

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@
2121
&.is-highlight,
2222
&:hover {
2323
background-color: @highlight-bg-color;
24+
border-radius: 4px;
25+
26+
.@{css-prefix}-tree-node-actions {
27+
display: block;
28+
}
2429
}
2530

2631
.@{css-prefix}-indent {
@@ -36,11 +41,24 @@
3641
}
3742
}
3843

44+
.@{css-prefix}-tree-node-actions {
45+
display: none;
46+
position: absolute;
47+
right: 0;
48+
top: 0;
49+
padding: 0 4px;
50+
background-color: @highlight-bg-color;
51+
border-radius: 4px;
52+
}
53+
3954
&.dark {
4055
&.is-highlight,
4156
&:hover {
4257
background-color: @highlight-bg-color-dark;
4358
}
59+
.@{css-prefix}-tree-node-actions {
60+
background-color: @highlight-bg-color-dark;
61+
}
4462
}
4563
}
4664

src/hooks/useClipboard.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
11
import { ref } from 'vue';
22

3-
export type UseClipboardOptions = {
4-
source: string;
5-
};
6-
7-
export function useClipboard({ source }: UseClipboardOptions) {
3+
export function useClipboard() {
84
const copied = ref(false);
95

10-
const copy = async () => {
6+
const copy = async (source: string) => {
117
try {
128
await navigator.clipboard.writeText(source);
139
copied.value = true;
@@ -22,4 +18,4 @@ export function useClipboard({ source }: UseClipboardOptions) {
2218
return {
2319
copy,
2420
};
25-
};
21+
}

0 commit comments

Comments
 (0)