@onerjs/code-input 0.1.0 → 0.1.2
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/README.md +36 -1
- package/dist/code-input.cjs +22 -14
- package/dist/code-input.es.js +833 -680
- package/dist/components/CodeEditor.vue.d.ts +4 -1
- package/dist/core/types.d.ts +9 -0
- package/dist/index.d.ts +1 -1
- package/dist/style.css +1 -1
- package/package.json +7 -4
package/README.md
CHANGED
|
@@ -114,6 +114,7 @@ const completions: CompletionItem[] = [
|
|
|
114
114
|
| `language` | `'javascript' \| 'typescript'` | `'javascript'` | 语言,影响关键字集合 |
|
|
115
115
|
| `completions` | `CompletionItem[]` | `[]` | 自定义补全项(内置项会自动合并) |
|
|
116
116
|
| `tsTypes` | `string` | `''` | TS 类型声明代码,自动解析为补全项(见下文) |
|
|
117
|
+
| `completionFilter` | `CompletionFilter` | — | 自定义筛选 / 排序补全候选项(见下文) |
|
|
117
118
|
| `templateCompletions` | `CompletionItem[]` | `[]` | 模板模式专属补全项,展示优先级最高 |
|
|
118
119
|
| `templateOpen` | `string` | `'{{'` | 模板区间起始定界符 |
|
|
119
120
|
| `templateClose` | `string` | `'}}'` | 模板区间结束定界符 |
|
|
@@ -126,6 +127,7 @@ const completions: CompletionItem[] = [
|
|
|
126
127
|
| `defaultExpanded` | `boolean` | `false` | 折叠模式下是否默认展开 |
|
|
127
128
|
| `collapsedHeight` | `string` | `'34px'` | 单行模式高度(同时也是行高) |
|
|
128
129
|
| `collapseOnBlur` | `boolean` | `false` | 展开状态下失焦后是否自动收起 |
|
|
130
|
+
| `borderRadius` | `string` | `'4px'` | 输入框圆角 |
|
|
129
131
|
| `minHeight` | `string` | `'260px'` | 展开模式高度 |
|
|
130
132
|
|
|
131
133
|
## Events
|
|
@@ -211,8 +213,41 @@ declare function $notify(message: string, type?: 'success' | 'error'): void
|
|
|
211
213
|
- 成员区分属性 / 方法,方法生成 `(params) => ret` 签名;可选属性 `?` 会读取类型
|
|
212
214
|
- 变量的类型若指向已定义的 `interface / class / type`,会自动关联其类型定义与成员
|
|
213
215
|
- 成员类型再次指向某个对象类型时会递归展开,因此 `$store.user.` 也能提示 `User` 的成员
|
|
216
|
+
- **注释会解析为 `documentation`**:支持 `/** ... */`(JSDoc)与 `//`,成员与顶层声明均可;变量优先用自身注释,缺省时回退到其类型的注释;类型定义正文会剔除注释
|
|
217
|
+
- **解析结果按源代码字符串缓存**(LRU,上限 50 条),多个实例共用同一份 `tsTypes` 时只解析一次
|
|
214
218
|
- 也可单独调用:`import { parseTsCompletions } from './core/parser'`
|
|
215
219
|
|
|
220
|
+
## 自定义筛选补全
|
|
221
|
+
|
|
222
|
+
`completionFilter` 会在内置过滤(前缀匹配 / 打分排序)之后调用,可对最终候选做筛选、排序或改写。返回空数组则不弹出补全框。
|
|
223
|
+
|
|
224
|
+
```vue
|
|
225
|
+
<script setup lang="ts">
|
|
226
|
+
import type { CompletionFilter } from '@onerjs/code-input'
|
|
227
|
+
|
|
228
|
+
const completionFilter: CompletionFilter = (items, context) => {
|
|
229
|
+
// context: { prefix, chain, text, position, mode, language }
|
|
230
|
+
if (context.chain.length > 0) return items // 成员提示不处理
|
|
231
|
+
if (context.mode === 'template') return items.filter((i) => i.kind !== 'keyword')
|
|
232
|
+
return items.filter((i) => i.label.startsWith('$'))
|
|
233
|
+
}
|
|
234
|
+
</script>
|
|
235
|
+
|
|
236
|
+
<template>
|
|
237
|
+
<CodeEditor v-model="code" :completion-filter="completionFilter" />
|
|
238
|
+
</template>
|
|
239
|
+
```
|
|
240
|
+
|
|
241
|
+
`context` 字段:
|
|
242
|
+
|
|
243
|
+
| 字段 | 说明 |
|
|
244
|
+
| --- | --- |
|
|
245
|
+
| `items` | 内置过滤后的候选项(作为函数第一个参数) |
|
|
246
|
+
| `prefix` | 当前光标处的标识符前缀 |
|
|
247
|
+
| `chain` | 成员访问链,如 `$store.user.` 时为 `['$store', 'user']` |
|
|
248
|
+
| `text` / `position` | 当前完整文本与光标位置 |
|
|
249
|
+
| `mode` / `language` | 当前模式与语言 |
|
|
250
|
+
|
|
216
251
|
## 模板模式
|
|
217
252
|
|
|
218
253
|
模板模式下只有光标位于未闭合的 `{{ ... }}` 内才会触发代码补全,区间外输入不弹窗。
|
|
@@ -240,7 +275,7 @@ declare function $notify(message: string, type?: 'success' | 'error'): void
|
|
|
240
275
|
```
|
|
241
276
|
|
|
242
277
|
- 默认单行显示,行高等于输入框高度,文字垂直居中
|
|
243
|
-
-
|
|
278
|
+
- 点击右侧按钮展开为多行编辑器(含行号);展开时编辑器以浮层(绝对定位 + 缩放淡入动画 + 阴影)从输入框位置放大,**不会影响周围元素的布局**
|
|
244
279
|
- 单行模式下滚轮滚动按整行吸附
|
|
245
280
|
- 设置 `collapse-on-blur` 后:展开时若编辑器没有焦点会自动聚焦,失焦时自动收起(点击展开 / 收起按钮本身不会误触发)
|
|
246
281
|
|
package/dist/code-input.cjs
CHANGED
|
@@ -1,20 +1,28 @@
|
|
|
1
|
-
"use strict";Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const
|
|
1
|
+
"use strict";Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const n=require("vue"),Ve=[{label:"Math",kind:"class",detail:"Math",type:`interface Math {
|
|
2
2
|
readonly PI: number
|
|
3
3
|
readonly E: number
|
|
4
4
|
abs(x: number): number
|
|
5
5
|
round(x: number): number
|
|
6
6
|
max(...values: number[]): number
|
|
7
7
|
random(): number
|
|
8
|
-
}`,documentation:"内置数学对象",members:[{label:"PI",kind:"property",detail:"number"},{label:"E",kind:"property",detail:"number"},{label:"abs",kind:"function",detail:"(x: number) => number"},{label:"ceil",kind:"function",detail:"(x: number) => number"},{label:"floor",kind:"function",detail:"(x: number) => number"},{label:"round",kind:"function",detail:"(x: number) => number"},{label:"max",kind:"function",detail:"(...values: number[]) => number"},{label:"min",kind:"function",detail:"(...values: number[]) => number"},{label:"pow",kind:"function",detail:"(base: number, exp: number) => number"},{label:"random",kind:"function",detail:"() => number"},{label:"sqrt",kind:"function",detail:"(x: number) => number"},{label:"trunc",kind:"function",detail:"(x: number) => number"}]},{label:"JSON",kind:"class",detail:"JSON",members:[{label:"parse",kind:"function",detail:"(text: string) => any"},{label:"stringify",kind:"function",detail:"(value: any, space?: number) => string"}]},{label:"console",kind:"variable",detail:"Console",members:[{label:"log",kind:"function",detail:"(...data: any[]) => void"},{label:"info",kind:"function",detail:"(...data: any[]) => void"},{label:"warn",kind:"function",detail:"(...data: any[]) => void"},{label:"error",kind:"function",detail:"(...data: any[]) => void"},{label:"debug",kind:"function",detail:"(...data: any[]) => void"},{label:"table",kind:"function",detail:"(data: any) => void"},{label:"time",kind:"function",detail:"(label: string) => void"},{label:"timeEnd",kind:"function",detail:"(label: string) => void"}]},{label:"Object",kind:"class",detail:"Object",members:[{label:"keys",kind:"function",detail:"(o: object) => string[]"},{label:"values",kind:"function",detail:"(o: object) => any[]"},{label:"entries",kind:"function",detail:"(o: object) => [string, any][]"},{label:"assign",kind:"function",detail:"(target: object, ...sources: object[]) => object"},{label:"freeze",kind:"function",detail:"(o: object) => object"}]},{label:"Array",kind:"class",detail:"Array",members:[{label:"isArray",kind:"function",detail:"(value: any) => boolean"},{label:"from",kind:"function",detail:"(iterable: any) => any[]"},{label:"of",kind:"function",detail:"(...items: any[]) => any[]"}]},{label:"Promise",kind:"class",detail:"Promise",members:[{label:"resolve",kind:"function",detail:"(value?: any) => Promise<any>"},{label:"reject",kind:"function",detail:"(reason?: any) => Promise<never>"},{label:"all",kind:"function",detail:"(values: any[]) => Promise<any[]>"},{label:"race",kind:"function",detail:"(values: any[]) => Promise<any>"}]},{label:"Date",kind:"class",detail:"Date",members:[{label:"now",kind:"function",detail:"() => number"},{label:"parse",kind:"function",detail:"(dateString: string) => number"}]},{label:"String",kind:"class",detail:"String",members:[{label:"fromCharCode",kind:"function",detail:"(...codes: number[]) => string"},{label:"from",kind:"function",detail:"(value: any) => string"}]},{label:"Number",kind:"class",detail:"Number",members:[{label:"isNaN",kind:"function",detail:"(value: any) => boolean"},{label:"isInteger",kind:"function",detail:"(value: any) => boolean"},{label:"parseFloat",kind:"function",detail:"(value: string) => number"},{label:"parseInt",kind:"function",detail:"(value: string, radix?: number) => number"}]},{label:"setTimeout",kind:"function",detail:"(callback: Function, ms?: number) => number"},{label:"clearTimeout",kind:"function",detail:"(id: number) => void"},{label:"setInterval",kind:"function",detail:"(callback: Function, ms?: number) => number"},{label:"clearInterval",kind:"function",detail:"(id: number) => void"}],
|
|
9
|
-
`)
|
|
10
|
-
`).replace(/^\s
|
|
11
|
-
`).replace(
|
|
12
|
-
|
|
13
|
-
`).
|
|
14
|
-
|
|
15
|
-
`)
|
|
16
|
-
|
|
17
|
-
`}),
|
|
18
|
-
`).
|
|
19
|
-
|
|
20
|
-
|
|
8
|
+
}`,documentation:"内置数学对象",members:[{label:"PI",kind:"property",detail:"number"},{label:"E",kind:"property",detail:"number"},{label:"abs",kind:"function",detail:"(x: number) => number"},{label:"ceil",kind:"function",detail:"(x: number) => number"},{label:"floor",kind:"function",detail:"(x: number) => number"},{label:"round",kind:"function",detail:"(x: number) => number"},{label:"max",kind:"function",detail:"(...values: number[]) => number"},{label:"min",kind:"function",detail:"(...values: number[]) => number"},{label:"pow",kind:"function",detail:"(base: number, exp: number) => number"},{label:"random",kind:"function",detail:"() => number"},{label:"sqrt",kind:"function",detail:"(x: number) => number"},{label:"trunc",kind:"function",detail:"(x: number) => number"}]},{label:"JSON",kind:"class",detail:"JSON",members:[{label:"parse",kind:"function",detail:"(text: string) => any"},{label:"stringify",kind:"function",detail:"(value: any, space?: number) => string"}]},{label:"console",kind:"variable",detail:"Console",members:[{label:"log",kind:"function",detail:"(...data: any[]) => void"},{label:"info",kind:"function",detail:"(...data: any[]) => void"},{label:"warn",kind:"function",detail:"(...data: any[]) => void"},{label:"error",kind:"function",detail:"(...data: any[]) => void"},{label:"debug",kind:"function",detail:"(...data: any[]) => void"},{label:"table",kind:"function",detail:"(data: any) => void"},{label:"time",kind:"function",detail:"(label: string) => void"},{label:"timeEnd",kind:"function",detail:"(label: string) => void"}]},{label:"Object",kind:"class",detail:"Object",members:[{label:"keys",kind:"function",detail:"(o: object) => string[]"},{label:"values",kind:"function",detail:"(o: object) => any[]"},{label:"entries",kind:"function",detail:"(o: object) => [string, any][]"},{label:"assign",kind:"function",detail:"(target: object, ...sources: object[]) => object"},{label:"freeze",kind:"function",detail:"(o: object) => object"}]},{label:"Array",kind:"class",detail:"Array",members:[{label:"isArray",kind:"function",detail:"(value: any) => boolean"},{label:"from",kind:"function",detail:"(iterable: any) => any[]"},{label:"of",kind:"function",detail:"(...items: any[]) => any[]"}]},{label:"Promise",kind:"class",detail:"Promise",members:[{label:"resolve",kind:"function",detail:"(value?: any) => Promise<any>"},{label:"reject",kind:"function",detail:"(reason?: any) => Promise<never>"},{label:"all",kind:"function",detail:"(values: any[]) => Promise<any[]>"},{label:"race",kind:"function",detail:"(values: any[]) => Promise<any>"}]},{label:"Date",kind:"class",detail:"Date",members:[{label:"now",kind:"function",detail:"() => number"},{label:"parse",kind:"function",detail:"(dateString: string) => number"}]},{label:"String",kind:"class",detail:"String",members:[{label:"fromCharCode",kind:"function",detail:"(...codes: number[]) => string"},{label:"from",kind:"function",detail:"(value: any) => string"}]},{label:"Number",kind:"class",detail:"Number",members:[{label:"isNaN",kind:"function",detail:"(value: any) => boolean"},{label:"isInteger",kind:"function",detail:"(value: any) => boolean"},{label:"parseFloat",kind:"function",detail:"(value: string) => number"},{label:"parseInt",kind:"function",detail:"(value: string, radix?: number) => number"}]},{label:"setTimeout",kind:"function",detail:"(callback: Function, ms?: number) => number"},{label:"clearTimeout",kind:"function",detail:"(id: number) => void"},{label:"setInterval",kind:"function",detail:"(callback: Function, ms?: number) => number"},{label:"clearInterval",kind:"function",detail:"(id: number) => void"}],Be=["break","case","catch","class","const","continue","debugger","default","delete","do","else","export","extends","finally","for","from","function","if","import","in","instanceof","let","new","of","return","super","switch","this","throw","try","typeof","var","void","while","with","yield","async","await","true","false","null","undefined","NaN","Infinity"],ht=["abstract","any","as","asserts","bigint","boolean","declare","enum","implements","infer","interface","is","keyof","namespace","never","number","object","override","private","protected","public","readonly","satisfies","static","string","symbol","type","unknown"];function fe(o){return o==="typescript"?[...Be,...ht]:Be}function ue(o){return new Set(fe(o))}const gt=/((?:[A-Za-z_$][\w$]*\??\.)*)([A-Za-z_$][\w$]*)?$/,Ie=/\b(?:const|let|var|function|class)\s+([A-Za-z_$][\w$]*)/g,bt=/^[A-Za-z_$]/;function Oe(o,s){const r=o.slice(0,s),t=gt.exec(r);if(!t)return{prefix:"",chain:[]};const i=t[1]??"",u=t[2]??"",d=i.split(".").filter(Boolean).map(f=>f.replace(/\?$/,""));return{prefix:u,chain:d}}function de(o,s){const r=[],t=new Set;let i;for(Ie.lastIndex=0;(i=Ie.exec(o))&&!(i.index>=s);){const u=i[1];if(t.has(u))continue;t.add(u);const d=i[0].slice(0,i[0].indexOf(u)).trim();r.push({label:u,kind:d==="function"?"function":d==="class"?"class":"variable",detail:"当前文件"})}return r}function ze(o,s){let r=s;for(const t of o){if(!r)return null;const i=r.find(u=>u.label===t);if(!i)return null;r=i.members??[]}return r}function vt(o,s){const r=o.toLowerCase(),t=s.toLowerCase(),i=r.indexOf(t);if(i===0)return 1e3-r.length;if(i>0)return 800-i;let u=0,d=0,f=-1;for(let a=0;a<r.length&&u<t.length;a++)r[a]===t[u]&&(f>=0&&(d+=a-f-1),f=a,u++);return u!==t.length?null:400-d-r.length}function yt(o,s){if(!s)return o;const r=[];for(const t of o){const i=vt(t.label,s);i!==null&&r.push({item:t,score:i})}return r.sort((t,i)=>i.score-t.score||t.item.label.localeCompare(i.item.label)),r.map(t=>t.item)}function kt(o){const s=new Set,r=[];for(const t of o)s.has(t.label)||(s.add(t.label),r.push(t));return r}function De(o,s,r){const{prefix:t,chain:i}=Oe(o,s);if(r.auto&&(!t&&i.length===0||t&&!bt.test(t)))return null;const u=r.declared??[],d=[...u,...r.completions];let f;if(i.length>0){const m=ze(i,d);if(!m||m.length===0)return null;f=m}else f=[...r.completions,...u,...fe(r.language).map(m=>({label:m,kind:"keyword"}))];const a=yt(kt(f),t);return a.length===0?null:{items:a,start:s-t.length,end:s}}const xt=/([A-Za-z_$][\w$]*(?:\??\.[A-Za-z_$][\w$]*)*)\??\.\s*$/;function je(o,s,r,t){const i=o.slice(s,r);if(!i)return null;const u=[...t.declared??[],...t.completions],d=xt.exec(o.slice(0,s));if(d){const f=d[1].split(".").filter(Boolean).map($=>$.replace(/\?$/,"")),a=ze(f,u),m=a==null?void 0:a.find($=>$.label===i);if(m)return m}return u.find(f=>f.label===i)??null}const z="[A-Za-z_$][\\w$]*",Me="(?:readonly\\s+|public\\s+|private\\s+|protected\\s+|static\\s+|abstract\\s+)*";function Re(o,s){let r=0,t=!1,i=s;for(let u=s;u<o.length;u++){const d=o[u];for(let f=0;f<d.length;f++){const a=d[f];a==="{"?(r++,t=!0):a==="}"&&r--}if(t&&r<=0){i=u;break}}return{end:i,body:o.slice(s+1,i)}}function _e(o){return o.trim().replace(/[,;]$/,"").trim()}function wt(o){return/^(?:export\s+)?(?:declare\s+)?(?:abstract\s+)?(?:interface|class|type|function|const|let|var|namespace|enum)\b/.test(o)}function U(o,s){const r=o[s];let t=r.lastIndexOf("}");const i=r.lastIndexOf(";");if(i>t&&(t=i),t===-1)return;const u=r.slice(t+1).replace(/^[\s;]+/,"").trim();!u||!wt(u)||(o[s]=r.slice(0,t+1),o.splice(s+1,0,u))}function j(o){return o.length?o.join(`
|
|
9
|
+
`).replace(/^\s*\/\*\*?/,"").replace(/\*\/\s*$/,"").split(`
|
|
10
|
+
`).map(s=>s.replace(/^\s*\*+\s?/,"").replace(/^\s*\/\/\s?/,"")).join(`
|
|
11
|
+
`).replace(/\n{3,}/g,`
|
|
12
|
+
|
|
13
|
+
`).trim():""}function He(o){return o.split(`
|
|
14
|
+
`).filter(s=>!/^\s*(\/\/|\/\*|\*)/.test(s)).join(`
|
|
15
|
+
`).replace(/\n{2,}/g,`
|
|
16
|
+
`).trim()}function Ne(o){const s=[],r=new Set,t=new RegExp(`^${Me}(${z})(\\?)?\\s*\\((.*)\\)\\s*:\\s*(.+?);?$`),i=new RegExp(`^${Me}(${z})(\\?)?\\s*:\\s*(.+?);?$`);let u=[],d=!1;for(const f of o){const a=f.trim();if(d){u.push(a),a.includes("*/")&&(d=!1);continue}if(a.startsWith("/*")){u.push(a),a.includes("*/")||(d=!0);continue}if(a.startsWith("//")){u.push(a);continue}if(!a||a.startsWith("}")||a.startsWith("[")){u=[];continue}const m=t.exec(a);if(m){const L=j(u);u=[];const v=m[1];if(r.has(v))continue;r.add(v),s.push({label:v,kind:"function",detail:`(${m[3].trim()}) => ${_e(m[4])}`,documentation:L||void 0});continue}const $=i.exec(a);if($){const L=j(u);u=[];const v=$[1];if(r.has(v))continue;r.add(v);const b=_e($[3]),I=/^\(|=>/.test(b)?"function":"property";s.push({label:v,kind:I,detail:b,documentation:L||void 0})}}return s}function We(o){const s=o.replace(/\[\]/g,"").replace(/<[^>]*>/g,"").replace(/\s*\|\s*null\s*/g,"").trim(),r=new RegExp(`^(${z})$`).exec(s);return r?r[1]:null}function Et(o){const s=o.trim();if(!s||s.endsWith(";"))return!1;const r=(s.match(/[(<[{]/g)??[]).length,t=(s.match(/[)>\]}]/g)??[]).length;return/[|&,(<]\s*$/.test(s)||r>t}function Fe(o,s,r,t=0){return t>6?o:o.map(i=>{if(i.kind!=="property"||!i.detail)return i;const u=We(i.detail),d=u?s.get(u):void 0;if(!d||r.has(d.label))return i;const f=new Set(r);return f.add(d.label),{...i,type:d.type,members:Fe(d.members,s,f,t+1)}})}const Ct=50,Y=new Map;function Pe(o){if(!o||!o.trim())return[];const s=Y.get(o);if(s)return s;const r=Tt(o);if(Y.size>=Ct){const t=Y.keys().next().value;t!==void 0&&Y.delete(t)}return Y.set(o,r),r}function Tt(o){const s=o.split(`
|
|
17
|
+
`),r=new Map,t=new RegExp(`^(?:export\\s+)?(?:declare\\s+)?interface\\s+(${z})`),i=new RegExp(`^(?:export\\s+)?(?:declare\\s+)?(?:abstract\\s+)?class\\s+(${z})`),u=new RegExp(`^(?:export\\s+)?(?:declare\\s+)?type\\s+(${z})\\s*=`);let d=[],f=!1;for(let v=0;v<s.length;v++){const b=s[v].trim();if(f){d.push(b),b.includes("*/")&&(f=!1);continue}if(b.startsWith("/*")){d.push(b),b.includes("*/")||(f=!0);continue}if(b.startsWith("//")){d.push(b);continue}if(!b){d=[];continue}const I=t.exec(b);if(I&&b.includes("{")){const E=j(d);d=[];const{end:k,body:T}=Re(s,v),x=I[1];r.set(x,{label:x,type:He(s.slice(v,k+1).join(`
|
|
18
|
+
`).replace(/^\s*export\s+/,"")),members:Ne(T),kind:"interface",documentation:E||void 0}),U(s,k),v=k;continue}const H=i.exec(b);if(H&&b.includes("{")){const E=j(d);d=[];const{end:k,body:T}=Re(s,v),x=H[1];r.set(x,{label:x,type:He(s.slice(v,k+1).join(`
|
|
19
|
+
`).replace(/^\s*export\s+/,"")),members:Ne(T),kind:"class",documentation:E||void 0}),U(s,k),v=k;continue}const M=u.exec(b);if(M){const E=j(d);d=[];const k=[s[v]];let T=v;for(;Et(k.join(`
|
|
20
|
+
`))&&T+1<s.length;)T++,k.push(s[T]);const x=k.join(`
|
|
21
|
+
`).replace(/^\s*export\s+/,"").replace(/;\s*$/,"").trim();r.set(M[1],{label:M[1],type:x,members:[],kind:"type",documentation:E||void 0}),U(s,T),v=T;continue}d=[]}const a=[],m=new Set,$=new RegExp(`^(?:export\\s+)?(?:declare\\s+)?function\\s+(${z})\\s*\\(([^)]*)\\)\\s*:\\s*([^;{]+)`),L=new RegExp(`^(?:export\\s+)?(?:declare\\s+)?(?:const|let|var)\\s+(${z})\\s*:\\s*([^=;]+)`);d=[],f=!1;for(let v=0;v<s.length;v++){const b=s[v].trim();if(f){d.push(b),b.includes("*/")&&(f=!1);continue}if(b.startsWith("/*")){d.push(b),b.includes("*/")||(f=!0);continue}if(b.startsWith("//")){d.push(b);continue}if(!b){d=[];continue}const I=$.exec(b);if(I){U(s,v);const M=j(d);d=[];const E=I[1];m.has(E)||(m.add(E),a.push({label:E,kind:"function",detail:`(${I[2].trim()}) => ${I[3].trim()}`,documentation:M||void 0}));continue}const H=L.exec(b);if(H){U(s,v);const M=j(d);d=[];const E=H[1];if(m.has(E))continue;m.add(E);const k=H[2].trim(),T=We(k),x=T?r.get(T):void 0,C=(x==null?void 0:x.kind)==="class"?"class":/^\(|=>/.test(k)?"function":"variable";a.push({label:E,kind:C,detail:k,type:x==null?void 0:x.type,documentation:M||(x==null?void 0:x.documentation)||void 0,members:x?Fe(x.members,r,new Set([x.label])):void 0});continue}d=[]}return a}function ee(o,s,r,t){if(!r||!t)return null;const i=Math.max(0,s-1),u=o.lastIndexOf(r,i);if(u===-1||o.lastIndexOf(t,i)>u)return null;const f=o.indexOf(t,u+r.length);return{start:u+r.length,end:f===-1?o.length:f}}function St(o,s,r,t){return ee(o,s,r,t)!==null}const Ae=/[A-Za-z_$]/,$t=/[\w$]/,Le=/\s/;function J(o,s){const r=[],t=o.length;let i=0;for(;i<t;){const u=o[i],d=o[i+1];if(Le.test(u)){i++;continue}if(u==="/"&&d==="/"){let a=o.indexOf(`
|
|
22
|
+
`,i);a===-1&&(a=t),r.push({start:i,end:a,type:"comment"}),i=a;continue}if(u==="/"&&d==="*"){const a=o.indexOf("*/",i+2),m=a===-1?t:a+2;r.push({start:i,end:m,type:"comment"}),i=m;continue}if(u==='"'||u==="'"){let a=i+1;for(;a<t;){if(o[a]==="\\"){a+=2;continue}if(o[a]===`
|
|
23
|
+
`)break;if(o[a]===u){a++;break}a++}r.push({start:i,end:a,type:"string"}),i=a;continue}if(u==="`"){let a=i+1;for(;a<t;){if(o[a]==="\\"){a+=2;continue}if(o[a]==="`"){a++;break}a++}r.push({start:i,end:a,type:"template"}),i=a;continue}if(/[0-9]/.test(u)||u==="."&&/[0-9]/.test(d??"")){let a=i+1;for(;a<t&&/[\w.]/.test(o[a]);)a++;r.push({start:i,end:a,type:"number"}),i=a;continue}if(Ae.test(u)){let a=i+1;for(;a<t&&$t.test(o[a]);)a++;const m=o.slice(i,a),$=s.keywords.has(m)?"keyword":s.builtins.has(m)?"builtin":"identifier";r.push({start:i,end:a,type:$}),i=a;continue}let f=i+1;for(;f<t;){const a=o[f];if(Le.test(a)||Ae.test(a)||/[0-9]/.test(a)||a==='"'||a==="'"||a==="`"||a==="/")break;f++}r.push({start:i,end:f,type:"punctuation"}),i=f}return r}const Bt={"&":"&","<":"<",">":">"};function W(o){return o.replace(/[&<>]/g,s=>Bt[s]??s)}function G(o,s,r=0){let t="",i=0;for(const u of s){u.start>i&&(t+=W(o.slice(i,u.start)));const d=W(o.slice(u.start,u.end));t+=`<span class="tok-${u.type}" data-start="${u.start+r}" data-end="${u.end+r}">${d}</span>`,i=u.end}return i<o.length&&(t+=W(o.slice(i))),t}function Ke(o,s,r,t){if(!r||!t)return G(o,J(o,s));let i="",u=0;for(;u<o.length;){const d=o.indexOf(r,u);if(d===-1){i+=W(o.slice(u));break}i+=W(o.slice(u,d));const f=o.indexOf(t,d+r.length),a=f===-1?o.length:f,m=o.slice(d+r.length,a);i+=`<span class="tok-brace">${W(r)}</span>`,i+=G(m,J(m,s),d+r.length),f===-1?u=a:(i+=`<span class="tok-brace">${W(t)}</span>`,u=f+t.length)}return i}const It={key:0,class:"gutter","aria-hidden":"true"},Mt=["innerHTML"],Rt=["value","readonly","placeholder"],_t=["onMouseenter","onClick"],Ht={class:"popup-label"},Nt={key:0,class:"popup-detail"},At={class:"hover-head"},Lt={class:"hover-name"},Vt={key:0,class:"hover-colon"},Ot={key:0,class:"hover-type"},zt={key:0,class:"hover-doc"},Dt={key:1,class:"hover-def"},jt=["innerHTML"],Wt=["title","aria-label"],Ft={key:0,viewBox:"0 0 16 16",width:"14",height:"14","aria-hidden":"true"},Pt={key:1,viewBox:"0 0 16 16",width:"14",height:"14","aria-hidden":"true"},Kt=n.defineComponent({__name:"CodeEditor",props:{modelValue:{},mode:{default:"code"},language:{default:"javascript"},completions:{default:()=>[]},tsTypes:{default:""},completionFilter:{},templateCompletions:{default:()=>[]},templateOpen:{default:"{{"},templateClose:{default:"}}"},placeholder:{default:""},readonly:{type:Boolean,default:!1},lineNumbers:{type:Boolean,default:!0},hover:{type:Boolean,default:!0},theme:{default:"light"},collapsible:{type:Boolean,default:!1},defaultExpanded:{type:Boolean,default:!1},collapsedHeight:{default:"34px"},collapseOnBlur:{type:Boolean,default:!1},borderRadius:{default:"4px"},minHeight:{default:"260px"}},emits:["update:modelValue","change","expand","collapse"],setup(o,{expose:s,emit:r}){const t=o,i=r,u={variable:"变量",function:"函数",property:"属性",class:"对象",keyword:"关键字"},d={"(":")","[":"]","{":"}",'"':'"',"'":"'"},f=new Set(["ArrowLeft","ArrowRight","ArrowUp","ArrowDown","Home","End","PageUp","PageDown"]),a=n.ref(t.modelValue),m=n.ref(),$=n.ref(),L=n.ref(),v=n.ref(),b=n.ref(),I=n.ref(),H=n.ref(),M=n.ref([]),E=n.ref(0),k=n.ref(!1),T=n.ref({start:0,end:0}),x=n.ref({}),C=n.ref(null),pe=n.ref({}),qe=n.computed(()=>t.tsTypes?Pe(t.tsTypes):[]),me=n.computed(()=>[...Ve,...qe.value,...t.completions]),te=n.computed(()=>t.mode==="template"?[...t.templateCompletions,...me.value]:me.value),Ue=n.computed(()=>{const e={keywords:ue(t.language),builtins:new Set(te.value.map(l=>l.label))};return t.mode==="template"?`${Ke(a.value,e,t.templateOpen,t.templateClose)}
|
|
24
|
+
`:`${G(a.value,J(a.value,e))}
|
|
25
|
+
`}),Ye=n.computed(()=>a.value.split(`
|
|
26
|
+
`).length),N=n.ref(t.defaultExpanded),ne=n.ref(!1),le=n.computed(()=>t.collapsible&&!N.value),Je=n.computed(()=>{const e={};return t.collapsible&&(e.height=t.collapsedHeight),e}),Ge=n.computed(()=>{const e={"--ce-radius":t.borderRadius,"--ce-collapsed-height":t.collapsedHeight};return t.collapsible?{...e,height:N.value?t.minHeight:t.collapsedHeight}:{...e,height:t.minHeight}});let he=0;function ge(e){if(N.value===e)return;const l=m.value;e&&l&&(he=l.scrollTop),N.value=e,t.collapsible&&(ne.value=!0),i(e?"expand":"collapse"),S(),_(),n.nextTick(()=>{if(oe(),!N.value&&l){const c=Math.max(0,l.scrollHeight-l.clientHeight);l.scrollTop=Math.max(0,Math.min(he,c)),A(!0),document.activeElement===l&&P();return}A(!0),e&&t.collapseOnBlur&&l&&document.activeElement!==l&&l.focus()})}function Xe(){ne.value=!1}function Qe(){ge(!N.value)}n.watch(()=>t.modelValue,e=>{e!==a.value&&(a.value=e,S())}),n.watch(a,()=>{n.nextTick(()=>A(!0))});function be(e){return u[e]}function et(e){return e?G(e,J(e,{keywords:ue("typescript"),builtins:new Set})):""}let F={top:14,bottom:14,lineHeight:22};function oe(){const e=m.value;if(!e)return;const l=window.getComputedStyle(e);F={top:parseFloat(l.paddingTop)||0,bottom:parseFloat(l.paddingBottom)||0,lineHeight:parseFloat(l.lineHeight)||22}}function A(e=!1){const l=m.value;if(!l)return;const c=v.value;if(c&&(c.style.transform=`translate(${-l.scrollLeft}px, ${-l.scrollTop}px)`),b.value&&(b.value.style.transform=`translateY(${-l.scrollTop}px)`),e){const h=`${a.value}|${t.mode}|${l.clientWidth}|${l.clientHeight}`;h!==ye&&(ye=h,tt())}}function P(){const e=m.value;if(!e||!le.value)return;const l=ke();if(!l)return;const c=e.clientHeight-F.top-F.bottom,g=l.top+e.scrollTop-F.top-Math.max(0,(c-l.lineHeight)/2),p=Math.max(0,e.scrollHeight-e.clientHeight),y=Math.max(0,Math.min(g,p));Math.abs(y-e.scrollTop)>.5&&(e.scrollTop=y,A())}let ve=[],ye="",X=0,ie=-1,ae=-1,re={x:0,y:0};function tt(){const e=m.value,l=L.value;if(!e||!l)return;const c=e.getBoundingClientRect(),h=[];l.querySelectorAll("[data-start]").forEach(g=>{const p=g.getBoundingClientRect();h.push({start:Number(g.dataset.start),end:Number(g.dataset.end),left:p.left-c.left+e.scrollLeft,right:p.right-c.left+e.scrollLeft,top:p.top-c.top+e.scrollTop,bottom:p.bottom-c.top+e.scrollTop})}),ve=h}function nt(e,l){for(const c of ve)if(e>=c.left-1&&e<=c.right+1&&l>=c.top&&l<=c.bottom)return c;return null}function _(){C.value=null,X=0,ie=-1,ae=-1}function lt(e){const l=H.value,c=m.value;if(!l||!c)return;const h=c.getBoundingClientRect(),g=h.left+e.left-c.scrollLeft,p=h.left+e.right-c.scrollLeft,y=h.top+e.top-c.scrollTop,w=h.top+e.bottom-c.scrollTop,B=12,R=8,V=l.offsetWidth,Z=l.offsetHeight;let O=g,D=w+R;O+V>window.innerWidth-B&&(O=p-V),D+Z>window.innerHeight-B&&(D=y-Z-R),O=Math.max(B,Math.min(O,window.innerWidth-B-V)),D=Math.max(B,D),pe.value={left:`${O}px`,top:`${D}px`}}function ot(e,l){const c=m.value;if(!c||t.readonly||!t.hover||k.value){_();return}if(c.selectionStart!==c.selectionEnd){_();return}const h=H.value;if(C.value&&h){const w=h.getBoundingClientRect();if(e>=w.left&&e<=w.right&&l>=w.top&&l<=w.bottom)return}const g=c.getBoundingClientRect(),p=nt(e-g.left+c.scrollLeft,l-g.top+c.scrollTop);if(!p){_();return}if(C.value&&ie===p.start&&ae===p.end)return;const y=je(c.value,p.start,p.end,{completions:te.value,declared:de(c.value,p.start)});if(!y){_();return}C.value=y,ie=p.start,ae=p.end,n.nextTick(()=>lt(p))}function it(e){re={x:e.clientX,y:e.clientY},!X&&(X=requestAnimationFrame(()=>{X=0,ot(re.x,re.y)}))}function ke(){const e=m.value;if(!e)return null;const l=window.getComputedStyle(e),c=document.createElement("div"),h=parseFloat(l.paddingLeft)||0,g=parseFloat(l.paddingRight)||0,p=e.getBoundingClientRect();Object.assign(c.style,{position:"fixed",left:`${p.left}px`,top:`${p.top}px`,visibility:"hidden",pointerEvents:"none",zIndex:"-1",whiteSpace:"pre-wrap",overflowWrap:"break-word",wordBreak:"normal",boxSizing:"content-box",width:`${e.clientWidth-h-g}px`,paddingTop:l.paddingTop,paddingRight:l.paddingRight,paddingBottom:l.paddingBottom,paddingLeft:l.paddingLeft,border:"0",fontFamily:l.fontFamily,fontSize:l.fontSize,fontWeight:l.fontWeight,fontStyle:l.fontStyle,fontVariant:l.fontVariant,letterSpacing:l.letterSpacing,lineHeight:l.lineHeight,textTransform:l.textTransform,tabSize:l.tabSize}),c.textContent=e.value.slice(0,e.selectionStart);const y=document.createElement("span");y.textContent="",c.appendChild(y),document.body.appendChild(c);const w=y.getBoundingClientRect();document.body.removeChild(c);const B=parseFloat(l.lineHeight)||22,R=w.height,V=R>0&&R<B?(B-R)/2:0;return{left:w.left-p.left-e.scrollLeft,top:w.top-p.top-e.scrollTop-V,lineHeight:B}}async function at(){await n.nextTick();const e=$.value,l=I.value,c=ke();if(!e||!l||!c)return;const h=4,g=8,p=window.innerWidth,y=window.innerHeight,w=e.getBoundingClientRect(),B=l.scrollHeight,R=y-(w.top+c.top+c.lineHeight)-h-g,V=w.top+c.top-h-g,Z=R>=Math.min(B,180)||R>=V,O=Math.min(248,Math.max(Z?R:V,96)),D=Math.min(B,O),$e=l.offsetWidth||280;let q=c.left;w.left+q+$e>p-g&&(q=p-g-$e-w.left),q=Math.max(q,g-w.left),x.value={left:`${q}px`,top:`${Z?c.top+c.lineHeight+h:c.top-h-D}px`,maxHeight:`${O}px`}}function S(){k.value=!1,M.value=[],E.value=0,_()}function K(e=!1){const l=m.value;if(!l||t.readonly)return;if(l.selectionStart!==l.selectionEnd){S();return}const c=l.selectionStart;let h=!e;if(t.mode==="template"){const y=ee(l.value,c,t.templateOpen,t.templateClose);if(!y){S();return}e&&l.value.slice(y.start,c).trim()===""&&(h=!0)}const g=De(l.value,c,{language:t.language,completions:te.value,declared:de(l.value,c),auto:!h});if(!g){S();return}let p=g.items;if(t.completionFilter){const y=Oe(l.value,c);if(p=t.completionFilter(p,{prefix:y.prefix,chain:y.chain,text:l.value,position:c,mode:t.mode,language:t.language}),p.length===0){S();return}}M.value=p,T.value={start:g.start,end:g.end},E.value=0,k.value=!0,at()}function rt(e){a.value=e,i("update:modelValue",e),i("change",e)}function Q(e,l,c,h=e+c.length){const g=m.value;if(!g)return;const p=g.value.slice(0,e)+c+g.value.slice(l);rt(p),S(),n.nextTick(()=>{g.value=p,g.setSelectionRange(h,h),g.focus(),A(!0),P()})}function xe(e=M.value[E.value]){if(!e)return;const l=e.insertText??e.label;Q(T.value.start,T.value.end,l),S()}function we(e){const l=M.value.length;l&&(E.value=(E.value+e+l)%l,n.nextTick(()=>{var c,h;(h=(c=I.value)==null?void 0:c.querySelector(".is-active"))==null||h.scrollIntoView({block:"nearest"})}))}function st(e){const l=e.target;a.value=l.value,i("update:modelValue",l.value),i("change",l.value),_(),A(),n.nextTick(P),K(!0)}function ct(e){if(S(),_(),!t.collapsible||!t.collapseOnBlur||!N.value)return;const l=e.relatedTarget;l&&l.closest(".expand-toggle")||ge(!1)}function ut(){Ee(),P()}function Ee(){const e=m.value;if(!e||t.readonly)return;if(e.selectionStart!==e.selectionEnd){S();return}const l=e.selectionStart;if(l>0&&e.value[l-1]==="."){K(!0);return}if(t.mode==="template"){const c=ee(e.value,l,t.templateOpen,t.templateClose);if(c&&e.value.slice(c.start,l).trim()===""){K(!0);return}}S()}let se=0;function Ce(){window.clearTimeout(se),se=window.setTimeout(()=>{se=0,Ee(),P()},0)}function Te(e){e.target!==m.value&&k.value&&S()}function dt(){_(),A(),le.value&&ft()}let ce=0;function ft(){window.clearTimeout(ce),ce=window.setTimeout(()=>{ce=0,pt()},120)}function pt(){const e=m.value;if(!e)return;const l=F.lineHeight,c=e.scrollTop,h=Math.round(c/l)*l;Math.abs(h-c)<.5||(e.scrollTop=h,A())}function mt(e){var g;const l=m.value;if(!l)return;const c=e.metaKey||e.ctrlKey,h=f.has(e.key);if(e.key===" "&&c){e.preventDefault(),K(!1);return}if(k.value){if(e.key==="ArrowDown"){e.preventDefault(),we(1);return}if(e.key==="ArrowUp"){e.preventDefault(),we(-1);return}if(e.key==="Enter"||e.key==="Tab"){e.preventDefault(),xe();return}if(e.key==="Escape"){e.preventDefault(),S();return}if(h||c){S(),h&&!c&&Ce();return}}if(h&&!c){Ce();return}if(e.key==="Enter"&&!c&&!e.shiftKey){const p=l.selectionStart;if(p!==l.selectionEnd)return;const y=l.value.lastIndexOf(`
|
|
27
|
+
`,p-1)+1,w=l.value.slice(y,p),B=((g=w.match(/^[ \t]*/))==null?void 0:g[0])??"",R=/[{[(]$/.test(w.trimEnd())?" ":"";e.preventDefault(),Q(p,p,`
|
|
28
|
+
${B}${R}`);return}if(e.key==="Tab"){e.preventDefault(),Q(l.selectionStart,l.selectionEnd," ");return}if(e.key.length===1&&!c&&!e.altKey&&d[e.key]){if(l.selectionStart!==l.selectionEnd)return;const p=l.selectionStart,y=l.value[p-1],w=l.value[p];if((e.key==='"'||e.key==="'")&&y&&/[\w$]/.test(y)||w!==void 0&&!/[\s)\]},;:]/.test(w))return;e.preventDefault(),Q(p,p,e.key+d[e.key],p+1),t.mode==="template"&&n.nextTick(()=>K(!0))}}function Se(){k.value&&S(),_(),n.nextTick(()=>{oe(),A(!0)})}return n.onMounted(()=>{window.addEventListener("resize",Se),window.addEventListener("scroll",Te,!0),n.nextTick(()=>{oe(),A(!0)})}),n.onBeforeUnmount(()=>{window.removeEventListener("resize",Se),window.removeEventListener("scroll",Te,!0)}),s({focus:()=>{var e;return(e=m.value)==null?void 0:e.focus()}}),(e,l)=>(n.openBlock(),n.createElementBlock("div",{class:n.normalizeClass(["code-input",{"is-collapsible":o.collapsible}]),style:n.normalizeStyle(Je.value)},[n.createElementVNode("div",{class:n.normalizeClass(["code-editor",{"is-readonly":o.readonly,"is-dark":o.theme==="dark","is-collapsed":le.value,"is-expanding":ne.value}]),style:n.normalizeStyle(Ge.value),onAnimationend:n.withModifiers(Xe,["self"])},[o.lineNumbers?(n.openBlock(),n.createElementBlock("div",It,[n.createElementVNode("div",{ref_key:"gutterInner",ref:b,class:"gutter-inner code-font"},[(n.openBlock(!0),n.createElementBlock(n.Fragment,null,n.renderList(Ye.value,c=>(n.openBlock(),n.createElementBlock("div",{key:c,class:"gutter-line"},n.toDisplayString(c),1))),128))],512)])):n.createCommentVNode("",!0),n.createElementVNode("div",{ref_key:"stage",ref:$,class:"stage"},[n.createElementVNode("div",{ref_key:"highlightLayer",ref:L,class:"highlight code-font"},[n.createElementVNode("pre",{ref_key:"highlightInner",ref:v,class:"code-font"},[n.createElementVNode("code",{innerHTML:Ue.value},null,8,Mt)],512)],512),n.createElementVNode("textarea",{ref_key:"textarea",ref:m,class:"code-font input",value:a.value,readonly:o.readonly,placeholder:o.placeholder,spellcheck:"false",autocomplete:"off",autocapitalize:"off",autocorrect:"off",wrap:"soft",onInput:st,onKeydown:mt,onScroll:dt,onClick:ut,onMousemove:it,onMouseleave:_,onBlur:ct},null,40,Rt),k.value?(n.openBlock(),n.createElementBlock("ul",{key:0,ref_key:"popup",ref:I,class:"popup",style:n.normalizeStyle(x.value),onMousedown:l[0]||(l[0]=n.withModifiers(()=>{},["prevent"]))},[(n.openBlock(!0),n.createElementBlock(n.Fragment,null,n.renderList(M.value,(c,h)=>(n.openBlock(),n.createElementBlock("li",{key:`${c.label}-${h}`,class:n.normalizeClass(["popup-item",{"is-active":h===E.value}]),onMouseenter:g=>E.value=h,onClick:g=>xe(c)},[n.createElementVNode("span",{class:n.normalizeClass(["badge",`badge-${c.kind}`])},n.toDisplayString(be(c.kind)),3),n.createElementVNode("span",Ht,n.toDisplayString(c.label),1),c.detail?(n.openBlock(),n.createElementBlock("span",Nt,n.toDisplayString(c.detail),1)):n.createCommentVNode("",!0)],42,_t))),128))],36)):n.createCommentVNode("",!0),C.value?(n.openBlock(),n.createElementBlock("div",{key:1,ref_key:"hoverCard",ref:H,class:"hover-card",style:n.normalizeStyle(pe.value)},[n.createElementVNode("div",At,[n.createElementVNode("span",{class:n.normalizeClass(["badge",`badge-${C.value.kind}`])},n.toDisplayString(be(C.value.kind)),3),n.createElementVNode("span",Lt,[n.createTextVNode(n.toDisplayString(C.value.label),1),C.value.detail?(n.openBlock(),n.createElementBlock("span",Vt,":")):n.createCommentVNode("",!0)]),C.value.detail?(n.openBlock(),n.createElementBlock("span",Ot,n.toDisplayString(C.value.detail),1)):n.createCommentVNode("",!0)]),C.value.documentation?(n.openBlock(),n.createElementBlock("p",zt,n.toDisplayString(C.value.documentation),1)):n.createCommentVNode("",!0),C.value.type?(n.openBlock(),n.createElementBlock("pre",Dt,[n.createElementVNode("code",{innerHTML:et(C.value.type)},null,8,jt)])):n.createCommentVNode("",!0)],4)):n.createCommentVNode("",!0)],512),o.collapsible?(n.openBlock(),n.createElementBlock("button",{key:1,type:"button",class:"expand-toggle",title:N.value?"收起":"展开","aria-label":N.value?"收起编辑器":"展开编辑器",onClick:Qe},[N.value?(n.openBlock(),n.createElementBlock("svg",Pt,[...l[2]||(l[2]=[n.createElementVNode("path",{d:"M11 2 5 8 11 14",fill:"none",stroke:"currentColor","stroke-width":"1.6","stroke-linecap":"round","stroke-linejoin":"round"},null,-1)])])):(n.openBlock(),n.createElementBlock("svg",Ft,[...l[1]||(l[1]=[n.createElementVNode("path",{d:"M6 2 12 8 6 14",fill:"none",stroke:"currentColor","stroke-width":"1.6","stroke-linecap":"round","stroke-linejoin":"round"},null,-1)])]))],8,Wt)):n.createCommentVNode("",!0)],38)],6))}}),Zt=(o,s)=>{const r=o.__vccOpts||o;for(const[t,i]of s)r[t]=i;return r},Ze=Zt(Kt,[["__scopeId","data-v-fcbf39fe"]]),qt={install(o){o.component("CodeInput",Ze)}};exports.CodeEditor=Ze;exports.collectCompletions=De;exports.default=qt;exports.defaultCompletions=Ve;exports.findTemplateRange=ee;exports.getKeywordSet=ue;exports.getKeywords=fe;exports.highlight=G;exports.highlightTemplate=Ke;exports.isInsideTemplate=St;exports.parseTsCompletions=Pe;exports.resolveHoverItem=je;exports.scanDeclarations=de;exports.tokenize=J;
|