@andares/pdeditor 0.4.1 → 0.5.0

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/dist/inline.d.ts CHANGED
@@ -1,25 +1,6 @@
1
1
  /**
2
- * 行内代码(`...`)与 `:-` 整行转义判定的轻量实现。
3
- * 语义与主包 @andares/promptdown 的 src/parser/lexer.ts 一致,但零依赖(组件自包含):
4
- * - 反引号配对(markdown 风格整体字串),不支持换行——未闭合按普通字符
5
- * - 行内代码段内冒号/`:-` 不参与键值/转义判定
6
- * - `:-` / `:-`(代码段外)使整行不是键值
2
+ * 行内代码(`...`)/ `:-` 整行转义 / 严格键值判定——**单一来源**:
3
+ * 直接复用共享语义包 @andares/pdfoundation 的 lexer 工具,组件不再保留副本
4
+ * (历史教训:两套判定漂移后,`-\t`、`//!pd` 的边界行为在主包/组件间不一致)。
7
5
  */
8
- export interface InlineSeg {
9
- code: boolean;
10
- seg: string;
11
- }
12
- /** 按反引号配对切分;未闭合段当普通字符(code:false) */
13
- export declare function splitInlineCode(text: string): InlineSeg[];
14
- /** 整行是否含 `:-` / `:-`(行内代码段外)→ 整行不是键值 */
15
- export declare function hasLiteralColon(s: string): boolean;
16
- /**
17
- * 代码段外第一个半角冒号的位置(-1 = 无)。
18
- * 供高亮层定位原始文本中的冒号位置(精确 slice,保证覆盖层对齐)。
19
- */
20
- export declare function findKeyValueSeparator(s: string): number;
21
- /** 严格键值判定(与 lexer 一致):键名不以空白结尾、冒号后跟空白或行尾、只认第一个半角冒号 */
22
- export declare function matchKeyValue(s: string): {
23
- key: string;
24
- value: string | undefined;
25
- } | null;
6
+ export { findKeyValueSeparator, hasLiteralColon, matchKeyValue, splitInlineCode, } from "@andares/pdfoundation";
@@ -1,4 +1,4 @@
1
1
  /**
2
- * pd 文本 → HTML(Yace highlighter)。围栏状态跟踪:围栏内行原样(仅转义)。
2
+ * pd 文本 → HTML。围栏状态跟踪:围栏内行原样(仅转义)。
3
3
  */
4
4
  export declare function highlightPd(value: string): string;
