@coze-editor/extension-json-hover 0.1.0-alpha.09ffeb

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 ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 coze-dev
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.
@@ -0,0 +1,69 @@
1
+ // src/index.ts
2
+ import astField, {
3
+ findNodeAtOffset
4
+ } from "@coze-editor/extension-json-ast";
5
+ import { hoverTooltip } from "@codemirror/view";
6
+ var jsonHover = (options) => [
7
+ astField,
8
+ hoverTooltip((view, pos) => {
9
+ const ast = view.state.field(astField);
10
+ if (!ast) {
11
+ return null;
12
+ }
13
+ const node = findNodeAtOffset(ast, pos, true);
14
+ if (node && isKeyNode(node)) {
15
+ const tooltip = options.key({
16
+ paths: getPaths(node)
17
+ });
18
+ if (!tooltip) {
19
+ return null;
20
+ }
21
+ return {
22
+ pos: node.offset,
23
+ end: node.offset + node.length,
24
+ create() {
25
+ return tooltip;
26
+ }
27
+ };
28
+ }
29
+ return null;
30
+ })
31
+ ];
32
+ function isKeyNode(node) {
33
+ const parentNode = node == null ? void 0 : node.parent;
34
+ if (!node || !parentNode) {
35
+ return false;
36
+ }
37
+ return node.type === "string" && parentNode.type === "property" && parentNode.keyNode === node;
38
+ }
39
+ function isInProperty(node) {
40
+ var _a;
41
+ return ((_a = node == null ? void 0 : node.parent) == null ? void 0 : _a.type) === "property";
42
+ }
43
+ function isItemNode(node) {
44
+ const parentNode = node == null ? void 0 : node.parent;
45
+ if (!node || !parentNode) {
46
+ return false;
47
+ }
48
+ return parentNode.type === "array" && !!~parentNode.items.indexOf(node);
49
+ }
50
+ function getPaths(node) {
51
+ const paths = [];
52
+ let target = node;
53
+ while (target) {
54
+ if (isInProperty(target)) {
55
+ const key = target.parent.keyNode.value;
56
+ paths.unshift(key);
57
+ } else if (isItemNode(target)) {
58
+ const itemIndex = target.parent.items.indexOf(target);
59
+ paths.unshift(itemIndex);
60
+ }
61
+ target = target.parent;
62
+ }
63
+ return paths;
64
+ }
65
+ var index_default = jsonHover;
66
+ export {
67
+ index_default as default
68
+ };
69
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/index.ts"],"sourcesContent":["// Copyright (c) 2025 coze-dev\n// SPDX-License-Identifier: MIT\n\nimport astField, {\n type ArrayASTNode,\n type ASTNode,\n findNodeAtOffset,\n type PropertyASTNode,\n} from '@coze-editor/extension-json-ast';\nimport { hoverTooltip, type TooltipView } from '@codemirror/view';\n\ntype JSONPath = string | number;\n\ninterface Options {\n key: (e: { paths: JSONPath[] }) => TooltipView | undefined;\n}\n\nconst jsonHover = (options: Options) => [\n astField,\n hoverTooltip((view, pos) => {\n const ast = view.state.field(astField);\n\n if (!ast) {\n return null;\n }\n\n const node = findNodeAtOffset(ast, pos, true);\n\n // hover on key\n if (node && isKeyNode(node)) {\n const tooltip = options.key({\n paths: getPaths(node),\n });\n\n if (!tooltip) {\n return null;\n }\n\n return {\n pos: node.offset,\n end: node.offset + node.length,\n create() {\n return tooltip;\n },\n };\n }\n\n return null;\n }),\n];\n\nfunction isKeyNode(node?: ASTNode): boolean {\n const parentNode = node?.parent;\n\n if (!node || !parentNode) {\n return false;\n }\n\n return (\n node.type === 'string' &&\n parentNode.type === 'property' &&\n parentNode.keyNode === node\n );\n}\n\nfunction isInProperty(node?: ASTNode): boolean {\n return node?.parent?.type === 'property';\n}\n\nfunction isItemNode(node?: ASTNode): boolean {\n const parentNode = node?.parent;\n\n if (!node || !parentNode) {\n return false;\n }\n\n return parentNode.type === 'array' && !!~parentNode.items.indexOf(node);\n}\n\nfunction getPaths(node: ASTNode): JSONPath[] {\n const paths: JSONPath[] = [];\n\n let target: ASTNode | undefined = node;\n while (target) {\n if (isInProperty(target)) {\n const key = (target.parent as PropertyASTNode).keyNode.value;\n paths.unshift(key);\n } else if (isItemNode(target)) {\n const itemIndex = (target.parent as ArrayASTNode).items.indexOf(target);\n paths.unshift(itemIndex);\n }\n\n target = target.parent;\n }\n\n return paths;\n}\n\nexport default jsonHover;\n"],"mappings":";AAGA,OAAO;AAAA,EAGL;AAAA,OAEK;AACP,SAAS,oBAAsC;AAQ/C,IAAM,YAAY,CAAC,YAAqB;AAAA,EACtC;AAAA,EACA,aAAa,CAAC,MAAM,QAAQ;AAC1B,UAAM,MAAM,KAAK,MAAM,MAAM,QAAQ;AAErC,QAAI,CAAC,KAAK;AACR,aAAO;AAAA,IACT;AAEA,UAAM,OAAO,iBAAiB,KAAK,KAAK,IAAI;AAG5C,QAAI,QAAQ,UAAU,IAAI,GAAG;AAC3B,YAAM,UAAU,QAAQ,IAAI;AAAA,QAC1B,OAAO,SAAS,IAAI;AAAA,MACtB,CAAC;AAED,UAAI,CAAC,SAAS;AACZ,eAAO;AAAA,MACT;AAEA,aAAO;AAAA,QACL,KAAK,KAAK;AAAA,QACV,KAAK,KAAK,SAAS,KAAK;AAAA,QACxB,SAAS;AACP,iBAAO;AAAA,QACT;AAAA,MACF;AAAA,IACF;AAEA,WAAO;AAAA,EACT,CAAC;AACH;AAEA,SAAS,UAAU,MAAyB;AAC1C,QAAM,aAAa,6BAAM;AAEzB,MAAI,CAAC,QAAQ,CAAC,YAAY;AACxB,WAAO;AAAA,EACT;AAEA,SACE,KAAK,SAAS,YACd,WAAW,SAAS,cACpB,WAAW,YAAY;AAE3B;AAEA,SAAS,aAAa,MAAyB;AAjE/C;AAkEE,WAAO,kCAAM,WAAN,mBAAc,UAAS;AAChC;AAEA,SAAS,WAAW,MAAyB;AAC3C,QAAM,aAAa,6BAAM;AAEzB,MAAI,CAAC,QAAQ,CAAC,YAAY;AACxB,WAAO;AAAA,EACT;AAEA,SAAO,WAAW,SAAS,WAAW,CAAC,CAAC,CAAC,WAAW,MAAM,QAAQ,IAAI;AACxE;AAEA,SAAS,SAAS,MAA2B;AAC3C,QAAM,QAAoB,CAAC;AAE3B,MAAI,SAA8B;AAClC,SAAO,QAAQ;AACb,QAAI,aAAa,MAAM,GAAG;AACxB,YAAM,MAAO,OAAO,OAA2B,QAAQ;AACvD,YAAM,QAAQ,GAAG;AAAA,IACnB,WAAW,WAAW,MAAM,GAAG;AAC7B,YAAM,YAAa,OAAO,OAAwB,MAAM,QAAQ,MAAM;AACtE,YAAM,QAAQ,SAAS;AAAA,IACzB;AAEA,aAAS,OAAO;AAAA,EAClB;AAEA,SAAO;AACT;AAEA,IAAO,gBAAQ;","names":[]}
@@ -0,0 +1,16 @@
1
+ import * as _codemirror_view from '@codemirror/view';
2
+ import { TooltipView } from '@codemirror/view';
3
+ import * as _codemirror_state from '@codemirror/state';
4
+ import { ASTNode } from '@coze-editor/extension-json-ast';
5
+
6
+ type JSONPath = string | number;
7
+ interface Options {
8
+ key: (e: {
9
+ paths: JSONPath[];
10
+ }) => TooltipView | undefined;
11
+ }
12
+ declare const jsonHover: (options: Options) => (_codemirror_state.StateField<ASTNode> | (_codemirror_state.Extension & {
13
+ active: _codemirror_state.StateField<readonly _codemirror_view.Tooltip[]>;
14
+ }))[];
15
+
16
+ export { jsonHover as default };
@@ -0,0 +1,16 @@
1
+ import * as _codemirror_view from '@codemirror/view';
2
+ import { TooltipView } from '@codemirror/view';
3
+ import * as _codemirror_state from '@codemirror/state';
4
+ import { ASTNode } from '@coze-editor/extension-json-ast';
5
+
6
+ type JSONPath = string | number;
7
+ interface Options {
8
+ key: (e: {
9
+ paths: JSONPath[];
10
+ }) => TooltipView | undefined;
11
+ }
12
+ declare const jsonHover: (options: Options) => (_codemirror_state.StateField<ASTNode> | (_codemirror_state.Extension & {
13
+ active: _codemirror_state.StateField<readonly _codemirror_view.Tooltip[]>;
14
+ }))[];
15
+
16
+ export { jsonHover as default };
package/dist/index.js ADDED
@@ -0,0 +1,97 @@
1
+ var __create = Object.create;
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __getProtoOf = Object.getPrototypeOf;
6
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
7
+ var __export = (target, all) => {
8
+ for (var name in all)
9
+ __defProp(target, name, { get: all[name], enumerable: true });
10
+ };
11
+ var __copyProps = (to, from, except, desc) => {
12
+ if (from && typeof from === "object" || typeof from === "function") {
13
+ for (let key of __getOwnPropNames(from))
14
+ if (!__hasOwnProp.call(to, key) && key !== except)
15
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
16
+ }
17
+ return to;
18
+ };
19
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
20
+ // If the importer is in node compatibility mode or this is not an ESM
21
+ // file that has been converted to a CommonJS file using a Babel-
22
+ // compatible transform (i.e. "__esModule" has not been set), then set
23
+ // "default" to the CommonJS "module.exports" for node compatibility.
24
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
25
+ mod
26
+ ));
27
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
28
+
29
+ // src/index.ts
30
+ var index_exports = {};
31
+ __export(index_exports, {
32
+ default: () => index_default
33
+ });
34
+ module.exports = __toCommonJS(index_exports);
35
+ var import_extension_json_ast = __toESM(require("@coze-editor/extension-json-ast"));
36
+ var import_view = require("@codemirror/view");
37
+ var jsonHover = (options) => [
38
+ import_extension_json_ast.default,
39
+ (0, import_view.hoverTooltip)((view, pos) => {
40
+ const ast = view.state.field(import_extension_json_ast.default);
41
+ if (!ast) {
42
+ return null;
43
+ }
44
+ const node = (0, import_extension_json_ast.findNodeAtOffset)(ast, pos, true);
45
+ if (node && isKeyNode(node)) {
46
+ const tooltip = options.key({
47
+ paths: getPaths(node)
48
+ });
49
+ if (!tooltip) {
50
+ return null;
51
+ }
52
+ return {
53
+ pos: node.offset,
54
+ end: node.offset + node.length,
55
+ create() {
56
+ return tooltip;
57
+ }
58
+ };
59
+ }
60
+ return null;
61
+ })
62
+ ];
63
+ function isKeyNode(node) {
64
+ const parentNode = node == null ? void 0 : node.parent;
65
+ if (!node || !parentNode) {
66
+ return false;
67
+ }
68
+ return node.type === "string" && parentNode.type === "property" && parentNode.keyNode === node;
69
+ }
70
+ function isInProperty(node) {
71
+ var _a;
72
+ return ((_a = node == null ? void 0 : node.parent) == null ? void 0 : _a.type) === "property";
73
+ }
74
+ function isItemNode(node) {
75
+ const parentNode = node == null ? void 0 : node.parent;
76
+ if (!node || !parentNode) {
77
+ return false;
78
+ }
79
+ return parentNode.type === "array" && !!~parentNode.items.indexOf(node);
80
+ }
81
+ function getPaths(node) {
82
+ const paths = [];
83
+ let target = node;
84
+ while (target) {
85
+ if (isInProperty(target)) {
86
+ const key = target.parent.keyNode.value;
87
+ paths.unshift(key);
88
+ } else if (isItemNode(target)) {
89
+ const itemIndex = target.parent.items.indexOf(target);
90
+ paths.unshift(itemIndex);
91
+ }
92
+ target = target.parent;
93
+ }
94
+ return paths;
95
+ }
96
+ var index_default = jsonHover;
97
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/index.ts"],"sourcesContent":["// Copyright (c) 2025 coze-dev\n// SPDX-License-Identifier: MIT\n\nimport astField, {\n type ArrayASTNode,\n type ASTNode,\n findNodeAtOffset,\n type PropertyASTNode,\n} from '@coze-editor/extension-json-ast';\nimport { hoverTooltip, type TooltipView } from '@codemirror/view';\n\ntype JSONPath = string | number;\n\ninterface Options {\n key: (e: { paths: JSONPath[] }) => TooltipView | undefined;\n}\n\nconst jsonHover = (options: Options) => [\n astField,\n hoverTooltip((view, pos) => {\n const ast = view.state.field(astField);\n\n if (!ast) {\n return null;\n }\n\n const node = findNodeAtOffset(ast, pos, true);\n\n // hover on key\n if (node && isKeyNode(node)) {\n const tooltip = options.key({\n paths: getPaths(node),\n });\n\n if (!tooltip) {\n return null;\n }\n\n return {\n pos: node.offset,\n end: node.offset + node.length,\n create() {\n return tooltip;\n },\n };\n }\n\n return null;\n }),\n];\n\nfunction isKeyNode(node?: ASTNode): boolean {\n const parentNode = node?.parent;\n\n if (!node || !parentNode) {\n return false;\n }\n\n return (\n node.type === 'string' &&\n parentNode.type === 'property' &&\n parentNode.keyNode === node\n );\n}\n\nfunction isInProperty(node?: ASTNode): boolean {\n return node?.parent?.type === 'property';\n}\n\nfunction isItemNode(node?: ASTNode): boolean {\n const parentNode = node?.parent;\n\n if (!node || !parentNode) {\n return false;\n }\n\n return parentNode.type === 'array' && !!~parentNode.items.indexOf(node);\n}\n\nfunction getPaths(node: ASTNode): JSONPath[] {\n const paths: JSONPath[] = [];\n\n let target: ASTNode | undefined = node;\n while (target) {\n if (isInProperty(target)) {\n const key = (target.parent as PropertyASTNode).keyNode.value;\n paths.unshift(key);\n } else if (isItemNode(target)) {\n const itemIndex = (target.parent as ArrayASTNode).items.indexOf(target);\n paths.unshift(itemIndex);\n }\n\n target = target.par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SAAO,QAAQ;AACb,QAAI,aAAa,MAAM,GAAG;AACxB,YAAM,MAAO,OAAO,OAA2B,QAAQ;AACvD,YAAM,QAAQ,GAAG;AAAA,IACnB,WAAW,WAAW,MAAM,GAAG;AAC7B,YAAM,YAAa,OAAO,OAAwB,MAAM,QAAQ,MAAM;AACtE,YAAM,QAAQ,SAAS;AAAA,IACzB;AAEA,aAAS,OAAO;AAAA,EAClB;AAEA,SAAO;AACT;AAEA,IAAO,gBAAQ;","names":["astField"]}
package/package.json ADDED
@@ -0,0 +1,47 @@
1
+ {
2
+ "name": "@coze-editor/extension-json-hover",
3
+ "version": "0.1.0-alpha.09ffeb",
4
+ "description": "extension-json-hover",
5
+ "license": "MIT",
6
+ "author": "fengzilong",
7
+ "maintainers": [],
8
+ "sideEffects": [
9
+ "**/*.css",
10
+ "**/*.less",
11
+ "**/*.sass",
12
+ "**/*.scss"
13
+ ],
14
+ "main": "./dist/esm/index.js",
15
+ "module": "./dist/esm/index.js",
16
+ "types": "./dist/index.d.ts",
17
+ "files": [
18
+ "dist"
19
+ ],
20
+ "scripts": {
21
+ "build": "tsup",
22
+ "lint": "eslint && tsc --noEmit"
23
+ },
24
+ "dependencies": {
25
+ "@coze-editor/extension-json-ast": "0.1.0-alpha.09ffeb",
26
+ "@coze-editor/utils": "0.1.0-alpha.09ffeb"
27
+ },
28
+ "devDependencies": {
29
+ "@codemirror/state": "^6.4.1",
30
+ "@codemirror/view": "^6.26.1",
31
+ "@coze-arch/ts-config": "workspace:*",
32
+ "@coze-editor/eslint-config": "workspace:*",
33
+ "@types/node": "^22",
34
+ "eslint": "9.14.0",
35
+ "tsup": "^8.0.1",
36
+ "typescript": "^5.8.2"
37
+ },
38
+ "peerDependencies": {
39
+ "@codemirror/state": "^6.4.1",
40
+ "@codemirror/view": "^6.26.1"
41
+ },
42
+ "publishConfig": {
43
+ "access": "public",
44
+ "registry": "https://registry.npmjs.org"
45
+ },
46
+ "test:main": "./src/index.ts"
47
+ }