package/dist/pd.cjs CHANGED
@@ -1,14 +1,14 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const z=require("@andares/pdfoundation");function G(s,e,{signal:n,edges:t}={}){let i,o=null;const a=t!=null&&t.includes("leading"),c=t==null||t.includes("trailing"),l=()=>{o!==null&&(s.apply(i,o),i=void 0,o=null)},f=()=>{c&&l(),x()};let h=null;const y=()=>{h!=null&&clearTimeout(h),h=setTimeout(()=>{h=null,f()},e)},E=()=>{h!==null&&(clearTimeout(h),h=null)},x=()=>{E(),i=void 0,o=null},u=()=>{l()},p=function(...g){if(n!=null&&n.aborted)return;i=this,o=g;const S=h==null;y(),a&&S&&l()};return p.schedule=y,p.cancel=x,p.flush=u,n==null||n.addEventListener("abort",x,{once:!0}),p}const A={position:"relative",boxSizing:"border-box",overflow:"hidden",fontSize:"16px",fontFamily:'Menlo, Consolas, "Liberation Mono", Monaco, "Lucida Console", monospace'},Q={lineHeight:"inherit",whiteSpace:"pre-wrap",wordBreak:"keep-all",overflowWrap:"break-word",background:"none",position:"absolute",width:"100%",height:"100%",zIndex:"1",resize:"none",caretColor:"currentColor",padding:"inherit",outline:"none",fontSize:"inherit",fontFamily:"inherit",letterSpacing:"inherit",boxSizing:"border-box",border:"none",top:"0px",left:"0px",color:"inherit",overflow:"hidden","-webkit-font-smoothing":"antialiased","-webkit-text-fill-color":"transparent"},X={lineHeight:"inherit",position:"relative",whiteSpace:"pre-wrap",wordBreak:"keep-all",padding:"0",width:"100%",margin:"0",pointerEvents:"none",boxSizing:"border-box",overflowWrap:"break-word",fontFamily:"inherit"},Y={lineHeight:"inherit",position:"absolute",width:"100%",height:"100%",whiteSpace:"pre-wrap",wordBreak:"keep-all",padding:"inherit",margin:"0",top:"0px",left:"0px",pointerEvents:"none",boxSizing:"border-box",overflowWrap:"break-word",fontFamily:"inherit"},B=s=>J(s);function D(s){const e=Array.isArray(s)?s:[s];return e.length===0?B:n=>e.reduce((t,i,o)=>i(t,{html:o>0}),n)}class Z{constructor(e,n={}){if(this.lines=null,!e)throw new Error("selector is not defined");if(this.root=e instanceof Node?e:document.querySelector(e),!this.root)throw new Error(`element with "${e}" selector is not exist`);const t={value:"",styles:{},plugins:[],highlighter:B},{highlighters:i,...o}=n;this.options={...t,...o,highlighter:i?D(i):t.highlighter},this.options.value=this.options.value==null?"":String(this.options.value),this.init()}init(){this.textarea=document.createElement("textarea"),this.textarea.setAttribute("spellcheck","false"),this.textarea.setAttribute("autocapitalize","off"),this.textarea.setAttribute("autocorrect","off"),this.textarea.setAttribute("autocomplete","off"),this.pre=document.createElement("pre");const e=Object.keys(A).concat(Object.keys(this.options.styles)).concat(["paddingLeft"]);this.initialRootStyles={};const n=this.root.style;e.forEach(t=>{this.initialRootStyles[t]=n[t]||""}),Object.assign(this.root.style,A,this.options.styles),Object.assign(this.textarea.style,Q),Object.assign(this.pre.style,X),this.basePaddingLeft=this.root.style.paddingLeft||"",this.root.appendChild(this.textarea),this.root.appendChild(this.pre),this.addTextareaEvents(),this.update({value:this.options.value}),this.updateLines()}addTextareaEvents(){this.handleEvent=e=>{if(e.type==="keydown"&&this.textarea.readOnly)return;const n=e;if(n.isComposing||n.keyCode===229)return;const t=(function(i,o){const{value:a,selectionStart:c,selectionEnd:l}=o.target;return i.reduce((f,h)=>({...f,...h(f,o)}),{value:a,selectionStart:c,selectionEnd:l})})(this.options.plugins,e);this.update(t)},this.textarea.addEventListener("input",this.handleEvent),this.textarea.addEventListener("keydown",this.handleEvent),this.textarea.addEventListener("compositionend",this.handleEvent)}update(e){if(!this.textarea)return;let{value:n,selectionStart:t,selectionEnd:i}=e;n!=null&&(n=String(n),t==null&&(t=this.textarea.selectionStart),i==null&&(i=this.textarea.selectionEnd),this.textarea.value!==n&&(this.textarea.value=n)),t!=null&&(this.textarea.selectionStart=t),i!=null&&(this.textarea.selectionEnd=i),n!==this.value&&n!=null&&(this.value=n,this.render(),this.updateCallback&&this.updateCallback(n))}updateOptions(e){if(!this.textarea)return;const{highlighters:n,...t}=e;if(this.options={...this.options,...t,highlighter:n?D(n):this.options.highlighter},e.styles){const i=this.root.style;Object.keys(e.styles).forEach(o=>{o in this.initialRootStyles||(this.initialRootStyles[o]=i[o]||"")}),Object.assign(this.root.style,e.styles),("paddingLeft"in e.styles||"padding"in e.styles)&&(this.basePaddingLeft=this.root.style.paddingLeft||"")}this.lines&&!this.options.lineNumbers&&(T(this.lines),this.lines=null,this.root.style.paddingLeft=this.basePaddingLeft),e.value!=null&&this.update({value:e.value}),this.render()}render(){const e=this.options.highlighter(this.value);this.pre.innerHTML=e+"<br/>",this.updateLines()}updateLines(){if(!this.options.lineNumbers)return;this.lines||(this.lines=document.createElement("pre"),this.root.appendChild(this.lines),Object.assign(this.lines.style,Y));const e=this.value.split(`
2
- `),n=e.length.toString().length;this.root.style.paddingLeft=`${n+1}ch`,this.lines.innerHTML=e.map((t,i)=>`${`<span class="yace-line" style="position: absolute; opacity: .3; left: 0; width: ${n}ch; text-align: right">${1+i}</span>`}${`<span style="color: transparent; pointer-events: none">${J(t)}</span>`}`).join(`
3
- `)}destroy(){this.textarea&&(this.textarea.removeEventListener("input",this.handleEvent),this.textarea.removeEventListener("keydown",this.handleEvent),this.textarea.removeEventListener("compositionend",this.handleEvent),T(this.textarea),T(this.pre),T(this.lines),Object.assign(this.root.style,this.initialRootStyles),Object.assign(this,{textarea:null,pre:null,lines:null,updateCallback:null,handleEvent:null,initialRootStyles:null,options:null,root:null}))}onUpdate(e){this.updateCallback=e}}function T(s){s&&s.parentNode&&s.parentNode.removeChild(s)}function J(s){return s.replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;").replace(/"/g,"&quot;").replace(/'/g,"&#039;")}function I(s,e){const n=s,t=n.key.toLowerCase(),i=e.split("+"),o=i.slice(0,-1),a=i[i.length-1];return!(t!==a||o.includes("shift")!==n.shiftKey||o.includes("ctrl")!==n.ctrlKey||o.includes("alt")!==n.altKey||o.includes("meta")!==n.metaKey)}function F(s,e){var i;const t=s.slice(0,e).split(`
4
- `);return{line:t.length-1,col:((i=t[t.length-1])==null?void 0:i.length)??0}}function O(s,e,n){var i;let t=0;for(let o=0;o<e;o++)t+=(((i=s[o])==null?void 0:i.length)??0)+1;return t+n}function U(){return(s,e)=>{var f;if(e.type!=="keydown"||!I(e,"enter"))return;const{value:n,selectionStart:t,selectionEnd:i}=s,o=n.slice(0,t).split(`
5
- `).pop()??"",a=((f=/^\s*/.exec(o))==null?void 0:f[0])??"";e.preventDefault();const c=/^(\s*)-(?=\s|$)/.test(o),l=`
6
- ${a}${c?"- ":""}`;return{value:n.slice(0,t)+l+n.slice(i),selectionStart:t+l.length,selectionEnd:t+l.length}}}function ee(s){return/^(\s*)-(?=\s|$)/.test(s)}function N(s,e){return s.startsWith(e)?e.length:s.startsWith(" ")?1:0}function te(s){return(e,n)=>{if(n.type!=="keydown")return;const{value:t,selectionStart:i,selectionEnd:o}=e,a=t.split(`
7
- `),c=t.slice(0,i).split(`
8
- `).length-1,l=t.slice(0,o).split(`
9
- `).length-1,f=F(t,i),h=F(t,o);if(I(n,"shift+tab")){n.preventDefault();const y=a.map((g,S)=>S>=c&&S<=l?g.slice(N(g,s)):g),E=y.join(`
10
- `);if(E===t)return;const x=g=>{const S=N(a[g.line]??"",s);return{line:g.line,col:Math.max(0,g.col-S)}},u=x(f),p=x(h);return{value:E,selectionStart:O(y,u.line,u.col),selectionEnd:O(y,p.line,p.col)}}if(I(n,"tab")){n.preventDefault();const y=a.map((u,p)=>({l:u,i:p})).filter(({i:u})=>u>=c&&u<=l);if(y.length>0&&y.every(({l:u})=>ee(u))||i!==o){const u=a.map((b,$)=>$>=c&&$<=l&&b!==""?s+b:b),p=u.join(`
11
- `),g=b=>{const $=a[b.line]??"",j=b.line>=c&&b.line<=l;return{line:b.line,col:j&&$!==""?b.col+s.length:b.col}},S=g(f),k=g(h);return{value:p,selectionStart:O(u,S.line,S.col),selectionEnd:O(u,k.line,k.col)}}const x=i+s.length;return{value:t.slice(0,i)+s+t.slice(o),selectionStart:x,selectionEnd:x}}}}function ne(s){return s.replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;")}function se(s,e){const n=Math.min(s.length,e.length);let t=0;for(;t<n&&s.charCodeAt(t)===e.charCodeAt(t);)t++;return t}function ie(s,e,n){let t=e.language??"pd";const i=e.onValueChange;let o=!1;const a=e.indentUnit??" ",c=new Z(s,{value:e.value??"",lineNumbers:e.lineNumbers??!1,styles:e.styles??{},highlighters:[e.highlight?r=>{var d;return((d=e.highlight)==null?void 0:d.call(e,r,t))??""}:n(t)],plugins:[U(),te(a)]});c.onUpdate(r=>{o||(b(r),i==null||i(r))});const l=c.textarea;let f=!1;const h=()=>{f=!0},y=()=>{f=!1},E=()=>{f&&(o=!0,c.update({value:l.value,selectionStart:l.selectionStart,selectionEnd:l.selectionEnd}),o=!1)};l.addEventListener("compositionstart",h),l.addEventListener("compositionend",y),l.addEventListener("input",E);const x=5,u=[{value:c.value,selStart:-1,selEnd:-1}],p=[],g=()=>{u.length>x+1&&u.splice(1,1)},k=G(r=>{const d=u[0];if(d!==void 0&&d.selStart<0&&d.value!==r.value){const P=se(d.value,r.value);d.selStart=P,d.selEnd=P}const v=u.at(-1);v!==void 0&&v.value===r.value||(p.length=0,u.push(r),g())},1e3),b=r=>{const d=l.selectionStart,v=l.selectionEnd;k({value:r,selStart:d,selEnd:v})},$=r=>{const d=r.selStart>=0?r.selStart:r.value.length,v=r.selEnd>=0?r.selEnd:r.value.length;o=!0,c.update({value:r.value,selectionStart:d,selectionEnd:v}),l.value===r.value&&l.setSelectionRange(d,v),o=!1},j=r=>{const d=r.ctrlKey||r.metaKey,v=r.key.toLowerCase();if(!d||v!=="z"&&v!=="y")return;if(v==="y"||v==="z"&&r.shiftKey){if(k.flush(),p.length===0)return;r.preventDefault();const C=p.pop();C!==void 0&&(u.push(C),g(),$(C))}else{if(k.flush(),u.length<=1)return;r.preventDefault();const C=u.pop();C!==void 0&&p.push(C);const M=u.at(-1);M!==void 0&&$(M)}};return l.addEventListener("keydown",j),{setValue(r){o=!0,c.update({value:r}),o=!1},getValue(){return c.value},setLanguage(r){t=r,c.updateOptions({highlighters:[e.highlight?d=>{var v;return((v=e.highlight)==null?void 0:v.call(e,d,t))??""}:n(t)]})},destroy(){k.cancel(),l.removeEventListener("compositionstart",h),l.removeEventListener("compositionend",y),l.removeEventListener("input",E),l.removeEventListener("keydown",j),c.destroy()},textarea:l}}function H(s){const e=[];let n="",t=!1;for(const i of s)i==="`"?t?(e.push({code:!0,seg:n+"`"}),n="",t=!1):(n&&e.push({code:!1,seg:n}),n="`",t=!0):n+=i;return n&&e.push({code:!1,seg:n}),e}function K(s){for(const{code:e,seg:n}of H(s))if(!e&&(n.includes(":-")||n.includes(":-")))return!0;return!1}function R(s){let e=0;for(const{code:n,seg:t}of H(s)){if(!n){const i=t.indexOf(":");if(i!==-1)return e+i}e+=t.length}return-1}function W(s){if(K(s))return null;const e=R(s);if(e<=0)return null;const n=s.slice(0,e),t=s.slice(e+1);if(/\s$/.test(n)||t!==""&&!/^\s/.test(t))return null;const i=t.trim();return{key:n.trim(),value:i===""?void 0:i}}function m(s){return s.replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;").replace(/"/g,"&quot;")}const L={key:"pd-key",keyPunct:"pd-key-punct",item:"pd-item",section:"pd-section",sep:"pd-sep",ref:"pd-ref",inlineCode:"pd-inline-code",fence:"pd-fence"};function oe(s){return`<span class="${L.inlineCode}">${m(s)}</span>`}const q=/(?:^|\s):([^\s-][^\s]*)(?=\s|$)/g;function le(s){let e="",n=0;q.lastIndex=0;let t;for(;(t=q.exec(s))!==null;){const i=/^\s/.test(t[0])?1:0,o=t.index+i;e+=m(s.slice(n,o)),e+=`<span class="${L.ref}">${m(s.slice(o,t.index+t[0].length))}</span>`,n=t.index+t[0].length}return e+=m(s.slice(n)),e}function w(s){let e="";for(const{code:n,seg:t}of H(s))e+=n?oe(t):le(t);return e}function V(s,e,n,t){const i=`<span class="${L.key}">${w(n)}</span>`,o=`<span class="${L.keyPunct}">:</span>`;return`${s}${e}${i}${o}${w(t)}`}function re(s){const e=s.trim();if(/^\/\/!pd(?:\s+[^\s]+)?$/.test(e)){const t=/^(\s*)(\/\/!pd)(\s*.*)$/.exec(s),i=(t==null?void 0:t[1])??"",o=(t==null?void 0:t[2])??"//!pd",a=(t==null?void 0:t[3])??"";return`${m(i)}<span class="${L.section}">${m(o)}</span>${w(a)}`}if(e==="---")return`<span class="${L.sep}">${m(s)}</span>`;const n=/^(\s*)(-)(?=\s|$)/.exec(s);if(n){const t=m(n[1]??""),i=(n[1]??"").length+1,o=s.slice(i),a=o.trimStart(),c=o.slice(0,o.length-a.length),l=`<span class="${L.item}">-</span>`;if(!K(a)&&W(a)){const h=R(a),y=a.slice(0,h),E=a.slice(h+1);return`${t}${l}${m(c)}${V("","",y,E)}`}return`${t}${l}${m(c)}${w(a)}`}if(!K(e)&&W(e)){const i=s.length-s.trimStart().length,o=s.slice(i),a=R(o),c=o.slice(0,a).trim(),l=o.slice(a+1);return V(m(s.slice(0,i)),"",c,l)}return w(s)}function _(s){const e=s.split(`
12
- `);let n=!1;const t=[];for(const i of e){const o=i.trimStart();if(n){o.startsWith("```")?(n=!1,t.push(`<span class="${L.fence}">${m(i)}</span>`)):t.push(m(i));continue}if(o.startsWith("```")){n=!0,t.push(`<span class="${L.fence}">${m(i)}</span>`);continue}t.push(re(i))}return t.join(`
13
- `)}function ae(s){return e=>s==="pd"?_(e):ne(e)}function ce(s,e={}){return ie(s,e,ae)}Object.defineProperty(exports,"format",{enumerable:!0,get:()=>z.format});Object.defineProperty(exports,"jsonToPdText",{enumerable:!0,get:()=>z.jsonToPdText});Object.defineProperty(exports,"pdToJsonText",{enumerable:!0,get:()=>z.pdToJsonText});exports.createPdEditor=ce;exports.highlightPd=_;
14
- //# sourceMappingURL=pd.cjs.map
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const z=require("@andares/pdfoundation");function Me(i,o,{signal:s,edges:e}={}){let l,r=null;const u=e!=null&&e.includes("leading"),p=e==null||e.includes("trailing"),b=()=>{r!==null&&(i.apply(l,r),l=void 0,r=null)},w=()=>{p&&b(),$()};let v=null;const S=()=>{v!=null&&clearTimeout(v),v=setTimeout(()=>{v=null,w()},o)},E=()=>{v!==null&&(clearTimeout(v),v=null)},$=()=>{E(),l=void 0,r=null},y=()=>{b()},L=function(...d){if(s!=null&&s.aborted)return;l=this,r=d;const h=v==null;S(),u&&h&&b()};return L.schedule=S,L.cancel=$,L.flush=y,s==null||s.addEventListener("abort",$,{once:!0}),L}function Q(i,o){const s=i,e=s.key.toLowerCase(),l=o.split("+"),r=l.slice(0,-1),u=l[l.length-1];return!(e!==u||r.includes("shift")!==s.shiftKey||r.includes("ctrl")!==s.ctrlKey||r.includes("alt")!==s.altKey||r.includes("meta")!==s.metaKey)}function be(i,o){var l;const e=i.slice(0,o).split(`
2
+ `);return{line:e.length-1,col:((l=e[e.length-1])==null?void 0:l.length)??0}}function I(i,o,s){var l;let e=0;for(let r=0;r<o;r++)e+=(((l=i[r])==null?void 0:l.length)??0)+1;return e+s}function ve(){return(i,o)=>{var w;if(o.type!=="keydown"||!Q(o,"enter"))return;const{value:s,selectionStart:e,selectionEnd:l}=i,r=s.slice(0,e).split(`
3
+ `).pop()??"",u=((w=/^\s*/.exec(r))==null?void 0:w[0])??"";o.preventDefault();const p=/^(\s*)-(?=[ ]|$)/.test(r),b=`
4
+ ${u}${p?"- ":""}`;return{value:s.slice(0,e)+b+s.slice(l),selectionStart:e+b.length,selectionEnd:e+b.length}}}function Ne(i){return/^(\s*)-(?=[ ]|$)/.test(i)}function ye(i,o){return i.startsWith(o)?o.length:i.startsWith(" ")?1:0}function Le(i){return(o,s)=>{if(s.type!=="keydown")return;const{value:e,selectionStart:l,selectionEnd:r}=o,u=e.split(`
5
+ `),p=e.slice(0,l).split(`
6
+ `).length-1,b=e.slice(0,r).split(`
7
+ `).length-1,w=be(e,l),v=be(e,r);if(Q(s,"shift+tab")){s.preventDefault();const S=u.map((d,h)=>h>=p&&h<=b?d.slice(ye(d,i)):d),E=S.join(`
8
+ `);if(E===e)return;const $=d=>{const h=ye(u[d.line]??"",i);return{line:d.line,col:Math.max(0,d.col-h)}},y=$(w),L=$(v);return{value:E,selectionStart:I(S,y.line,y.col),selectionEnd:I(S,L.line,L.col)}}if(Q(s,"tab")){s.preventDefault();const S=u.map((y,L)=>({l:y,i:L})).filter(({i:y})=>y>=p&&y<=b);if(S.length>0&&S.every(({l:y})=>Ne(y))||l!==r){const y=u.map((x,g)=>g>=p&&g<=b&&x!==""?i+x:x),L=y.join(`
9
+ `),d=x=>{const g=u[x.line]??"",U=x.line>=p&&x.line<=b;return{line:x.line,col:U&&g!==""?x.col+i.length:x.col}},h=d(w),n=d(v);return{value:L,selectionStart:I(y,h.line,h.col),selectionEnd:I(y,n.line,n.col)}}const $=l+i.length;return{value:e.slice(0,l)+i+e.slice(r),selectionStart:$,selectionEnd:$}}}}function Te(i){return i.replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;").replace(/"/g,"&quot;").replace(/'/g,"&#039;")}function O(i){return i.replace(/\r\n?/g,`
10
+ `)}function De(i,o){const s=document.createRange();s.selectNodeContents(i),i.replaceChildren(s.createContextualFragment(o))}function Oe(i,o){const s=Math.min(i.length,o.length);let e=0;for(;e<s&&i.charCodeAt(e)===o.charCodeAt(e);)e++;return e}const Fe=new Set(["padding","paddingTop","paddingRight","paddingBottom","paddingLeft"]);function xe(i){var e;const o={top:"0px",right:"0px",bottom:"0px",left:"0px"},s=(e=i.padding)==null?void 0:e.trim();if(s){const l=s.split(/\s+/);if(l.length===1){const r=l[0]??"0px";o.top=o.right=o.bottom=o.left=r}else l.length===2?(o.top=o.bottom=l[0]??"0px",o.right=o.left=l[1]??"0px"):l.length===3?(o.top=l[0]??"0px",o.right=o.left=l[1]??"0px",o.bottom=l[2]??"0px"):(o.top=l[0]??"0px",o.right=l[1]??"0px",o.bottom=l[2]??"0px",o.left=l[3]??"0px")}return i.paddingTop&&(o.top=i.paddingTop),i.paddingRight&&(o.right=i.paddingRight),i.paddingBottom&&(o.bottom=i.paddingBottom),i.paddingLeft&&(o.left=i.paddingLeft),o}function Ke(i){return i.startsWith("--")?i:i.replace(/[A-Z]/g,o=>`-${o.toLowerCase()}`)}const Se="position:relative;display:block;box-sizing:border-box;width:100%;height:auto;max-height:inherit;overflow:hidden;text-align:start;margin:0;border:0;font-family:var(--pd-font-family,inherit);font-size:var(--pd-font-size,inherit);line-height:var(--pd-line-height,1.5);letter-spacing:var(--pd-letter-spacing,inherit);color:var(--pd-color,inherit);background:var(--pd-background,transparent);tab-size:4;font-variant-ligatures:none;font-kerning:normal;",Ee="position:absolute;left:0;top:0;width:100%;height:100%;box-sizing:border-box;margin:0;border:0;outline:none;resize:none;background:transparent;-webkit-text-fill-color:transparent;caret-color:var(--pd-caret-color,currentColor);overflow:auto;scrollbar-gutter:stable;white-space:pre-wrap;overflow-wrap:break-word;word-break:normal;font:inherit;line-height:inherit;letter-spacing:inherit;tab-size:inherit;z-index:2;",$e="position:relative;left:auto;top:auto;width:100%;height:auto;box-sizing:border-box;margin:0;border:0;overflow:hidden;scrollbar-gutter:stable;pointer-events:none;user-select:none;white-space:pre-wrap;overflow-wrap:break-word;word-break:normal;font:inherit;line-height:inherit;letter-spacing:inherit;tab-size:inherit;color:inherit;z-index:1;",je="position:absolute;left:0;top:0;width:100%;height:100%;box-sizing:border-box;margin:0;border:0;overflow:hidden;scrollbar-gutter:stable;pointer-events:none;user-select:none;white-space:pre-wrap;overflow-wrap:break-word;word-break:normal;font:inherit;line-height:inherit;letter-spacing:inherit;tab-size:inherit;color:inherit;z-index:0;",ke="position:absolute;left:0;top:0;width:100%;height:100%;box-sizing:border-box;margin:0;border:0;overflow:hidden;scrollbar-gutter:stable;pointer-events:none;user-select:none;white-space:pre-wrap;overflow-wrap:break-word;word-break:normal;font:inherit;line-height:inherit;letter-spacing:inherit;tab-size:inherit;color:inherit;opacity:var(--pd-placeholder-opacity,0.5);z-index:1;";function V(i,o,s,e,l={}){const r=document.createElement(i);r.className=o,r.setAttribute("part",s),r.style.cssText=e;for(const[u,p]of Object.entries(l))r.setAttribute(u,p);return r}function Be(i,o,s){let e={...o},l=e.language??"pd",r=O(e.value??""),u=r,p=!1,b=!1,w=!1,v=e.lineNumbers??!1,S=G(),E="",$=!1,y=0,L=null;const d=V("div","pd-root","root",Se),h=V("pre","pd-highlight","highlight",$e,{"aria-hidden":"true"}),n=V("textarea","pd-input","input",Ee),x=V("div","pd-placeholder","placeholder",ke,{"aria-hidden":"true"});let g=null;n.spellcheck=e.spellcheck??!1,n.setAttribute("autocapitalize","off"),n.setAttribute("autocorrect","off"),n.setAttribute("autocomplete","off"),n.setAttribute("wrap","soft"),n.value=r,n.readOnly=e.readOnly??!1,n.disabled=e.disabled??!1,(t=>{t!==void 0&&t>=0?n.maxLength=t:n.removeAttribute("maxlength")})(e.maxLength),e.dir&&(n.dir=e.dir),B(),d.appendChild(h),d.appendChild(x),d.appendChild(n),i.appendChild(d),j(),J(),A(),q();function M(){p||(h.scrollTop=n.scrollTop,h.scrollLeft=n.scrollLeft,g&&(g.scrollTop=n.scrollTop,g.scrollLeft=n.scrollLeft))}function ze(){if(typeof getComputedStyle!="function")return 0;const t=getComputedStyle(i),a=Number.parseFloat(t.paddingTop)||0,c=Number.parseFloat(t.paddingBottom)||0;return Math.max(0,i.clientHeight-a-c)}function _(){$?(h.style.position="absolute",h.style.left="0",h.style.top="0",h.style.height="100%"):(h.style.position="relative",h.style.left="auto",h.style.top="auto",h.style.height="auto")}function q(){if(p)return;const t=ze(),a=h.scrollHeight;if(!$){t>0&&a>t+1&&($=!0,_(),d.style.height=`${t}px`,M());return}if(t<=0||a<=t+1){$=!1,_(),d.style.height="",n.scrollTop=0;return}const c=Number.parseFloat(d.style.height)||0;Math.abs(c-t)>.5&&(d.style.height=`${t}px`),M()}function ee(){if(!y){if(typeof requestAnimationFrame!="function"){q();return}y=requestAnimationFrame(()=>{y=0,q()})}}typeof ResizeObserver<"u"&&(L=new ResizeObserver(()=>ee()),L.observe(i),L.observe(d));const te=()=>M(),Pe=(t,a,c)=>{var f;r=t,u=t,W({value:t,selStart:a,selEnd:c}),(f=e.onValueChange)==null||f.call(e,t)},ne=t=>{if(p)return;const a=O(n.value);if(b||t!=null&&t.isComposing){r=a,A();return}a!==r&&(r=a,A()),a!==u&&Pe(a,n.selectionStart,n.selectionEnd)},ie=()=>{b=!0},re=()=>{b=!1;const t=O(n.value);t!==r&&(r=t,A())},Re=Math.max(0,e.historyLimit??5),oe=Math.max(0,e.historyDebounce??1e3),C=[{value:r,selStart:-1,selEnd:-1}],D=[],le=()=>{C.length>Re+1&&C.splice(1,1)},se=t=>{const a=C[0];if(a!==void 0&&a.selStart<0&&a.value!==t.value){const f=Oe(a.value,t.value);a.selStart=f,a.selEnd=f}const c=C.at(-1);c!==void 0&&c.value===t.value||(D.length=0,C.push(t),le())},F=Me(se,oe),W=t=>{oe===0?se(t):F(t)},ae=t=>{var f;const a=t.selStart>=0?t.selStart:Math.min(t.value.length,n.selectionStart),c=t.selEnd>=0?t.selEnd:a;r=t.value,u=t.value,n.value=t.value,n.setSelectionRange(Math.max(0,Math.min(a,t.value.length)),Math.max(0,Math.min(c,t.value.length))),A(),M(),(f=e.onValueChange)==null||f.call(e,t.value)},ce=()=>{if(F.flush(),C.length<=1)return!1;const t=C.pop();t!==void 0&&D.push(t);const a=C.at(-1);return a!==void 0&&ae(a),!0},ue=()=>{if(F.flush(),D.length===0)return!1;const t=D.pop();return t===void 0?!1:(C.push(t),le(),ae(t),!0)},de=t=>{var T;if(p||t.isComposing||b||n.readOnly||n.disabled)return;const a=t.ctrlKey||t.metaKey,c=t.key.toLowerCase();if(a&&(c==="z"||c==="y")){t.preventDefault(),c==="y"||c==="z"&&t.shiftKey?ue():ce();return}if(c==="escape"){(e.tabBehavior??"indent")==="indent"&&(w=!0);return}if(c==="tab"&&(e.tabBehavior??"indent")==="focus")return;if(c==="tab"&&w){w=!1;return}const f={value:r,selectionStart:n.selectionStart,selectionEnd:n.selectionEnd};let m=f;for(const N of Y){const P=N(m,t);P&&(m={...m,...P})}(m.value!==f.value||m.selectionStart!==f.selectionStart||m.selectionEnd!==f.selectionEnd)&&(t.preventDefault(),r=m.value,n.value=m.value,n.setSelectionRange(m.selectionStart,m.selectionEnd),u=m.value,A(),W({value:m.value,selStart:m.selectionStart,selEnd:m.selectionEnd}),(T=e.onValueChange)==null||T.call(e,m.value),M())},fe=()=>d.classList.add("pd-focused"),pe=()=>d.classList.remove("pd-focused");n.addEventListener("input",ne),n.addEventListener("compositionstart",ie),n.addEventListener("compositionend",re),n.addEventListener("scroll",te,{passive:!0}),n.addEventListener("keydown",de),n.addEventListener("focus",fe),n.addEventListener("blur",pe);function G(){return e.highlight?t=>{var a;return((a=e.highlight)==null?void 0:a.call(e,t,l))??""}:s(l)}function A(){if(p)return;const t=e.highlightThreshold??1e5,a=r.length>t?Te(r):S(r);De(h,`${a}<br/>`),v&&he(),J(),L||ee()}function he(){if(!v)return;g||(g=document.createElement("pre"),g.className="pd-lines",g.setAttribute("part","gutter"),g.setAttribute("aria-hidden","true"),d.insertBefore(g,x));const t=r.split(`
11
+ `),c=`${String(t.length).length+1}ch`,f=xe(e.styles??{}),m=`position:absolute;inset-inline-start:${f.left};width:${c};text-align:end;opacity:var(--pd-gutter-opacity,0.35);color:var(--pd-gutter-color,inherit)`,T=document.createDocumentFragment();t.forEach((N,P)=>{const H=document.createElement("span");H.className="pd-ln",H.style.cssText=m,H.textContent=String(P+1);const Z=document.createElement("span");Z.style.color="transparent",Z.textContent=N,T.append(H,Z),P<t.length-1&&T.append(document.createTextNode(`
12
+ `))}),T.append(document.createElement("br")),g.replaceChildren(T),c!==E&&(E=c,j(),g.style.cssText=me(f))}function J(){const t=e.placeholder??"";x.textContent=t,x.style.display=t&&r.length===0?"block":"none",t?n.setAttribute("aria-placeholder",t):n.removeAttribute("aria-placeholder")}function ge(t){const a=E?`calc(${t.left} + ${E})`:t.left;return`padding:${t.top} ${t.right} ${t.bottom} ${a};`}function X(t,a){return a+ge(t)}function me(t){return je+ge(t)}function j(){const t=e.styles??{},a=xe(t),c=Object.entries(t).filter(([f])=>!Fe.has(f)).map(([f,m])=>`${Ke(f)}:${m};`).join("");d.style.cssText=Se+c,h.style.cssText=X(a,$e),n.style.cssText=X(a,Ee),x.style.cssText=X(a,ke),g&&(g.style.cssText=me(a)),_(),e.dir&&(d.dir=e.dir,n.dir=e.dir,h.dir=e.dir,x.dir=e.dir,g&&(g.dir=e.dir))}function B(){e.ariaLabel?n.setAttribute("aria-label",e.ariaLabel):n.removeAttribute("aria-label"),e.ariaLabelledby?n.setAttribute("aria-labelledby",e.ariaLabelledby):n.removeAttribute("aria-labelledby"),e.ariaDescribedby?n.setAttribute("aria-describedby",e.ariaDescribedby):n.removeAttribute("aria-describedby"),e.readOnly?n.setAttribute("aria-readonly","true"):n.removeAttribute("aria-readonly"),e.disabled?n.setAttribute("aria-disabled","true"):n.removeAttribute("aria-disabled")}v&&he();const Y=[ve(),Le(e.indentUnit??" ")];return{setValue(t,a){if(p||b)return;let c=O(t);e.maxLength!==void 0&&e.maxLength>=0&&(c=c.slice(0,e.maxLength)),F.cancel(),r=c,u=c,n.value=c,n.setSelectionRange(c.length,c.length),((a==null?void 0:a.resetHistory)??!0)&&(C.length=0,C.push({value:c,selStart:-1,selEnd:-1}),D.length=0),A(),M()},getValue(){return r},setLanguage(t){l=t,e={...e,language:t},S=G(),A()},setPlaceholder(t){e={...e,placeholder:t},J()},setReadOnly(t){e={...e,readOnly:t},n.readOnly=t,B()},setDisabled(t){e={...e,disabled:t},n.disabled=t,B()},setLineNumbers(t){v!==t&&(v=t,!t&&g&&(g.remove(),g=null,E=""),j(),A())},update(t){p||(e={...e,...t},t.indentUnit!==void 0&&Y.splice(0,Y.length,ve(),Le(t.indentUnit)),t.language!==void 0&&(l=t.language),("highlight"in t||t.language!==void 0)&&(S=G()),t.lineNumbers!==void 0&&(v=t.lineNumbers,!v&&g&&(g.remove(),g=null,E="")),t.value!==void 0&&(r=O(t.value),u=r,n.value=r,C.length=0,C.push({value:r,selStart:-1,selEnd:-1}),D.length=0),t.readOnly!==void 0&&(n.readOnly=t.readOnly),t.disabled!==void 0&&(n.disabled=t.disabled),t.maxLength!==void 0&&(t.maxLength>=0?n.maxLength=t.maxLength:n.removeAttribute("maxlength")),t.spellcheck!==void 0&&(n.spellcheck=t.spellcheck),t.dir!==void 0&&(n.dir=t.dir),B(),j(),A(),M())},focus(){n.focus()},blur(){n.blur()},undo(){ce()},redo(){ue()},insertText(t){var N;if(p||n.readOnly||n.disabled)return;const a=n.selectionStart,c=n.selectionEnd;let f=O(t);if(e.maxLength!==void 0&&e.maxLength>=0){const P=e.maxLength-(r.length-(c-a));if(P<=0)return;f.length>P&&(f=f.slice(0,P))}const m=r.slice(0,a)+f+r.slice(c);r=m,u=m,n.value=m;const T=a+f.length;n.setSelectionRange(T,T),A(),W({value:m,selStart:T,selEnd:T}),(N=e.onValueChange)==null||N.call(e,m),M()},getSelection(){return{start:n.selectionStart,end:n.selectionEnd}},setSelection(t,a=t){const c=f=>Math.max(0,Math.min(r.length,Number.isFinite(f)?f:0));n.setSelectionRange(c(t),c(a))},destroy(){p||(p=!0,F.cancel(),L==null||L.disconnect(),y&&typeof cancelAnimationFrame=="function"&&cancelAnimationFrame(y),n.removeEventListener("input",ne),n.removeEventListener("compositionstart",ie),n.removeEventListener("compositionend",re),n.removeEventListener("scroll",te),n.removeEventListener("keydown",de),n.removeEventListener("focus",fe),n.removeEventListener("blur",pe),d.remove())},textarea:n,root:d,pre:h}}function k(i){return i.replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;").replace(/"/g,"&quot;")}const R={key:"pd-key",keyPunct:"pd-key-punct",item:"pd-item",section:"pd-section",sep:"pd-sep",ref:"pd-ref",inlineCode:"pd-inline-code",fence:"pd-fence"};function He(i){return`<span class="${R.inlineCode}">${k(i)}</span>`}const Ce=/(?:^|\s):([^\s-][^\s]*)(?=\s|$)/g;function Ie(i){let o="",s=0;Ce.lastIndex=0;let e;for(;(e=Ce.exec(i))!==null;){const l=/^\s/.test(e[0])?1:0,r=e.index+l;o+=k(i.slice(s,r)),o+=`<span class="${R.ref}">${k(i.slice(r,e.index+e[0].length))}</span>`,s=e.index+e[0].length}return o+=k(i.slice(s)),o}function K(i){let o="";for(const{code:s,seg:e}of z.splitInlineCode(i))o+=s?He(e):Ie(e);return o}function we(i,o,s,e){const l=`<span class="${R.key}">${K(s)}</span>`,r=`<span class="${R.keyPunct}">:</span>`;return`${i}${o}${l}${r}${K(e)}`}function Ve(i){const o=i.trim();if(/^\/\/!pd(?:\s+[^\s]+)?$/.test(o)){const e=/^(\s*)(\/\/!pd)(\s*.*)$/.exec(i),l=(e==null?void 0:e[1])??"",r=(e==null?void 0:e[2])??"//!pd",u=(e==null?void 0:e[3])??"";return`${k(l)}<span class="${R.section}">${k(r)}</span>${K(u)}`}if(o==="---")return`<span class="${R.sep}">${k(i)}</span>`;const s=/^(\s*)(-)(?=[ ]|$)/.exec(i);if(s){const e=k(s[1]??""),l=(s[1]??"").length+1,r=i.slice(l),u=r.trimStart(),p=r.slice(0,r.length-u.length),b=`<span class="${R.item}">-</span>`;if(!z.hasLiteralColon(u)&&z.matchKeyValue(u)){const v=z.findKeyValueSeparator(u),S=u.slice(0,v),E=u.slice(v+1);return`${e}${b}${k(p)}${we("","",S,E)}`}return`${e}${b}${k(p)}${K(u)}`}if(!z.hasLiteralColon(o)&&z.matchKeyValue(o)){const l=i.length-i.trimStart().length,r=i.slice(l),u=z.findKeyValueSeparator(r),p=r.slice(0,u).trim(),b=r.slice(u+1);return we(k(i.slice(0,l)),"",p,b)}return K(i)}function Ae(i){const o=i.split(`
13
+ `);let s=!1;const e=[];for(const l of o){const r=l.trimStart();if(s){r.startsWith("```")?(s=!1,e.push(`<span class="${R.fence}">${k(l)}</span>`)):e.push(k(l));continue}if(r.startsWith("```")){s=!0,e.push(`<span class="${R.fence}">${k(l)}</span>`);continue}e.push(Ve(l))}return e.join(`
14
+ `)}function _e(i){return o=>i==="pd"?Ae(o):Te(o)}function qe(i,o={}){return Be(i,o,_e)}Object.defineProperty(exports,"format",{enumerable:!0,get:()=>z.format});Object.defineProperty(exports,"jsonToPdText",{enumerable:!0,get:()=>z.jsonToPdText});Object.defineProperty(exports,"pdToJsonText",{enumerable:!0,get:()=>z.pdToJsonText});exports.createPdEditor=qe;exports.highlightPd=Ae;
package/dist/pd.d.ts CHANGED
@@ -1,9 +1,9 @@
1
- import type { PdEditorInstance, PdEditorOptions } from "./core";
1
+ import { type PdEditorInstance, type PdEditorOptions } from "./core";
2
2
  export type { EditorLang, PdEditorInstance, PdEditorOptions } from "./core";
3
3
  export { format, jsonToPdText, pdToJsonText } from "@andares/pdfoundation";
4
4
  export { highlightPd } from "./pd-highlight";
5
5
  /**
6
- * Headless 提示词输入框(基于 Yace)——pd-only 精简入口。
6
+ * 低 UI 提示词输入框——pd-only 精简入口。
7
7
  * 仅 pd 语言高亮(自研 tokenizer);setLanguage 切到 md/xml/json/yaml 时内容
8
8
  * 以纯文本渲染(无高亮但功能完好)。需要其余语言内置高亮时用主入口
9
9
  * `@andares/pdeditor`(含 Prism),或通过 options.highlight 自带高亮器(BYO)。