@softwarity/geojson-editor 1.0.20 → 1.0.22

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 CHANGED
@@ -446,6 +446,29 @@ editor.goToPrevError();
446
446
  | `PageUp` | Scroll up one page |
447
447
  | `PageDown` | Scroll down one page |
448
448
  | `Shift+Home/End/PageUp/PageDown` | Extend selection while navigating |
449
+ | `Ctrl+I` / `Cmd+I` | Add feature via prompt (requires `internal-add-shortcut` attribute) |
450
+
451
+ ### Overriding Shortcuts
452
+
453
+ All keyboard shortcuts handled by the editor call `stopPropagation()` to prevent them from bubbling up. To override a default shortcut behavior, use a capture-phase event listener:
454
+
455
+ ```javascript
456
+ editor.addEventListener('keydown', async (e) => {
457
+ // Override Ctrl+S to save to a remote server instead of downloading
458
+ if (e.ctrlKey && e.key === 's') {
459
+ e.preventDefault();
460
+ e.stopPropagation();
461
+ await saveToRemoteServer(editor.getAll());
462
+ }
463
+ // Override Ctrl+O to load from a remote API instead of local filesystem
464
+ if (e.ctrlKey && e.key === 'o') {
465
+ e.preventDefault();
466
+ e.stopPropagation();
467
+ const features = await openRemoteFeatureSelector();
468
+ if (features) editor.set(features);
469
+ }
470
+ }, { capture: true }); // capture: true is required to intercept before the editor
471
+ ```
449
472
 
450
473
  ## Events
451
474
 
@@ -1,9 +1,9 @@
1
1
  /**
2
2
  * @license MIT
3
3
  * @name @softwarity/geojson-editor
4
- * @version 1.0.20
4
+ * @version 1.0.22
5
5
  * @author Softwarity (https://www.softwarity.io/)
6
6
  * @copyright 2025 Softwarity
7
7
  * @see https://github.com/softwarity/geojson-editor
8
8
  */
9
- const e=["type","geometry","properties","coordinates","id","features"],t=["Point","MultiPoint","LineString","MultiLineString","Polygon","MultiPolygon"],s=/"geometry"\s*:/,i=/"properties"\s*:/,n=/"features"\s*:/,r=/^(\s*"[^"]+"\s*:\s*)([{\[])/,o=/^(\s*)([{\[]),?\s*$/,l=/&/g,a=/</g,h=/>/g,c=/([{}[\],:])/g,d=/"([^"]+)"(<span class="json-punctuation">:<\/span>)/g,u=/<span class="geojson-key">"type"<\/span><span class="json-punctuation">:<\/span>(\s*)"([^"]*)"/g,p=/(<span class="json-punctuation">:<\/span>)(\s*)"([^"]*)"/g,g=/(<span class="json-punctuation">:<\/span>)(\s*)(-?\d+\.?\d*(?:e[+-]?\d+)?)/gi,f=/(<span class="json-punctuation">[\[,]<\/span>)(\s*)(-?\d+\.?\d*(?:e[+-]?\d+)?)/gi,m=/^(\s*)(-?\d+\.?\d*(?:e[+-]?\d+)?)/gim,_=/(<span class="json-punctuation">:<\/span>)(\s*)(true|false)/g,C=/(<span class="json-punctuation">:<\/span>)(\s*)(null)/g,v=/(<\/span>|^)([^<]+)(<span|$)/g,b=/^#(?:[0-9a-fA-F]{3}|[0-9a-fA-F]{6})$/,L=/^#?([0-9a-fA-F])([0-9a-fA-F])([0-9a-fA-F])$/,y=/^\s*$/,x=/(\s+)/,w=/"([\w-]+)"\s*:\s*(?:"([^"]*)"|(\btrue\b|\bfalse\b))/g,k=/"([\w-]+)"\s*:\s*(?:"([^"]*)"|(\btrue\b|\bfalse\b))/,S=/"([\w-]+)"\s*:\s*(true|false)/,T=/"type"\s*:\s*"Feature"/,E=/^\s*"([^"]+)"\s*:\s*([{\[])/,j=/^\s*([{\[]),?\s*$/,R=/[{\[]/,N=/[\w-]/,I=/([A-Z])/g,F=/\{/g,A=/\}/g,$=/\[/g,B=/\]/g;function M(e){if(!e)return null;if(void 0!==e.id)return`id:${e.id}`;if(void 0!==e.properties?.id)return`prop:${e.properties.id}`;const t=e.geometry?.type||"null",s=e.geometry,i=JSON.stringify(s?.coordinates||[]);let n=0;for(let r=0;r<i.length;r++)n=(n<<5)-n+i.charCodeAt(r),n&=n;return`hash:${t}:${n.toString(36)}`}function K(e,t){const s="{"===t?"}":"]";let i=0,n=0,r=!1,o=!1;for(const l of e)o?o=!1:"\\"===l&&r?o=!0:'"'!==l?r||(l===t&&i++,l===s&&n++):r=!r;return{open:i,close:n}}function P(e){if(!e||"object"!=typeof e)throw new Error("Feature must be an object");if("Feature"!==e.type)throw new Error('Feature type must be "Feature"');if(!("geometry"in e))throw new Error("Feature must have a geometry property");if(!("properties"in e))throw new Error("Feature must have a properties property");if(null!==e.geometry){if("object"!=typeof e.geometry)throw new Error("Feature geometry must be an object or null");if(!e.geometry.type)throw new Error("Feature geometry must have a type");if(!t.includes(e.geometry.type))throw new Error(`Invalid geometry type: "${e.geometry.type}"`);if(!("coordinates"in e.geometry))throw new Error("Feature geometry must have coordinates")}if(null!==e.properties&&"object"!=typeof e.properties)throw new Error("Feature properties must be an object or null")}function H(e){let t=[];if(Array.isArray(e))t=e;else{if(!e||"object"!=typeof e)throw new Error("Input must be a Feature, array of Features, or FeatureCollection");if("FeatureCollection"===e.type&&"features"in e&&Array.isArray(e.features))t=e.features;else{if("Feature"!==e.type)throw new Error("Input must be a Feature, array of Features, or FeatureCollection");t=[e]}}for(const s of t)P(s);return t}let D=null;function O(e){return",aliceblue,antiquewhite,aqua,aquamarine,azure,beige,bisque,black,blanchedalmond,blue,blueviolet,brown,burlywood,cadetblue,chartreuse,chocolate,coral,cornflowerblue,cornsilk,crimson,cyan,darkblue,darkcyan,darkgoldenrod,darkgray,darkgreen,darkgrey,darkkhaki,darkmagenta,darkolivegreen,darkorange,darkorchid,darkred,darksalmon,darkseagreen,darkslateblue,darkslategray,darkslategrey,darkturquoise,darkviolet,deeppink,deepskyblue,dimgray,dimgrey,dodgerblue,firebrick,floralwhite,forestgreen,fuchsia,gainsboro,ghostwhite,gold,goldenrod,gray,green,greenyellow,grey,honeydew,hotpink,indianred,indigo,ivory,khaki,lavender,lavenderblush,lawngreen,lemonchiffon,lightblue,lightcoral,lightcyan,lightgoldenrodyellow,lightgray,lightgreen,lightgrey,lightpink,lightsalmon,lightseagreen,lightskyblue,lightslategray,lightslategrey,lightsteelblue,lightyellow,lime,limegreen,linen,magenta,maroon,mediumaquamarine,mediumblue,mediumorchid,mediumpurple,mediumseagreen,mediumslateblue,mediumspringgreen,mediumturquoise,mediumvioletred,midnightblue,mintcream,mistyrose,moccasin,navajowhite,navy,oldlace,olive,olivedrab,orange,orangered,orchid,palegoldenrod,palegreen,paleturquoise,palevioletred,papayawhip,peachpuff,peru,pink,plum,powderblue,purple,rebeccapurple,red,rosybrown,royalblue,saddlebrown,salmon,sandybrown,seagreen,seashell,sienna,silver,skyblue,slateblue,slategray,slategrey,snow,springgreen,steelblue,tan,teal,thistle,tomato,turquoise,violet,wheat,white,whitesmoke,yellow,yellowgreen,".includes(","+e.toLowerCase()+",")}function V(s,i,n){if(!s)return"";let L=s,w=null;if(n?.collapseButton?.isCollapsed){const e=s.match(r),t=!e&&s.match(o);e?(L=e[1]+e[2],w=e[2]):t&&(L=t[1]+t[2],w=t[2])}let k=L.replace(l,"&amp;").replace(a,"&lt;").replace(h,"&gt;");if(k=k.replace(c,'<span class="json-punctuation">$1</span>'),d.lastIndex=0,k=k.replace(d,(t,s,n)=>"properties"!==i&&e.includes(s)?`<span class="geojson-key">"${s}"</span>${n}`:`<span class="json-key">"${s}"</span>${n}`),"properties"!==i&&(u.lastIndex=0,k=k.replace(u,(e,s,i)=>`<span class="geojson-key">"type"</span><span class="json-punctuation">:</span>${s}<span class="${"Feature"===i||"FeatureCollection"===i||t.includes(i)?"geojson-type":"geojson-type-invalid"}">"${i}"</span>`)),p.lastIndex=0,k=k.replace(p,(e,t,s,i)=>e.includes("geojson-type")||e.includes("json-string")?e:b.test(i)||O(i)?`${t}${s}<span class="json-string json-color" data-color="${i}" style="--swatch-color: ${i}">"${i}"</span>`:`${t}${s}<span class="json-string">"${i}"</span>`),g.lastIndex=0,k=k.replace(g,'$1$2<span class="json-number">$3</span>'),f.lastIndex=0,k=k.replace(f,'$1$2<span class="json-number">$3</span>'),m.lastIndex=0,k=k.replace(m,'$1<span class="json-number">$2</span>'),_.lastIndex=0,k=k.replace(_,(e,t,s,i)=>`${t}${s}<span class="json-boolean${"true"===i?" json-bool-true":" json-bool-false"}">${i}</span>`),C.lastIndex=0,k=k.replace(C,'$1$2<span class="json-null">$3</span>'),w){const e="["===w?"collapsed-bracket-array":"collapsed-bracket-object";k=k.replace(new RegExp(`<span class="json-punctuation">\\${w}<\\/span>$`),`<span class="${e}">${w}</span>`)}return v.lastIndex=0,k=k.replace(v,(e,t,s,i)=>{if(!s||y.test(s))return e;const n=s.split(x);let r=!1;const o=n.map(e=>y.test(e)?e:(r=!0,`<span class="json-error">${e}</span>`)).join("");return r?t+o+i:e}),k}const z=e=>document.createElement(e);class q extends HTMLElement{constructor(){super(),this.lines=[],this.collapsedNodes=/* @__PURE__ */new Set,this.hiddenFeatures=/* @__PURE__ */new Set,this._nodeIdCounter=0,this._lineToNodeId=/* @__PURE__ */new Map,this._nodeIdToLines=/* @__PURE__ */new Map,this._openedNodeKeys=/* @__PURE__ */new Set,this.visibleLines=[],this.lineMetadata=/* @__PURE__ */new Map,this.featureRanges=/* @__PURE__ */new Map,this.viewportHeight=0,this.lineHeight=19.5,this.bufferLines=5,this._lastStartIndex=-1,this._lastEndIndex=-1,this._lastTotalLines=-1,this._scrollRaf=null,this.cursorLine=0,this.cursorColumn=0,this.selectionStart=null,this.selectionEnd=null,this.renderTimer=void 0,this.inputTimer=void 0,this.themes={dark:{},light:{}},this._undoStack=[],this._redoStack=[],this._maxHistorySize=100,this._lastActionTime=0,this._lastActionType=null,this._groupingDelay=500,this._isSelecting=!1,this._isComposing=!1,this._blockRender=!1,this._insertMode=!0,this._charWidth=null,this._contextMapCache=null,this._contextMapLinesLength=0,this._contextMapFirstLine=void 0,this._contextMapLastLine=void 0,this._errorLinesCache=null,this._viewport=null,this._linesContainer=null,this._scrollContent=null,this._hiddenTextarea=null,this._gutterContent=null,this._gutterScrollContent=null,this._gutterScroll=null,this._gutter=null,this._clearBtn=null,this._editorWrapper=null,this._placeholderLayer=null,this._editorPrefix=null,this._editorSuffix=null,this._errorNav=null,this._errorCount=null,this._prevErrorBtn=null,this._nextErrorBtn=null,this.attachShadow({mode:"open"})}_id(e){return this.shadowRoot.getElementById(e)}_invalidateRenderCache(){this._lastStartIndex=-1,this._lastEndIndex=-1,this._lastTotalLines=-1}_createSnapshot(){return{lines:[...this.lines],cursorLine:this.cursorLine,cursorColumn:this.cursorColumn,timestamp:Date.now()}}_restoreSnapshot(e){this.lines=[...e.lines],this.cursorLine=e.cursorLine,this.cursorColumn=e.cursorColumn,this.updateModel(),this._invalidateRenderCache(),this.scheduleRender(),this.updatePlaceholderVisibility(),this.emitChange()}_saveToHistory(e="edit"){const t=Date.now();if(!(e===this._lastActionType&&t-this._lastActionTime<this._groupingDelay)){const e=this._createSnapshot();this._undoStack.push(e),this._undoStack.length>this._maxHistorySize&&this._undoStack.shift(),this._redoStack=[]}this._lastActionTime=t,this._lastActionType=e}undo(){if(0===this._undoStack.length)return!1;this._redoStack.push(this._createSnapshot());const e=this._undoStack.pop();return e&&this._restoreSnapshot(e),this._lastActionType=null,this._lastActionTime=0,!0}redo(){if(0===this._redoStack.length)return!1;this._undoStack.push(this._createSnapshot());const e=this._redoStack.pop();return e&&this._restoreSnapshot(e),this._lastActionType=null,this._lastActionTime=0,!0}clearHistory(){this._undoStack=[],this._redoStack=[],this._lastActionType=null,this._lastActionTime=0}canUndo(){return this._undoStack.length>0}canRedo(){return this._redoStack.length>0}_generateNodeId(){return"node_"+ ++this._nodeIdCounter}_getCollapsedRangeForLine(e){for(const[t,s]of this._nodeIdToLines)if(this.collapsedNodes.has(t)&&e>s.startLine&&e<s.endLine)return{nodeId:t,...s};return null}_getCollapsedClosingLine(e){for(const[t,s]of this._nodeIdToLines)if(this.collapsedNodes.has(t)&&e===s.endLine)return{nodeId:t,...s};return null}_getClosingBracketPos(e){return Math.max(e.lastIndexOf("]"),e.lastIndexOf("}"))}_getCollapsedNodeAtLine(e){const t=this._lineToNodeId.get(e);if(t&&this.collapsedNodes.has(t)){const e=this._nodeIdToLines.get(t);if(e)return{nodeId:t,...e}}return null}_getContainingExpandedNode(e){let t=null;for(const[s,i]of this._nodeIdToLines)this.collapsedNodes.has(s)||e>=i.startLine&&e<=i.endLine&&(!t||i.endLine-i.startLine<t.endLine-t.startLine)&&(t={nodeId:s,...i});return t}_deleteCollapsedNode(e){this._saveToHistory("delete");const t=e.endLine-e.startLine+1;this.lines.splice(e.startLine,t),this.cursorLine=Math.min(e.startLine,this.lines.length-1),this.cursorColumn=0,this.formatAndUpdate()}_rebuildNodeIdMappings(){const e=/* @__PURE__ */new Set;for(const s of this.collapsedNodes){const t=this._nodeIdToLines.get(s);t?.uniqueKey&&e.add(t.uniqueKey)}this._nodeIdCounter=0,this._lineToNodeId.clear(),this._nodeIdToLines.clear(),this.collapsedNodes.clear();const t=/* @__PURE__ */new Map;for(let s=0;s<this.lines.length;s++){const i=this.lines[s],n=i.match(E),r=!n&&i.match(j);if(!n&&!r)continue;let o,l;if(n)o=n[1],l=n[2];else{if(!r)continue;l=r[1],o=`__root_${l}_${s}`}const a=K(i.substring(i.indexOf(l)+1),l);if(a.close>a.open)continue;const h=this._findClosingLine(s,l);if(-1===h||h===s)continue;const c=this._generateNodeId(),d=t.get(o)||0;t.set(o,d+1);const u=`${o}:${d}`;this._lineToNodeId.set(s,c),this._nodeIdToLines.set(c,{startLine:s,endLine:h,nodeKey:o,uniqueKey:u,isRootFeature:!!r}),e.has(u)&&!this._openedNodeKeys.has(u)&&this.collapsedNodes.add(c)}}static get observedAttributes(){return["readonly","value","placeholder","dark-selector"]}connectedCallback(){this.render(),this._cacheElements(),this.setupEventListeners(),this.updatePrefixSuffix(),this.updateThemeCSS(),this.value&&this.setValue(this.value),this.updatePlaceholderVisibility()}disconnectedCallback(){this.renderTimer&&clearTimeout(this.renderTimer),this.inputTimer&&clearTimeout(this.inputTimer);const e=document.querySelector(".geojson-color-picker-input");e&&(e._closeListener&&document.removeEventListener("click",e._closeListener,!0),e.remove())}attributeChangedCallback(e,t,s){if(t!==s)switch(e){case"value":this.setValue(s);break;case"readonly":this.updateReadonly();break;case"placeholder":this.updatePlaceholderContent();break;case"dark-selector":this.updateThemeCSS()}}get readonly(){return this.hasAttribute("readonly")}get value(){return this.getAttribute("value")||""}get placeholder(){return this.getAttribute("placeholder")||""}get prefix(){return'{"type": "FeatureCollection", "features": ['}get suffix(){return"]}"}render(){const e=this.shadowRoot,t=z("style");t.textContent=':host *,:host *:before,:host *:after{box-sizing:border-box;font: 13px/1.5 Courier New,Courier,monospace;font-variant:normal;letter-spacing:0;word-spacing:0;text-transform:none;text-decoration:none;text-indent:0}:host{--line-height: 19.5px;--gutter-width: 50px;--editor-padding-y: 8px;--editor-padding-x: 12px;display:flex;flex-direction:column;position:relative;width:100%;height:400px;border-radius:4px;overflow:hidden}:host([readonly]) .editor-wrapper:after{content:"";position:absolute;inset:0;pointer-events:none;background:repeating-linear-gradient(-45deg,rgba(128,128,128,.08),rgba(128,128,128,.08) 3px,transparent 3px,transparent 12px);z-index:1}:host([readonly]) .hidden-textarea{cursor:text}.editor-wrapper{position:relative;width:100%;flex:1;background:var(--bg-color, #fff);display:flex;overflow:hidden}.gutter{width:var(--gutter-width);background:var(--gutter-bg, #f0f0f0);border-right:1px solid var(--gutter-border, #e0e0e0);overflow:hidden;flex-shrink:0;position:relative}.gutter-scroll{position:absolute;inset:0;overflow:hidden;padding:8px 0}.gutter-scroll-content{position:relative;width:100%}.gutter-content{position:absolute;top:0;left:0;right:0;will-change:transform}.gutter-line{height:var(--line-height);display:flex;align-items:center;justify-content:flex-end;padding-right:4px;position:relative}.gutter-line.has-error:before{content:"";position:absolute;right:0;top:0;bottom:0;width:3px;background:var(--error-color, #dc3545)}.line-number{font-size:11px;color:var(--gutter-text, #999);-webkit-user-select:none;user-select:none;min-width:20px;text-align:right}.collapse-column{width:16px;display:flex;align-items:center;justify-content:center;flex-shrink:0}.collapse-button{width:12px;height:12px;border-radius:2px;cursor:pointer;flex-shrink:0;background:transparent;border:none;color:var(--json-punct, #a9b7c6);font-size:10px;display:flex;align-items:center;justify-content:center;-webkit-user-select:none;user-select:none;opacity:0;transition:transform .1s,opacity .15s}.collapse-button.collapsed,.gutter:hover .collapse-button{opacity:1}.collapse-button:hover{transform:scale(1.2)}@media(hover:none),(pointer:coarse){.collapse-button{opacity:1}}.editor-content{position:relative;flex:1;overflow:hidden}.hidden-textarea{position:absolute;top:0;left:0;width:100%;height:100%;opacity:0;padding:0;margin:0;border:none;outline:none;resize:none;overflow:hidden;z-index:-1;pointer-events:none;caret-color:transparent}.viewport{position:absolute;inset:0;overflow:auto;padding:var(--editor-padding-y) var(--editor-padding-x);overscroll-behavior:contain;scrollbar-width:thin;scrollbar-color:var(--control-border, #c0c0c0) var(--control-bg, #e8e8e8)}.scroll-content{position:relative;width:100%}.lines-container{position:absolute;top:0;left:0;right:0;will-change:transform}.line{height:var(--line-height);white-space:pre;display:block;position:relative}.cursor{position:absolute;width:2px;height:1em;background:var(--caret-color, #000);top:.15em;pointer-events:none;animation:cursor-blink 1s step-end infinite}.cursor.cursor-block{width:.6em;opacity:.5;animation:cursor-blink-block 1s step-end infinite}@keyframes cursor-blink-block{0%,to{opacity:.5}50%{opacity:.2}}@keyframes cursor-blink{0%,to{opacity:1}50%{opacity:0}}.selection{position:absolute;height:100%;top:0;background:var(--selection-color, rgba(51, 153, 255, .3));pointer-events:none;z-index:0}.line-hidden>span{opacity:.35;filter:grayscale(50%)}.placeholder-layer{position:absolute;top:var(--editor-padding-y);left:var(--editor-padding-x);color:#6a6a6a;pointer-events:none;z-index:0;white-space:pre}.json-key{color:var(--json-key, #660e7a)}.json-string{color:var(--json-string, #008000)}.json-number{color:var(--json-number, #00f)}.json-boolean,.json-null{color:var(--json-boolean, #000080)}.json-punctuation{color:var(--json-punct, #000)}.json-error{color:var(--json-error, #f00)}.geojson-key{color:var(--geojson-key, #660e7a);font-weight:600}.geojson-type{color:var(--geojson-type, #008000);font-weight:600}.geojson-type-invalid{color:var(--geojson-type-invalid, #f00);font-weight:600}.collapsed-bracket-array,.collapsed-bracket-object{color:var(--json-punct, #000)}.collapsed-bracket-array:after,.collapsed-bracket-object:after{content:"…";color:var(--json-punct, #888)}.prefix-wrapper,.suffix-wrapper{display:flex;flex-shrink:0;background:var(--bg-color, #fff)}.prefix-gutter,.suffix-gutter{width:var(--gutter-width);background:var(--gutter-bg, #f0f0f0);border-right:1px solid var(--gutter-border, #e0e0e0);flex-shrink:0}.editor-prefix,.editor-suffix{flex:1;padding:4px 12px;color:var(--text-color, #000);background:var(--bg-color, #fff);-webkit-user-select:none;user-select:none;white-space:pre-wrap;word-wrap:break-word;opacity:.6}.prefix-wrapper{border-bottom:1px solid rgba(255,255,255,.1);position:relative}.suffix-wrapper{border-top:1px solid rgba(255,255,255,.1);position:relative}.info-btn{position:absolute;right:.5rem;top:50%;transform:translateY(-50%);background:transparent;border:none;color:var(--text-color, #000);opacity:.15;cursor:pointer;font-size:.7rem;width:1rem;height:1rem;padding:0;border-radius:50%;display:flex;align-items:center;justify-content:center;transition:opacity .2s;font-family:sans-serif}.info-btn:hover{opacity:.5}.clear-btn{position:absolute;right:.5rem;top:50%;transform:translateY(-50%);background:transparent;border:none;color:var(--text-color, #000);opacity:.3;cursor:pointer;font-size:.65rem;width:1rem;height:1rem;padding:.15rem 0 0;border-radius:3px;display:flex;align-items:center;justify-content:center;transition:opacity .2s,background .2s}.clear-btn:hover{opacity:.7;background:#ffffff1a}.clear-btn[hidden]{display:none}.error-nav{display:none;align-items:center;gap:4px;margin-right:24px}.error-nav.visible{display:flex}.error-nav-btn{background:transparent;border:none;color:var(--error-color, #dc3545);cursor:pointer;font-size:8px;width:16px;height:16px;padding:0;display:flex;align-items:center;justify-content:center;opacity:.7;transition:opacity .2s}.error-nav-btn:hover{opacity:1}.error-count{color:var(--error-color, #dc3545);font-size:11px;min-width:20px;text-align:center}.viewport::-webkit-scrollbar{width:10px;height:10px}.viewport::-webkit-scrollbar-track{background:var(--control-bg, #e8e8e8)}.viewport::-webkit-scrollbar-thumb{background:var(--control-border, #c0c0c0);border-radius:5px}.viewport::-webkit-scrollbar-thumb:hover{background:var(--control-color, #000080)}.json-color,.json-boolean{position:relative}.json-color:before,.json-boolean:before{content:"";position:absolute;left:-8px;top:50%;transform:translateY(-50%);margin-top:-1px;width:8px;height:8px;border-radius:2px;cursor:pointer}.json-color:hover:before,.json-boolean:hover:before{transform:translateY(-50%) scale(1.2);border-color:var(--control-color, #000080)}.json-color:before{background-color:var(--swatch-color);border:1px solid var(--json-punct, #a9b7c6)}.json-boolean:before{border:1.5px solid var(--control-border, #c0c0c0);background:transparent}.json-bool-true:before{content:"✔";border-color:var(--control-color, #000080);color:var(--control-color, #000080);font-size:8px;display:flex;align-items:center;justify-content:center;line-height:8px}.line.has-visibility:before{content:"👁";position:absolute;left:0;top:3px;font-size:10px;color:var(--control-color, #000080);opacity:.6;cursor:pointer;z-index:1}.line.has-visibility:hover:before{opacity:1;transform:scale(1.15)}.line.has-visibility.feature-hidden:before{opacity:.5}';const s=z("div");for(s.innerHTML=function(e="",t=""){return`\n <div class="prefix-wrapper">\n <div class="prefix-gutter"></div>\n <div class="editor-prefix" id="editorPrefix"></div>\n <button class="info-btn" id="infoBtn" title="@softwarity/geojson-editor v${t}" aria-label="About">ⓘ</button>\n </div>\n <div class="editor-wrapper">\n <div class="gutter">\n <div class="gutter-scroll" id="gutterScroll">\n <div class="gutter-scroll-content" id="gutterScrollContent">\n <div class="gutter-content" id="gutterContent"></div>\n </div>\n </div>\n </div>\n <div class="editor-content">\n <div class="placeholder-layer" id="placeholderLayer">${s=e,s?s.replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;"):""}</div>\n <textarea\n class="hidden-textarea"\n id="hiddenTextarea"\n spellcheck="false"\n autocomplete="off"\n autocorrect="off"\n autocapitalize="off"\n tabindex="0"\n ></textarea>\n <div class="viewport" id="viewport">\n <div class="scroll-content" id="scrollContent">\n <div class="lines-container" id="linesContainer"></div>\n </div>\n </div>\n </div>\n </div>\n <div class="suffix-wrapper">\n <div class="suffix-gutter"></div>\n <div class="editor-suffix" id="editorSuffix"></div>\n <div class="error-nav" id="errorNav">\n <button class="error-nav-btn" id="prevErrorBtn" title="Previous error">◀</button>\n <span class="error-count" id="errorCount"></span>\n <button class="error-nav-btn" id="nextErrorBtn" title="Next error">▶</button>\n </div>\n <button class="clear-btn" id="clearBtn" title="Clear editor">✕</button>\n </div>\n `;var s}(this.placeholder,"1.0.20"),e.innerHTML="",e.appendChild(t);s.firstChild;)e.appendChild(s.firstChild)}_cacheElements(){this._viewport=this._id("viewport"),this._linesContainer=this._id("linesContainer"),this._scrollContent=this._id("scrollContent"),this._hiddenTextarea=this._id("hiddenTextarea"),this._gutterContent=this._id("gutterContent"),this._gutterScrollContent=this._id("gutterScrollContent"),this._gutterScroll=this._id("gutterScroll"),this._gutter=this.shadowRoot.querySelector(".gutter"),this._clearBtn=this._id("clearBtn"),this._editorWrapper=this.shadowRoot.querySelector(".editor-wrapper"),this._placeholderLayer=this._id("placeholderLayer"),this._editorPrefix=this._id("editorPrefix"),this._editorSuffix=this._id("editorSuffix"),this._errorNav=this._id("errorNav"),this._errorCount=this._id("errorCount"),this._prevErrorBtn=this._id("prevErrorBtn"),this._nextErrorBtn=this._id("nextErrorBtn")}setupEventListeners(){const e=this._hiddenTextarea,t=this._viewport,s=this._gutterContent,i=this._gutter,n=this._clearBtn,r=this._editorWrapper;if(!(e&&t&&s&&i&&n&&r))return;this._isSelecting=!1,t.addEventListener("click",e=>{this.handleEditorClick(e)},!0),t.addEventListener("mousedown",t=>{const s=t.target,i=s.closest(".line.has-visibility");if(i){const e=i.getBoundingClientRect();if(t.clientX-e.left<14)return void(this._blockRender=!0)}if(s.classList.contains("json-color")||s.classList.contains("json-boolean")){const e=s.getBoundingClientRect(),i=t.clientX-e.left;if(i<0&&i>=-8)return void(this._blockRender=!0)}if(t.preventDefault(),2===t.detail){const s=this._getPositionFromClick(t);return this._selectWordAt(s.line,s.column),this._isSelecting=!1,e.focus(),this._invalidateRenderCache(),void this.scheduleRender()}const n=this._getPositionFromClick(t);t.shiftKey&&this.selectionStart?(this.selectionEnd=n,this.cursorLine=n.line,this.cursorColumn=n.column):(this.cursorLine=n.line,this.cursorColumn=n.column,this.selectionStart={line:n.line,column:n.column},this.selectionEnd=null,this._isSelecting=!0),e.focus(),this._invalidateRenderCache(),this.scheduleRender()}),t.addEventListener("mousemove",e=>{if(!this._isSelecting)return;const s=this._getPositionFromClick(e);this.selectionEnd=s,this.cursorLine=s.line,this.cursorColumn=s.column;const i=t.getBoundingClientRect();e.clientY<i.top+30?t.scrollTop-=20:e.clientY>i.bottom-30&&(t.scrollTop+=20),this._invalidateRenderCache(),this.scheduleRender()}),document.addEventListener("mouseup",()=>{this._isSelecting=!1}),e.addEventListener("focus",()=>{r.classList.add("focused"),this._invalidateRenderCache(),this.scheduleRender()}),e.addEventListener("blur",()=>{r.classList.remove("focused"),this._invalidateRenderCache(),this.scheduleRender()});let o=!1;t.addEventListener("scroll",()=>{o||(this.syncGutterScroll(),this._scrollRaf||(this._scrollRaf=requestAnimationFrame(()=>{this._scrollRaf=null,o=!0,this.renderViewport(),o=!1})))}),e.addEventListener("compositionstart",()=>{this._isComposing=!0}),e.addEventListener("compositionend",()=>{this._isComposing=!1,this.handleInput()}),e.addEventListener("input",()=>{this._isComposing||this.handleInput()}),e.addEventListener("keydown",e=>{this.handleKeydown(e)}),e.addEventListener("paste",e=>{this.handlePaste(e)}),e.addEventListener("copy",e=>{this.handleCopy(e)}),e.addEventListener("cut",e=>{this.handleCut(e)}),s.addEventListener("click",e=>{this.handleGutterClick(e)}),i.addEventListener("mousedown",e=>{e.preventDefault()}),i.addEventListener("wheel",e=>{e.preventDefault(),t.scrollTop+=e.deltaY}),n.addEventListener("click",()=>{this.removeAll()}),this._prevErrorBtn?.addEventListener("click",()=>{this.goToPrevError()}),this._nextErrorBtn?.addEventListener("click",()=>{this.goToNextError()}),this.updateReadonly()}setValue(e,t=!0){if(this.lines.length>0&&this._saveToHistory("setValue"),e&&e.trim())try{const t="["+e+"]",s=JSON.parse(t),i=JSON.stringify(s,null,2).split("\n");this.lines=i.slice(1,-1)}catch(s){this.lines=e.split("\n")}else this.lines=[];this.collapsedNodes.clear(),this.hiddenFeatures.clear(),this._openedNodeKeys.clear(),this._lineToNodeId.clear(),this._nodeIdToLines.clear(),this.cursorLine=0,this.cursorColumn=0,this.updateModel(),this.scheduleRender(),this.updatePlaceholderVisibility(),t&&this.lines.length>0&&requestAnimationFrame(()=>{this.autoCollapseCoordinates()}),this.emitChange()}getContent(){return this.lines.join("\n")}updateModel(){this._contextMapCache=null,this._errorLinesCache=null,this._rebuildNodeIdMappings(),this.computeFeatureRanges(),this.computeLineMetadata(),this.computeVisibleLines()}updateView(){this.computeLineMetadata(),this.computeVisibleLines()}computeFeatureRanges(){this.featureRanges.clear();try{const e=this.lines.join("\n"),t=this.prefix+e+this.suffix,s=JSON.parse(t);if(!s.features)return;let i=0,n=0,r=!1,o=-1,l=null;for(let a=0;a<this.lines.length;a++){const e=this.lines[a];if(!r&&T.test(e)){let e=a;for(let t=a;t>=0;t--){const s=this.lines[t].trim();if("{"===s||"{,"===s){e=t;break}}o=e,r=!0,n=1;for(let t=e;t<=a;t++){const s=K(this.lines[t],"{");n+=t===e?s.open-1-s.close:s.open-s.close}i<s.features.length&&(l=M(s.features[i]))}else if(r){const t=K(e,"{");n+=t.open-t.close,n<=0&&(l&&this.featureRanges.set(l,{startLine:o,endLine:a,featureIndex:i}),i++,r=!1,l=null)}}}catch(e){}}computeLineMetadata(){this.lineMetadata.clear();const e=this._findCollapsibleRanges(),t=this._computeErrorLines();for(let s=0;s<this.lines.length;s++){const i=this.lines[s],n={colors:[],booleans:[],collapseButton:null,visibilityButton:null,isHidden:!1,isCollapsed:!1,featureKey:null,hasError:t.has(s)};let r;for(w.lastIndex=0;null!==(r=w.exec(i));){const[,e,t,s]=r;s?n.booleans.push({attributeName:e,value:"true"===s}):t&&(b.test(t)||O(t))&&n.colors.push({attributeName:e,color:t})}const o=e.find(e=>e.startLine===s);o&&(n.collapseButton={nodeKey:o.nodeKey,nodeId:o.nodeId,isCollapsed:this.collapsedNodes.has(o.nodeId)}),e.find(e=>this.collapsedNodes.has(e.nodeId)&&s>e.startLine&&s<e.endLine)&&(n.isCollapsed=!0);for(const[e,t]of this.featureRanges)if(s>=t.startLine&&s<=t.endLine){n.featureKey=e,this.hiddenFeatures.has(e)&&(n.isHidden=!0),s===t.startLine&&(n.visibilityButton={featureKey:e,isHidden:this.hiddenFeatures.has(e)});break}this.lineMetadata.set(s,n)}}_computeErrorLines(){if(null!==this._errorLinesCache)return this._errorLinesCache;const e=/* @__PURE__ */new Set;for(let s=0;s<this.lines.length;s++)V(this.lines[s],"",void 0).includes("json-error")&&e.add(s);try{const e="["+this.lines.join("\n")+"]";JSON.parse(e)}catch(t){if(t instanceof Error){const s=t.message.match(/line (\d+)/);if(s){const t=Math.max(0,parseInt(s[1],10)-1);e.add(t)}}}return this._errorLinesCache=e,e}_getErrorLines(){const e=[];for(const[t,s]of this.lineMetadata)s.hasError&&e.push(t);return e.sort((e,t)=>e-t)}goToNextError(){const e=this._getErrorLines();if(0===e.length)return!1;const t=e.find(e=>e>this.cursorLine),s=void 0!==t?t:e[0];return this._goToErrorLine(s)}goToPrevError(){const e=this._getErrorLines();if(0===e.length)return!1;const t=e.filter(e=>e<this.cursorLine),s=t.length>0?t[t.length-1]:e[e.length-1];return this._goToErrorLine(s)}_expandNodesContainingLine(e){let t=!1;for(const[s,i]of this._nodeIdToLines)this.collapsedNodes.has(s)&&e>i.startLine&&e<=i.endLine&&(this.collapsedNodes.delete(s),i.uniqueKey&&this._openedNodeKeys.add(i.uniqueKey),t=!0);return t}_goToErrorLine(e){return this._expandNodesContainingLine(e)&&this.updateView(),this.cursorLine=e,this.cursorColumn=0,this._invalidateRenderCache(),this._scrollToCursor(!0),this.renderViewport(),this._updateErrorDisplay(),this._hiddenTextarea?.focus(),!0}_expandErrorNodes(){const e=this._getErrorLines();if(0===e.length)return;let t=!1;for(const s of e)this._expandNodesContainingLine(s)&&(t=!0);t&&this.updateView()}computeVisibleLines(){this.visibleLines=[];for(let e=0;e<this.lines.length;e++){const t=this.lineMetadata.get(e);t&&t.isCollapsed||this.visibleLines.push({index:e,content:this.lines[e],meta:t})}this._invalidateRenderCache()}scheduleRender(){this.renderTimer||(this.renderTimer=requestAnimationFrame(()=>{this.renderTimer=void 0,this.renderViewport()}))}renderViewport(){if(this._blockRender)return;const e=this._viewport,t=this._linesContainer,s=this._scrollContent;if(!e||!t)return;this.viewportHeight=e.clientHeight;const i=this.visibleLines.length,n=i*this.lineHeight;if(s){s.style.height=`${n}px`;const e=this._getCharWidth(),t=this.lines.reduce((e,t)=>Math.max(e,t.length),0)*e+20;s.style.minWidth=`${t}px`}const r=e.scrollTop,o=Math.floor(r/this.lineHeight),l=Math.ceil(this.viewportHeight/this.lineHeight),a=Math.max(0,o-this.bufferLines),h=Math.min(i,o+l+this.bufferLines);if(i>0&&this._lastStartIndex===a&&this._lastEndIndex===h&&this._lastTotalLines===i)return;this._lastStartIndex=a,this._lastEndIndex=h,this._lastTotalLines=i;const c=a*this.lineHeight;t.style.transform=`translateY(${c}px)`;const d=this._buildContextMap(),u=this._editorWrapper?.classList.contains("focused"),p=document.createDocumentFragment();if(0===i){const e=z("div");return e.className="line empty-line",e.dataset.lineIndex="0",u&&(e.innerHTML=this._insertCursor(0)),p.appendChild(e),t.innerHTML="",t.appendChild(p),void this.renderGutter(0,0)}for(let g=a;g<h;g++){const e=this.visibleLines[g];if(!e)continue;const t=z("div");t.className="line",t.dataset.lineIndex=String(e.index),e.meta?.visibilityButton&&(t.classList.add("has-visibility"),t.dataset.featureKey=e.meta.visibilityButton.featureKey,e.meta.visibilityButton.isHidden&&t.classList.add("feature-hidden")),e.meta?.isHidden&&t.classList.add("line-hidden");const s=d.get(e.index)||"Feature";let i=V(e.content,s,e.meta);u&&this._hasSelection()&&(i=this._addSelectionHighlight(i,e.index,e.content)),u&&e.index===this.cursorLine&&(i+=this._insertCursor(this.cursorColumn)),t.innerHTML=i,p.appendChild(t)}t.innerHTML="",t.appendChild(p),this.renderGutter(a,h)}_insertCursor(e){const t=this._getCharWidth(),s=e*t;return this._insertMode?`<span class="cursor" style="left: ${s}px"></span>`:`<span class="cursor cursor-block" style="left: ${s}px; width: ${t}px"></span>`}_addSelectionHighlight(e,t,s){const i=this._normalizeSelection();if(!i)return e;const{start:n,end:r}=i;if(t<n.line||t>r.line)return e;const o=this._getCharWidth();let l,a;return t===n.line&&t===r.line?(l=n.column,a=r.column):t===n.line?(l=n.column,a=s.length):t===r.line?(l=0,a=r.column):(l=0,a=s.length),`<span class="selection" style="left: ${l*o}px; width: ${(a-l)*o}px"></span>`+e}_getCharWidth(){if(!this._charWidth){const e=z("canvas").getContext("2d");e?(e.font="13px 'Courier New', Courier, monospace",this._charWidth=e.measureText("M").width):this._charWidth=7.8}return this._charWidth}renderGutter(e,t){const s=this._gutterContent,i=this._gutterScrollContent;if(!s)return;const n=this.visibleLines.length*this.lineHeight;i&&(i.style.height=`${n}px`);const r=e*this.lineHeight;s.style.transform=`translateY(${r}px)`;const o=document.createDocumentFragment();for(let l=e;l<t;l++){const e=this.visibleLines[l];if(!e)continue;const t=z("div");t.className="gutter-line";const s=e.meta;s?.hasError&&t.classList.add("has-error");const i=z("span");i.className="line-number",i.textContent=String(e.index+1),t.appendChild(i);const n=z("div");if(n.className="collapse-column",s?.collapseButton){const t=z("div");t.className="collapse-button"+(s.collapseButton.isCollapsed?" collapsed":""),t.textContent=s.collapseButton.isCollapsed?"›":"⌄",t.dataset.line=String(e.index),t.dataset.nodeId=s.collapseButton.nodeId,t.title=s.collapseButton.isCollapsed?"Expand":"Collapse",n.appendChild(t)}t.appendChild(n),o.appendChild(t)}s.innerHTML="",s.appendChild(o)}syncGutterScroll(){this._gutterScroll&&this._viewport&&(this._gutterScroll.scrollTop=this._viewport.scrollTop)}handleInput(){const e=this._hiddenTextarea,t=e?.value;if(t)if(this._hasSelection()&&this._deleteSelection(),this._getCollapsedRangeForLine(this.cursorLine))e.value="";else{if(this._getCollapsedClosingLine(this.cursorLine)){const t=this.lines[this.cursorLine],s=this._getClosingBracketPos(t);if(this.cursorColumn<=s)return void(e.value="")}if(this._getCollapsedNodeAtLine(this.cursorLine)){const t=this.lines[this.cursorLine].search(R);if(this.cursorColumn>t)return void(e.value="")}if(this.cursorLine<this.lines.length){const e=this.lines[this.cursorLine],s=e.substring(0,this.cursorColumn),i=t.split("\n");if(1===i.length){if(this._insertMode){const i=e.substring(this.cursorColumn);this.lines[this.cursorLine]=s+t+i}else{const i=e.substring(this.cursorColumn+t.length);this.lines[this.cursorLine]=s+t+i}this.cursorColumn+=t.length}else{const t=e.substring(this.cursorColumn);this.lines[this.cursorLine]=s+i[0];for(let e=1;e<i.length-1;e++)this.lines.splice(this.cursorLine+e,0,i[e]);const n=i[i.length-1]+t;this.lines.splice(this.cursorLine+i.length-1,0,n),this.cursorLine+=i.length-1,this.cursorColumn=i[i.length-1].length}}else{const e=t.split("\n");this.lines.push(...e),this.cursorLine=this.lines.length-1,this.cursorColumn=this.lines[this.cursorLine].length}e.value="",clearTimeout(this.inputTimer),this.inputTimer=setTimeout(()=>{this.formatAndUpdate()},150)}}handleKeydown(e){const t={inCollapsedZone:this._getCollapsedRangeForLine(this.cursorLine),onCollapsedNode:this._getCollapsedNodeAtLine(this.cursorLine),onClosingLine:this._getCollapsedClosingLine(this.cursorLine)},s={Enter:()=>this._handleEnter(e.shiftKey,t),Backspace:()=>this._handleBackspace(t),Delete:()=>this._handleDelete(t),ArrowUp:()=>this._handleArrowKey(-1,0,e.shiftKey,e.ctrlKey||e.metaKey),ArrowDown:()=>this._handleArrowKey(1,0,e.shiftKey,e.ctrlKey||e.metaKey),ArrowLeft:()=>this._handleArrowKey(0,-1,e.shiftKey,e.ctrlKey||e.metaKey),ArrowRight:()=>this._handleArrowKey(0,1,e.shiftKey,e.ctrlKey||e.metaKey),Home:()=>this._handleHomeEnd("home",e.shiftKey,t.onClosingLine),End:()=>this._handleHomeEnd("end",e.shiftKey,t.onClosingLine),PageUp:()=>this._handlePageUpDown("up",e.shiftKey),PageDown:()=>this._handlePageUpDown("down",e.shiftKey),Tab:()=>this._handleTab(e.shiftKey,t),Insert:()=>{this._insertMode=!this._insertMode,this.scheduleRender()}},i={a:()=>this._selectAll(),z:()=>e.shiftKey?this.redo():this.undo(),y:()=>this.redo(),s:()=>this.save(),o:()=>!this.hasAttribute("readonly")&&this.open()};if(s[e.key])return e.preventDefault(),void s[e.key]();(e.ctrlKey||e.metaKey)&&i[e.key]&&(e.preventDefault(),i[e.key]())}_handleEnter(e,t){if(e){const e=this._getContainingExpandedNode(this.cursorLine);if(e){const t=this.lines[e.startLine],s=t.search(R);this.toggleCollapse(e.nodeId),this.cursorLine=e.startLine,this.cursorColumn=s>=0?s+1:t.length,this._clearSelection(),this._scrollToCursor()}return}if(t.onCollapsedNode)this.toggleCollapse(t.onCollapsedNode.nodeId);else{if(t.onClosingLine){const e=this.lines[this.cursorLine],s=this._getClosingBracketPos(e);if(s>=0&&this.cursorColumn<=s)return void this.toggleCollapse(t.onClosingLine.nodeId)}t.inCollapsedZone}}_handleBackspace(e){if(this._hasSelection())return this._deleteSelection(),void this.formatAndUpdate();if(e.onClosingLine){const t=this.lines[this.cursorLine],s=this._getClosingBracketPos(t);return s>=0&&this.cursorColumn>s+1?void this.deleteBackward():void this._deleteCollapsedNode(e.onClosingLine)}if(e.onCollapsedNode&&0===this.cursorColumn)this._deleteCollapsedNode(e.onCollapsedNode);else if(!e.inCollapsedZone){if(e.onCollapsedNode){const t=this.lines[this.cursorLine].search(R);if(this.cursorColumn>t+1)return void this._deleteCollapsedNode(e.onCollapsedNode)}this.deleteBackward()}}_handleDelete(e){if(this._hasSelection())return this._deleteSelection(),void this.formatAndUpdate();if(e.onClosingLine){const t=this.lines[this.cursorLine],s=this._getClosingBracketPos(t);return s>=0&&this.cursorColumn>s?void this.deleteForward():void this._deleteCollapsedNode(e.onClosingLine)}if(e.onCollapsedNode){const t=this.lines[this.cursorLine].search(R);if(this.cursorColumn>t)return void this._deleteCollapsedNode(e.onCollapsedNode)}e.inCollapsedZone||this.deleteForward()}_handleTab(e,t){e?this._navigateToPrevAttribute():this._navigateToNextAttribute()}_navigateToNextAttribute(){const e=this.visibleLines.length;let t=this.visibleLines.findIndex(e=>e.index===this.cursorLine);t<0&&(t=0);for(let s=t;s<e;s++){const e=this.visibleLines[s],i=this.lines[e.index],n=s===t?this.cursorColumn:0,r=this._findNextAttributeOrBracket(i,n,e.index);if(null!==r)return this.cursorLine=e.index,this.cursorColumn=r.start,r.isBracket?this._clearSelection():(this.selectionStart={line:e.index,column:r.start},this.selectionEnd={line:e.index,column:r.end}),this._scrollToCursor(),this._invalidateRenderCache(),void this.scheduleRender()}for(let s=0;s<t;s++){const e=this.visibleLines[s],t=this.lines[e.index],i=this._findNextAttributeOrBracket(t,0,e.index);if(null!==i)return this.cursorLine=e.index,this.cursorColumn=i.start,i.isBracket?this._clearSelection():(this.selectionStart={line:e.index,column:i.start},this.selectionEnd={line:e.index,column:i.end}),this._scrollToCursor(),this._invalidateRenderCache(),void this.scheduleRender()}}_navigateToPrevAttribute(){const e=this.visibleLines.length;let t=this.visibleLines.findIndex(e=>e.index===this.cursorLine);t<0&&(t=e-1);for(let s=t;s>=0;s--){const e=this.visibleLines[s],i=this.lines[e.index],n=s===t?this.cursorColumn:i.length,r=this._findPrevAttributeOrBracket(i,n,e.index);if(null!==r)return this.cursorLine=e.index,this.cursorColumn=r.start,r.isBracket?this._clearSelection():(this.selectionStart={line:e.index,column:r.start},this.selectionEnd={line:e.index,column:r.end}),this._scrollToCursor(),this._invalidateRenderCache(),void this.scheduleRender()}for(let s=e-1;s>t;s--){const e=this.visibleLines[s],t=this.lines[e.index],i=this._findPrevAttributeOrBracket(t,t.length,e.index);if(null!==i)return this.cursorLine=e.index,this.cursorColumn=i.start,i.isBracket?this._clearSelection():(this.selectionStart={line:e.index,column:i.start},this.selectionEnd={line:e.index,column:i.end}),this._scrollToCursor(),this._invalidateRenderCache(),void this.scheduleRender()}}_findNextAttributeInLine(e,t){const s=[],i=/"([^"]+)"(?:\s*:\s*(?:"([^"]*)"|(-?\d+\.?\d*(?:e[+-]?\d+)?)|true|false|null))?/gi;let n;for(;null!==(n=i.exec(e));){const t=n.index+1,i=t+n[1].length;if(s.push({start:t,end:i}),void 0!==n[2]){const t=e.substring(n.index).match(/:\s*"([^"]*)"/);if(t){const e=n.index+(t.index||0)+t[0].indexOf('"')+1,i=e+n[2].length;s.push({start:e,end:i})}}else if(void 0!==n[3]){const t=e.substring(n.index).match(/:\s*(-?\d+\.?\d*(?:e[+-]?\d+)?)/i);if(t){const e=n.index+(t.index||0)+t[0].indexOf(t[1]),i=e+t[1].length;s.push({start:e,end:i})}}else{const t=e.substring(n.index).match(/:\s*(true|false|null)/);if(t){const e=n.index+(t.index||0)+t[0].indexOf(t[1]),i=e+t[1].length;s.push({start:e,end:i})}}}const r=/(?:^|[\[,\s])(-?\d+\.?\d*(?:e[+-]?\d+)?)\s*(?:[,\]]|$)/gi;for(;null!==(n=r.exec(e));){const e=n[1],t=n.index+n[0].indexOf(e),i=t+e.length;s.some(e=>e.start===t&&e.end===i)||s.push({start:t,end:i})}s.sort((e,t)=>e.start-t.start);for(const o of s)if(o.start>t)return o;return null}_findPrevAttributeInLine(e,t){const s=[],i=/"([^"]+)"(?:\s*:\s*(?:"([^"]*)"|(-?\d+\.?\d*(?:e[+-]?\d+)?)|true|false|null))?/gi;let n;for(;null!==(n=i.exec(e));){const t=n.index+1,i=t+n[1].length;if(s.push({start:t,end:i}),void 0!==n[2]){const t=e.substring(n.index).match(/:\s*"([^"]*)"/);if(t){const e=n.index+(t.index||0)+t[0].indexOf('"')+1,i=e+n[2].length;s.push({start:e,end:i})}}else if(void 0!==n[3]){const t=e.substring(n.index).match(/:\s*(-?\d+\.?\d*(?:e[+-]?\d+)?)/i);if(t){const e=n.index+(t.index||0)+t[0].indexOf(t[1]),i=e+t[1].length;s.push({start:e,end:i})}}else{const t=e.substring(n.index).match(/:\s*(true|false|null)/);if(t){const e=n.index+(t.index||0)+t[0].indexOf(t[1]),i=e+t[1].length;s.push({start:e,end:i})}}}const r=/(?:^|[\[,\s])(-?\d+\.?\d*(?:e[+-]?\d+)?)\s*(?:[,\]]|$)/gi;for(;null!==(n=r.exec(e));){const e=n[1],t=n.index+n[0].indexOf(e),i=t+e.length;s.some(e=>e.start===t&&e.end===i)||s.push({start:t,end:i})}s.sort((e,t)=>e.start-t.start);for(let o=s.length-1;o>=0;o--)if(s[o].end<t)return s[o];return null}_findBracketInLine(e){const t=e.match(/[\[{]\s*$/);return t&&void 0!==t.index?t.index+1:null}_findNextAttributeOrBracket(e,t,s){const i=this._findNextAttributeInLine(e,t),n=this._findBracketInLine(e);return null!==i&&null!==n?n>t&&n<i.start?{start:n,end:n,isBracket:!0}:{...i,isBracket:!1}:null!==i?{...i,isBracket:!1}:null!==n&&n>t?{start:n,end:n,isBracket:!0}:null}_findPrevAttributeOrBracket(e,t,s){const i=this._findPrevAttributeInLine(e,t),n=this._findBracketInLine(e);return null!==i&&null!==n?n<t&&n>i.end?{start:n,end:n,isBracket:!0}:{...i,isBracket:!1}:null!==i?{...i,isBracket:!1}:null!==n&&n<t?{start:n,end:n,isBracket:!0}:null}insertNewline(){if(this._saveToHistory("newline"),this.cursorLine<this.lines.length){const e=this.lines[this.cursorLine],t=e.substring(0,this.cursorColumn),s=e.substring(this.cursorColumn);this.lines[this.cursorLine]=t,this.lines.splice(this.cursorLine+1,0,s),this.cursorLine++,this.cursorColumn=0}else this.lines.push(""),this.cursorLine=this.lines.length-1,this.cursorColumn=0;this.formatAndUpdate()}deleteBackward(){if(this._saveToHistory("delete"),this.cursorColumn>0){const e=this.lines[this.cursorLine];this.lines[this.cursorLine]=e.substring(0,this.cursorColumn-1)+e.substring(this.cursorColumn),this.cursorColumn--}else if(this.cursorLine>0){const e=this.lines[this.cursorLine],t=this.lines[this.cursorLine-1];this.cursorColumn=t.length,this.lines[this.cursorLine-1]=t+e,this.lines.splice(this.cursorLine,1),this.cursorLine--}this.formatAndUpdate()}deleteForward(){if(this._saveToHistory("delete"),this.cursorLine<this.lines.length){const e=this.lines[this.cursorLine];this.cursorColumn<e.length?this.lines[this.cursorLine]=e.substring(0,this.cursorColumn)+e.substring(this.cursorColumn+1):this.cursorLine<this.lines.length-1&&(this.lines[this.cursorLine]=e+this.lines[this.cursorLine+1],this.lines.splice(this.cursorLine+1,1))}this.formatAndUpdate()}moveCursorSkipCollapsed(e){let t=this.cursorLine+e;for(;t>=0&&t<this.lines.length;){const s=this._getCollapsedRangeForLine(t);if(!s)break;t=e>0?s.endLine:s.startLine}this.cursorLine=Math.max(0,Math.min(this.lines.length-1,t));const s=this.lines[this.cursorLine]?.length||0;this.cursorColumn=Math.min(this.cursorColumn,s),this._invalidateRenderCache(),this._scrollToCursor(),this.scheduleRender()}moveCursorHorizontal(e){e>0?this._moveCursorRight():this._moveCursorLeft(),this._invalidateRenderCache(),this._scrollToCursor(),this.scheduleRender()}_moveCursorRight(){const e=this.lines[this.cursorLine],t=this._getCollapsedNodeAtLine(this.cursorLine);if(this._getCollapsedClosingLine(this.cursorLine)){const t=this._getClosingBracketPos(e);this.cursorColumn<t?this.cursorColumn=t:this.cursorColumn>=e.length?this.cursorLine<this.lines.length-1&&(this.cursorLine++,this.cursorColumn=0):this.cursorColumn++}else if(t){const s=e.search(R);this.cursorColumn<s?this.cursorColumn++:this.cursorColumn===s?this.cursorColumn=s+1:(this.cursorLine=t.endLine,this.cursorColumn=this._getClosingBracketPos(this.lines[this.cursorLine]))}else if(this.cursorColumn>=e.length){if(this.cursorLine<this.lines.length-1){this.cursorLine++,this.cursorColumn=0;const e=this._getCollapsedRangeForLine(this.cursorLine);e&&(this.cursorLine=e.endLine,this.cursorColumn=0)}}else this.cursorColumn++}_moveCursorLeft(){const e=this.lines[this.cursorLine],t=this._getCollapsedNodeAtLine(this.cursorLine),s=this._getCollapsedClosingLine(this.cursorLine);if(s){const t=this._getClosingBracketPos(e);if(this.cursorColumn>t+1)this.cursorColumn--;else{this.cursorLine=s.startLine;const e=this.lines[this.cursorLine];this.cursorColumn=e.search(R)+1}}else if(t){const t=e.search(R);this.cursorColumn>t+1?this.cursorColumn=t+1:this.cursorColumn===t+1?this.cursorColumn=t:this.cursorColumn>0?this.cursorColumn--:this.cursorLine>0&&(this.cursorLine--,this.cursorColumn=this.lines[this.cursorLine]?.length||0)}else if(this.cursorColumn>0)this.cursorColumn--;else if(this.cursorLine>0)if(this.cursorLine--,this._getCollapsedClosingLine(this.cursorLine))this.cursorColumn=this.lines[this.cursorLine]?.length||0;else{const e=this._getCollapsedRangeForLine(this.cursorLine);if(e){this.cursorLine=e.startLine;const t=this.lines[this.cursorLine];this.cursorColumn=t.search(R)+1}else this.cursorColumn=this.lines[this.cursorLine]?.length||0}}_scrollToCursor(e=!1){const t=this._viewport;if(!t)return;const s=this.visibleLines.findIndex(e=>e.index===this.cursorLine);if(-1===s)return;const i=s*this.lineHeight,n=t.clientHeight;if(e)t.scrollTop=Math.max(0,i-n/2+this.lineHeight/2);else{const e=t.scrollTop,s=e+n;i<e?t.scrollTop=i:i+this.lineHeight>s&&(t.scrollTop=i+this.lineHeight-n)}}_handleArrowKey(e,t,s,i=!1){s&&!this.selectionStart&&(this.selectionStart={line:this.cursorLine,column:this.cursorColumn}),0!==e?this.moveCursorSkipCollapsed(e):0!==t&&(i?this._moveCursorByWord(t):this.moveCursorHorizontal(t)),s?this.selectionEnd={line:this.cursorLine,column:this.cursorColumn}:(this.selectionStart=null,this.selectionEnd=null)}_moveCursorByWord(e){const t=this.lines[this.cursorLine]||"",s=e=>N.test(e),i=this._getCollapsedNodeAtLine(this.cursorLine);if(e>0){let e=this.cursorColumn;if(i){const s=t.search(R);if(s>=0&&e>=s)return this.cursorLine=i.endLine,this.cursorColumn=(this.lines[this.cursorLine]||"").length,this._invalidateRenderCache(),this._scrollToCursor(),void this.scheduleRender()}if(e>=t.length){if(this.cursorLine<this.lines.length-1){let e=this.cursorLine+1;const t=this._getCollapsedRangeForLine(e);t&&(e=t.endLine),this.cursorLine=Math.min(e,this.lines.length-1),this.cursorColumn=0}}else if(s(t[e])){for(;e<t.length&&s(t[e]);)e++;this.cursorColumn=e}else{for(;e<t.length&&!s(t[e]);)e++;this.cursorColumn=e}}else{let e=this.cursorColumn;const i=this._getCollapsedClosingLine(this.cursorLine);if(i){const s=this._getClosingBracketPos(t);if(s>=0&&e<=s+1){this.cursorLine=i.startLine;const e=(this.lines[this.cursorLine]||"").search(R);return this.cursorColumn=e>=0?e:0,this._invalidateRenderCache(),this._scrollToCursor(),void this.scheduleRender()}}if(0===e){if(this.cursorLine>0){let e=this.cursorLine-1;const t=this._getCollapsedRangeForLine(e);t&&(e=t.startLine),this.cursorLine=Math.max(e,0),this.cursorColumn=this.lines[this.cursorLine].length}}else if(e>0&&s(t[e-1])){for(;e>0&&s(t[e-1]);)e--;this.cursorColumn=e}else{for(;e>0&&!s(t[e-1]);)e--;for(;e>0&&s(t[e-1]);)e--;this.cursorColumn=e}}this._invalidateRenderCache(),this._scrollToCursor(),this.scheduleRender()}_handleHomeEnd(e,t,s){if(t&&!this.selectionStart&&(this.selectionStart={line:this.cursorLine,column:this.cursorColumn}),"home"===e)s?(this.cursorLine=s.startLine,this.cursorColumn=0):0===this.cursorColumn?(this.cursorLine=0,this.cursorColumn=0):this.cursorColumn=0;else{const e=this.lines[this.cursorLine]?.length||0;this.cursorColumn===e?(this.cursorLine=this.lines.length-1,this.cursorColumn=this.lines[this.cursorLine]?.length||0):this.cursorColumn=e}t?this.selectionEnd={line:this.cursorLine,column:this.cursorColumn}:(this.selectionStart=null,this.selectionEnd=null),this._invalidateRenderCache(),this._scrollToCursor(),this.scheduleRender()}_handlePageUpDown(e,t){t&&!this.selectionStart&&(this.selectionStart={line:this.cursorLine,column:this.cursorColumn});const s=this._viewport;if(!s)return;const i=Math.floor(s.clientHeight/this.lineHeight);if("up"===e){const e=this.visibleLines.findIndex(e=>e.index===this.cursorLine),t=Math.max(0,e-i);this.cursorLine=this.visibleLines[t]?.index||0}else{const e=this.visibleLines.findIndex(e=>e.index===this.cursorLine),t=Math.min(this.visibleLines.length-1,e+i);this.cursorLine=this.visibleLines[t]?.index||this.lines.length-1}this.cursorColumn=Math.min(this.cursorColumn,this.lines[this.cursorLine]?.length||0),t?this.selectionEnd={line:this.cursorLine,column:this.cursorColumn}:(this.selectionStart=null,this.selectionEnd=null),this._invalidateRenderCache(),this._scrollToCursor(),this.scheduleRender()}_selectAll(){this.selectionStart={line:0,column:0};const e=this.lines.length-1;this.selectionEnd={line:e,column:this.lines[e]?.length||0},this.cursorLine=e,this.cursorColumn=this.lines[e]?.length||0,this._invalidateRenderCache(),this.scheduleRender()}_getSelectedText(){const e=this._normalizeSelection();if(!e)return"";const{start:t,end:s}=e;if(t.line===s.line)return this.lines[t.line].substring(t.column,s.column);let i=this.lines[t.line].substring(t.column)+"\n";for(let n=t.line+1;n<s.line;n++)i+=this.lines[n]+"\n";return i+=this.lines[s.line].substring(0,s.column),i}_normalizeSelection(){if(!this.selectionStart||!this.selectionEnd)return null;const e=this.selectionStart,t=this.selectionEnd;return e.line<t.line||e.line===t.line&&e.column<=t.column?{start:e,end:t}:{start:t,end:e}}_hasSelection(){return!(!this.selectionStart||!this.selectionEnd||this.selectionStart.line===this.selectionEnd.line&&this.selectionStart.column===this.selectionEnd.column)}_clearSelection(){this.selectionStart=null,this.selectionEnd=null}_selectWordAt(e,t){if(e<0||e>=this.lines.length)return;const s=this.lines[e];if(!s||t>s.length)return;const i=e=>/[\w\-]/.test(e);let n=!1,r=-1,o=-1,l=!1;for(let c=0;c<s.length;c++){const i=s[c];if(l)l=!1;else if("\\"!==i){if('"'===i)if(n){if(o=c,t>=r&&t<=o)return this.selectionStart={line:e,column:r+1},this.selectionEnd={line:e,column:o},this.cursorLine=e,void(this.cursorColumn=o);n=!1,r=-1,o=-1}else n=!0,r=c}else l=!0}let a=t,h=t;for(;a>0&&i(s[a-1]);)a--;for(;h<s.length&&i(s[h]);)h++;a<h&&(this.selectionStart={line:e,column:a},this.selectionEnd={line:e,column:h},this.cursorLine=e,this.cursorColumn=h)}_deleteSelection(){const e=this._normalizeSelection();if(!e)return!1;const{start:t,end:s}=e;if(this._saveToHistory("delete"),t.line===s.line){const e=this.lines[t.line];this.lines[t.line]=e.substring(0,t.column)+e.substring(s.column)}else{const e=this.lines[t.line].substring(0,t.column),i=this.lines[s.line].substring(s.column);this.lines[t.line]=e+i,this.lines.splice(t.line+1,s.line-t.line)}return this.cursorLine=t.line,this.cursorColumn=t.column,this.selectionStart=null,this.selectionEnd=null,!0}insertText(e){if(this._hasSelection()&&this._deleteSelection(),!this._getCollapsedRangeForLine(this.cursorLine)){if(this._getCollapsedClosingLine(this.cursorLine)){const e=this.lines[this.cursorLine],t=this._getClosingBracketPos(e);if(this.cursorColumn<=t)return}if(this._getCollapsedNodeAtLine(this.cursorLine)){const e=this.lines[this.cursorLine].search(R);if(this.cursorColumn>e)return}if(this._saveToHistory("insert"),0===this.lines.length){const t=e.split("\n");this.lines=t,this.cursorLine=t.length-1,this.cursorColumn=t[t.length-1].length}else if(this.cursorLine<this.lines.length){const t=this.lines[this.cursorLine];this.lines[this.cursorLine]=t.substring(0,this.cursorColumn)+e+t.substring(this.cursorColumn),this.cursorColumn+=e.length}this.formatAndUpdate()}}handlePaste(e){e.preventDefault();const t=e.clipboardData?.getData("text/plain");if(!t)return;const s=0===this.lines.length;try{const e=H(JSON.parse(t)).map(e=>JSON.stringify(e,null,2)).join(",\n");this.insertText(e)}catch{this.insertText(t)}this.renderTimer&&(cancelAnimationFrame(this.renderTimer),this.renderTimer=void 0),s&&this.lines.length>0&&this.autoCollapseCoordinates(),this._expandErrorNodes(),this.renderViewport()}handleCopy(e){e.preventDefault(),e.clipboardData&&(this._hasSelection()?e.clipboardData.setData("text/plain",this._getSelectedText()):e.clipboardData.setData("text/plain",this.getContent()))}handleCut(e){e.preventDefault(),e.clipboardData&&(this._hasSelection()?(e.clipboardData.setData("text/plain",this._getSelectedText()),this._saveToHistory("cut"),this._deleteSelection(),this.formatAndUpdate()):(e.clipboardData.setData("text/plain",this.getContent()),this._saveToHistory("cut"),this.lines=[],this.cursorLine=0,this.cursorColumn=0,this.formatAndUpdate()))}_getPositionFromClick(e){const t=this._viewport,s=this._linesContainer;if(!t)return{line:0,column:0};const i=t.getBoundingClientRect(),n=e.clientY-i.top+t.scrollTop-8,r=Math.floor(n/this.lineHeight);let o=0,l=0;if(r>=0&&r<this.visibleLines.length){const n=this.visibleLines[r];o=n.index;const a=s?.querySelector(`.line[data-line-index="${n.index}"]`),h=this._getCharWidth();if(a){const t=a.getBoundingClientRect(),s=e.clientX-t.left,i=Math.round(s/h),r=n.content?.length||0;l=Math.max(0,Math.min(i,r))}else{const s=12,r=e.clientX-i.left+t.scrollLeft-s,o=Math.round(r/h),a=n.content?.length||0;l=Math.max(0,Math.min(o,a))}}return{line:o,column:l}}handleGutterClick(e){const t=e.target;if(!t)return;const s=t.closest(".visibility-button");if(s)this.toggleFeatureVisibility(s.dataset.featureKey);else if(t.classList.contains("collapse-button")){const e=t.dataset.nodeId;return void(e&&this.toggleCollapse(e))}}handleEditorClick(e){const t=e.target;if(!t)return;this._blockRender=!1;const s=t.closest(".line.has-visibility");if(s){const t=s.getBoundingClientRect();if(e.clientX-t.left<14){e.preventDefault(),e.stopPropagation();const t=s.dataset.featureKey;return void(t&&this.toggleFeatureVisibility(t))}}if(t.classList.contains("json-color")){const s=t.getBoundingClientRect(),i=e.clientX-s.left;if(i<0&&i>=-8){e.preventDefault(),e.stopPropagation();const s=t.dataset.color,i=t.closest(".line");if(i){const e=parseInt(i.dataset.lineIndex||"0"),n=this.lines[e].match(k);n&&n[1]&&s&&this.showColorPicker(t,e,s,n[1])}return}}if(t.classList.contains("json-boolean")){const s=t.getBoundingClientRect(),i=e.clientX-s.left;if(i<0&&i>=-8){e.preventDefault(),e.stopPropagation();const s=t.closest(".line");if(s){const e=parseInt(s.dataset.lineIndex||"0"),t=this.lines[e].match(S);if(t){const s="true"===t[2];this.updateBooleanValue(e,!s,t[1])}}return}}}toggleCollapse(e){const t=this._nodeIdToLines.get(e);this.collapsedNodes.has(e)?(this.collapsedNodes.delete(e),t?.uniqueKey&&this._openedNodeKeys.add(t.uniqueKey)):(this.collapsedNodes.add(e),t?.uniqueKey&&this._openedNodeKeys.delete(t.uniqueKey)),this.updateView(),this._invalidateRenderCache(),this.scheduleRender()}autoCollapseCoordinates(){this._hasErrors()||this._applyCollapsedOption(["coordinates"])}_hasErrors(){try{const e="["+this.lines.join("\n")+"]";JSON.parse(e)}catch{return!0}for(const e of this.lines)if(V(e,"",void 0).includes("json-error"))return!0;return!1}_applyCollapsedFromOptions(e,t){if(this._hasErrors())return;const s=void 0!==e.collapsed?e.collapsed:["coordinates"];s&&(!Array.isArray(s)||s.length>0)&&this._applyCollapsedOption(s,t)}_applyCollapsedOption(e,t=null){const s=this._findCollapsibleRanges(),i=s.filter(e=>e.isRootFeature);for(const n of s){let s=!1;if("function"==typeof e){const r=i.findIndex(e=>n.startLine>=e.startLine&&n.endLine<=e.endLine),o=e(t?.[r]||null,r);s=n.isRootFeature?o.includes("$root"):o.includes(n.nodeKey)}else Array.isArray(e)&&(s=n.isRootFeature?e.includes("$root"):e.includes(n.nodeKey));s&&this.collapsedNodes.add(n.nodeId)}this.updateModel(),this.scheduleRender()}toggleFeatureVisibility(e){e&&(this.hiddenFeatures.has(e)?this.hiddenFeatures.delete(e):this.hiddenFeatures.add(e),this.updateView(),this.scheduleRender(),this.emitChange())}showColorPicker(e,t,s,i){const n=document.querySelector(".geojson-color-picker-anchor");n&&n.remove();const r=z("div");r.className="geojson-color-picker-anchor";const o=e.getBoundingClientRect();r.style.cssText=`\n position: fixed;\n left: ${o.left-8}px;\n top: ${o.top+o.height}px;\n width: 10px;\n height: 10px;\n z-index: 9998;\n `,document.body.appendChild(r);const l=z("input");l.type="color";let a=s;a=s.startsWith("#")?s.replace(L,"#$1$1$2$2$3$3"):function(e){const t=(D||(D=document.createElement("div"),D.style.display="none",document.body.appendChild(D)),D);t.style.color=e;const s=getComputedStyle(t).color;if(!s)return null;const i=s.match(/rgba?\((\d+),\s*(\d+),\s*(\d+)/);if(!i)return null;const[,n,r,o]=i;return"#"+parseInt(n,10).toString(16).padStart(2,"0")+parseInt(r,10).toString(16).padStart(2,"0")+parseInt(o,10).toString(16).padStart(2,"0")}(s)||"#000000",l.value=a,l.className="geojson-color-picker-input",l.style.cssText="\n position: absolute;\n left: 0;\n top: 0;\n width: 10px;\n height: 10px;\n opacity: 0;\n border: none;\n padding: 0;\n cursor: pointer;\n ",r.appendChild(l),l.addEventListener("input",e=>{this.updateColorValue(t,e.target.value,i)});const h=e=>{e.target!==l&&(document.removeEventListener("click",h,!0),r.remove())};l._closeListener=h,setTimeout(()=>{document.addEventListener("click",h,!0)},100),l.focus(),l.click()}updateColorValue(e,t,s){const i=new RegExp(`"${s}"\\s*:\\s*"(?:#[0-9a-fA-F]{3,6}|[a-zA-Z]+)"`);this.lines[e]=this.lines[e].replace(i,`"${s}": "${t}"`),this.updateView(),this.scheduleRender(),this.emitChange()}updateBooleanValue(e,t,s){const i=new RegExp(`"${s}"\\s*:\\s*(true|false)`);this.lines[e]=this.lines[e].replace(i,`"${s}": ${t}`),this.updateView(),this.scheduleRender(),this.emitChange()}_bestEffortFormat(e,t){const s=e.split("\n");if(void 0!==t&&t>=0&&t<s.length){const e=s[t],i=s.slice(0,t).join("\n"),n=i.trim()?this._formatChunk(i):[],r=this._computeDepthAtEnd(n)+this._computeBracketDelta(e),o=s.slice(t+1).join("\n"),l=o.trim()?this._formatChunk(o,r):[];return[...n,e,...l]}return this._formatChunk(e)}_computeBracketDelta(e){let t=0,s=!1,i=!1;for(const n of e)i?i=!1:"\\"===n&&s?i=!0:'"'!==n?s||("{"===n||"["===n?t++:"}"!==n&&"]"!==n||t--):s=!s;return t}_computeDepthAtEnd(e){let t=1;for(const s of e)for(const e of s)"{"===e||"["===e?t++:"}"!==e&&"]"!==e||(t=Math.max(0,t-1));return t}_formatChunk(e,t=1){const s=[];let i="",n=t,r=!1,o=!1;for(let l=0;l<e.length;l++){const t=e[l];if(o)i+=t,o=!1;else if("\\"===t&&r)i+=t,o=!0;else if('"'!==t)if(r)i+=t;else if("{"===t||"["===t)i+=t,s.push(" ".repeat(n)+i.trim()),n++,i="";else if("}"===t||"]"===t)i.trim()&&s.push(" ".repeat(n)+i.trim()),n=Math.max(0,n-1),i=t;else if(","===t)i+=t,s.push(" ".repeat(n)+i.trim()),i="";else if(":"===t){if(i+=": ",l++," "===e[l])continue;l--}else{if("\n"===t||"\r"===t)continue;i+=t}else r=!r,i+=t}return i.trim()&&s.push(" ".repeat(n)+i.trim()),s}formatAndUpdate(){const e=this.cursorLine,t=this.cursorColumn,s=this.lines.join("\n");try{const e="["+s+"]",t=JSON.parse(e),i=JSON.stringify(t,null,2).split("\n");this.lines=i.slice(1,-1)}catch{if(s.trim()){const t=(this.lines[e]||"").length<80?e:void 0;this.lines=this._bestEffortFormat(s,t)}}if(this.lines.join("\n")===s)this.cursorLine=e,this.cursorColumn=t;else if(0===this.cursorColumn);else{const i=s.split("\n")[e]||"",n=i.length-i.trimStart().length,r=Math.max(0,t-n);if(this.cursorLine<this.lines.length){const e=this.lines[this.cursorLine],t=e.length-e.trimStart().length;this.cursorColumn=t+r}}this.cursorLine=Math.min(this.cursorLine,Math.max(0,this.lines.length-1)),this.cursorColumn=Math.min(this.cursorColumn,this.lines[this.cursorLine]?.length||0),this.updateModel(),this._expandErrorNodes(),this.scheduleRender(),this.updatePlaceholderVisibility(),this._updateErrorDisplay(),this.emitChange()}emitChange(){const e=this.getContent(),s=this.prefix+e+this.suffix;try{let i=JSON.parse(s);this.hiddenFeatures.size>0&&(i.features=i.features.filter(e=>{const t=M(e);return!t||!this.hiddenFeatures.has(t)}));const n=function(e){const s=[];return e.features?(e.features.forEach((e,i)=>{"Feature"!==e.type&&s.push(`features[${i}]: type must be "Feature"`),e.geometry&&e.geometry.type&&(t.includes(e.geometry.type)||s.push(`features[${i}].geometry: invalid type "${e.geometry.type}"`))}),s):s}(i);n.length>0?this.dispatchEvent(new CustomEvent("error",{detail:{error:n.join("; "),errors:n,content:e},bubbles:!0,composed:!0})):this.dispatchEvent(new CustomEvent("change",{detail:i,bubbles:!0,composed:!0}))}catch(i){this.dispatchEvent(new CustomEvent("error",{detail:{error:i instanceof Error?i.message:"Unknown error",content:e},bubbles:!0,composed:!0}))}}updateReadonly(){this._hiddenTextarea&&(this._hiddenTextarea.readOnly=this.readonly),this._clearBtn&&(this._clearBtn.hidden=this.readonly)}updatePlaceholderVisibility(){this._placeholderLayer&&(this._placeholderLayer.style.display=this.lines.length>0?"none":"block")}_updateErrorDisplay(){const e=this._getErrorLines().length;this._errorNav&&this._errorNav.classList.toggle("visible",e>0),this._errorCount&&(this._errorCount.textContent=e>0?String(e):"")}updatePlaceholderContent(){this._placeholderLayer&&(this._placeholderLayer.textContent=this.placeholder),this.updatePlaceholderVisibility()}updatePrefixSuffix(){this._editorPrefix&&(this._editorPrefix.textContent=this.prefix),this._editorSuffix&&(this._editorSuffix.textContent=this.suffix)}updateThemeCSS(){const e=(t=this.getAttribute("dark-selector")||".dark").startsWith(".")&&!t.includes(" ")?`:host(${t})`:`:host-context(${t})`;var t;let s=this._id("theme-styles");s||(s=z("style"),s.id="theme-styles",this.shadowRoot.insertBefore(s,this.shadowRoot.firstChild)),I.lastIndex=0;const i=e=>Object.entries(e).filter(e=>void 0!==e[1]).map(([e,t])=>{return`--${s=e,s.replace(I,"-$1").toLowerCase()}: ${t};`;var s}).join("\n "),n=i(this.themes.light||{});let r=n?`:host {\n ${n}\n }\n`:"";r+=`${e} {\n ${i({bgColor:"#2b2b2b",textColor:"#a9b7c6",caretColor:"#bbb",gutterBg:"#313335",gutterBorder:"#3c3f41",gutterText:"#606366",jsonKey:"#9876aa",jsonString:"#6a8759",jsonNumber:"#6897bb",jsonBoolean:"#cc7832",jsonNull:"#cc7832",jsonPunct:"#a9b7c6",jsonError:"#ff6b68",controlColor:"#cc7832",controlBg:"#3c3f41",controlBorder:"#5a5a5a",geojsonKey:"#9876aa",geojsonType:"#6a8759",geojsonTypeInvalid:"#ff6b68",jsonKeyInvalid:"#ff6b68",...this.themes.dark})}\n }`,s.textContent=r}setTheme(e){e.dark&&(this.themes.dark={...this.themes.dark,...e.dark}),e.light&&(this.themes.light={...this.themes.light,...e.light}),this.updateThemeCSS()}resetTheme(){this.themes={dark:{},light:{}},this.updateThemeCSS()}getTheme(){return{...this.themes}}_findCollapsibleRanges(){const e=[];for(const[t,s]of this._lineToNodeId){const i=this._nodeIdToLines.get(s);if(!i)continue;const n=this.lines[t];if(!n)continue;const r=n.match(E),o=!r&&n.match(j);if(!r&&!o)continue;const l=r?r[2]:o?o[1]:"{";e.push({startLine:i.startLine,endLine:i.endLine,nodeKey:i.nodeKey||(r?r[1]:`__root_${t}`),nodeId:s,openBracket:l,isRootFeature:!!o})}return e.sort((e,t)=>e.startLine-t.startLine),e}_findClosingLine(e,t){let s=1;const i=this.lines[e],n=i.indexOf(t);if(-1!==n){const r=K(i.substring(n+1),t);if(s+=r.open-r.close,0===s)return e}for(let r=e+1;r<this.lines.length;r++){const e=K(this.lines[r],t);if(s+=e.open-e.close,0===s)return r}return-1}_buildContextMap(){const e=this.lines.length;if(this._contextMapCache&&this._contextMapLinesLength===e&&this._contextMapFirstLine===this.lines[0]&&this._contextMapLastLine===this.lines[e-1])return this._contextMapCache;const t=/* @__PURE__ */new Map,r=[];let o=null;for(let l=0;l<e;l++){const e=this.lines[l],a=r[r.length-1]?.context||"Feature";t.set(l,a),s.test(e)?o="geometry":i.test(e)?o="properties":n.test(e)&&(o="Feature"),F.lastIndex=0,A.lastIndex=0,$.lastIndex=0,B.lastIndex=0;const h=(e.match(F)||[]).length,c=(e.match(A)||[]).length,d=(e.match($)||[]).length,u=(e.match(B)||[]).length;for(let t=0;t<h+d;t++)r.push({context:o||a,isArray:t>=h}),o=null;for(let t=0;t<c+u&&r.length>0;t++)r.pop()}return this._contextMapCache=t,this._contextMapLinesLength=e,this._contextMapFirstLine=this.lines[0],this._contextMapLastLine=this.lines[e-1],t}set(e,t={}){const s=H(e);this._setFeaturesInternal(s,t)}add(e,t={}){const s=H(e),i=[...this._parseFeatures(),...s];this._setFeaturesInternal(i,t)}insertAt(e,t,s={}){const i=H(e),n=this._parseFeatures(),r=t<0?n.length+t:t;n.splice(Math.max(0,Math.min(r,n.length)),0,...i),this._setFeaturesInternal(n,s)}_setFeaturesInternal(e,t){const s=e.map(e=>JSON.stringify(e,null,2)).join(",\n");this.setValue(s,!1),this._applyCollapsedFromOptions(t,e)}removeAt(e){const t=this._parseFeatures(),s=e<0?t.length+e:e;if(s>=0&&s<t.length){const e=t.splice(s,1)[0];return this.set(t),e}}removeAll(){this.lines.length>0&&this._saveToHistory("removeAll");const e=this._parseFeatures();return this.lines=[],this.collapsedNodes.clear(),this.hiddenFeatures.clear(),this.updateModel(),this.scheduleRender(),this.updatePlaceholderVisibility(),this.emitChange(),e}get(e){const t=this._parseFeatures();return t[e<0?t.length+e:e]}getAll(){return this._parseFeatures()}emit(){this.emitChange()}save(e="features.geojson"){try{const t={type:"FeatureCollection",features:this._parseFeatures()},s=JSON.stringify(t,null,2),i=new Blob([s],{type:"application/geo+json"}),n=URL.createObjectURL(i),r=z("a");return r.href=n,r.download=e,document.body.appendChild(r),r.click(),document.body.removeChild(r),URL.revokeObjectURL(n),!0}catch(t){return!1}}open(e={}){return new Promise(t=>{const s=z("input");s.type="file",s.accept=".geojson,.json,application/geo+json,application/json",s.style.display="none",s.addEventListener("change",i=>{const n=i.target.files?.[0];if(!n)return document.body.removeChild(s),void t(!1);const r=new FileReader;r.onload=i=>{try{const n=i.target?.result,r=H(JSON.parse(n));this._saveToHistory("open"),this.set(r,e),this.clearHistory(),document.body.removeChild(s),t(!0)}catch(n){document.body.removeChild(s),t(!1)}},r.onerror=()=>{document.body.removeChild(s),t(!1)},r.readAsText(n)}),s.addEventListener("cancel",()=>{document.body.removeChild(s),t(!1)}),document.body.appendChild(s),s.click()})}_parseFeatures(){try{const e=this.lines.join("\n");return e.trim()?JSON.parse("["+e+"]"):[]}catch(e){return[]}}}customElements.get("geojson-editor")||customElements.define("geojson-editor",q);export{q as default};
9
+ const e=["type","geometry","properties","coordinates","id","features"],t=["Point","MultiPoint","LineString","MultiLineString","Polygon","MultiPolygon"],i=/"geometry"\s*:/,s=/"properties"\s*:/,n=/"features"\s*:/,o=/^(\s*"[^"]+"\s*:\s*)([{\[])/,r=/^(\s*)([{\[]),?\s*$/,l=/&/g,a=/</g,h=/>/g,c=/([{}[\],:])/g,d=/"([^"]+)"(<span class="json-punctuation">:<\/span>)/g,u=/<span class="geojson-key">"type"<\/span><span class="json-punctuation">:<\/span>(\s*)"([^"]*)"/g,p=/(<span class="json-punctuation">:<\/span>)(\s*)"([^"]*)"/g,g=/(<span class="json-punctuation">:<\/span>)(\s*)(-?\d+\.?\d*(?:e[+-]?\d+)?)/gi,f=/(<span class="json-punctuation">[\[,]<\/span>)(\s*)(-?\d+\.?\d*(?:e[+-]?\d+)?)/gi,m=/^(\s*)(-?\d+\.?\d*(?:e[+-]?\d+)?)/gim,_=/(<span class="json-punctuation">:<\/span>)(\s*)(true|false)/g,v=/(<span class="json-punctuation">:<\/span>)(\s*)(null)/g,C=/(<\/span>|^)([^<]+)(<span|$)/g,b=/^#(?:[0-9a-fA-F]{3}|[0-9a-fA-F]{6})$/,L=/^#?([0-9a-fA-F])([0-9a-fA-F])([0-9a-fA-F])$/,y=/^\s*$/,x=/(\s+)/,w=/"([\w-]+)"\s*:\s*(?:"([^"]*)"|(\btrue\b|\bfalse\b))/g,k=/"([\w-]+)"\s*:\s*(?:"([^"]*)"|(\btrue\b|\bfalse\b))/,S=/"([\w-]+)"\s*:\s*(true|false)/,T=/"type"\s*:\s*"Feature"/,E=/^\s*"([^"]+)"\s*:\s*([{\[])/,j=/^\s*([{\[]),?\s*$/,R=/[{\[]/,N=/[\w-]/,F=/([A-Z])/g,I=/\{/g,$=/\}/g,A=/\[/g,B=/\]/g;function M(e){if(!e)return null;if(void 0!==e.id)return`id:${e.id}`;if(void 0!==e.properties?.id)return`prop:${e.properties.id}`;const t=e.geometry?.type||"null",i=e.geometry,s=JSON.stringify(i?.coordinates||[]);let n=0;for(let o=0;o<s.length;o++)n=(n<<5)-n+s.charCodeAt(o),n&=n;return`hash:${t}:${n.toString(36)}`}function K(e,t){const i="{"===t?"}":"]";let s=0,n=0,o=!1,r=!1;for(const l of e)r?r=!1:"\\"===l&&o?r=!0:'"'!==l?o||(l===t&&s++,l===i&&n++):o=!o;return{open:s,close:n}}function P(e){if(!e||"object"!=typeof e)throw new Error("Feature must be an object");if("Feature"!==e.type)throw new Error('Feature type must be "Feature"');if(!("geometry"in e))throw new Error("Feature must have a geometry property");if(!("properties"in e))throw new Error("Feature must have a properties property");if(null!==e.geometry){if("object"!=typeof e.geometry)throw new Error("Feature geometry must be an object or null");if(!e.geometry.type)throw new Error("Feature geometry must have a type");if(!t.includes(e.geometry.type))throw new Error(`Invalid geometry type: "${e.geometry.type}"`);if(!("coordinates"in e.geometry))throw new Error("Feature geometry must have coordinates")}if(null!==e.properties&&"object"!=typeof e.properties)throw new Error("Feature properties must be an object or null")}function H(e){let t=[];if(Array.isArray(e))t=e;else{if(!e||"object"!=typeof e)throw new Error("Input must be a Feature, array of Features, or FeatureCollection");if("FeatureCollection"===e.type&&"features"in e&&Array.isArray(e.features))t=e.features;else{if("Feature"!==e.type)throw new Error("Input must be a Feature, array of Features, or FeatureCollection");t=[e]}}for(const i of t)P(i);return t}let O=null;function D(e){return",aliceblue,antiquewhite,aqua,aquamarine,azure,beige,bisque,black,blanchedalmond,blue,blueviolet,brown,burlywood,cadetblue,chartreuse,chocolate,coral,cornflowerblue,cornsilk,crimson,cyan,darkblue,darkcyan,darkgoldenrod,darkgray,darkgreen,darkgrey,darkkhaki,darkmagenta,darkolivegreen,darkorange,darkorchid,darkred,darksalmon,darkseagreen,darkslateblue,darkslategray,darkslategrey,darkturquoise,darkviolet,deeppink,deepskyblue,dimgray,dimgrey,dodgerblue,firebrick,floralwhite,forestgreen,fuchsia,gainsboro,ghostwhite,gold,goldenrod,gray,green,greenyellow,grey,honeydew,hotpink,indianred,indigo,ivory,khaki,lavender,lavenderblush,lawngreen,lemonchiffon,lightblue,lightcoral,lightcyan,lightgoldenrodyellow,lightgray,lightgreen,lightgrey,lightpink,lightsalmon,lightseagreen,lightskyblue,lightslategray,lightslategrey,lightsteelblue,lightyellow,lime,limegreen,linen,magenta,maroon,mediumaquamarine,mediumblue,mediumorchid,mediumpurple,mediumseagreen,mediumslateblue,mediumspringgreen,mediumturquoise,mediumvioletred,midnightblue,mintcream,mistyrose,moccasin,navajowhite,navy,oldlace,olive,olivedrab,orange,orangered,orchid,palegoldenrod,palegreen,paleturquoise,palevioletred,papayawhip,peachpuff,peru,pink,plum,powderblue,purple,rebeccapurple,red,rosybrown,royalblue,saddlebrown,salmon,sandybrown,seagreen,seashell,sienna,silver,skyblue,slateblue,slategray,slategrey,snow,springgreen,steelblue,tan,teal,thistle,tomato,turquoise,violet,wheat,white,whitesmoke,yellow,yellowgreen,".includes(","+e.toLowerCase()+",")}function V(i,s,n){if(!i)return"";let L=i,w=null;if(n?.collapseButton?.isCollapsed){const e=i.match(o),t=!e&&i.match(r);e?(L=e[1]+e[2],w=e[2]):t&&(L=t[1]+t[2],w=t[2])}let k=L.replace(l,"&amp;").replace(a,"&lt;").replace(h,"&gt;");if(k=k.replace(c,'<span class="json-punctuation">$1</span>'),d.lastIndex=0,k=k.replace(d,(t,i,n)=>"properties"!==s&&e.includes(i)?`<span class="geojson-key">"${i}"</span>${n}`:`<span class="json-key">"${i}"</span>${n}`),"properties"!==s&&(u.lastIndex=0,k=k.replace(u,(e,i,s)=>`<span class="geojson-key">"type"</span><span class="json-punctuation">:</span>${i}<span class="${"Feature"===s||"FeatureCollection"===s||t.includes(s)?"geojson-type":"geojson-type-invalid"}">"${s}"</span>`)),p.lastIndex=0,k=k.replace(p,(e,t,i,s)=>e.includes("geojson-type")||e.includes("json-string")?e:b.test(s)||D(s)?`${t}${i}<span class="json-string json-color" data-color="${s}" style="--swatch-color: ${s}">"${s}"</span>`:`${t}${i}<span class="json-string">"${s}"</span>`),g.lastIndex=0,k=k.replace(g,'$1$2<span class="json-number">$3</span>'),f.lastIndex=0,k=k.replace(f,'$1$2<span class="json-number">$3</span>'),m.lastIndex=0,k=k.replace(m,'$1<span class="json-number">$2</span>'),_.lastIndex=0,k=k.replace(_,(e,t,i,s)=>`${t}${i}<span class="json-boolean${"true"===s?" json-bool-true":" json-bool-false"}">${s}</span>`),v.lastIndex=0,k=k.replace(v,'$1$2<span class="json-null">$3</span>'),w){const e="["===w?"collapsed-bracket-array":"collapsed-bracket-object";k=k.replace(new RegExp(`<span class="json-punctuation">\\${w}<\\/span>$`),`<span class="${e}">${w}</span>`)}return C.lastIndex=0,k=k.replace(C,(e,t,i,s)=>{if(!i||y.test(i))return e;const n=i.split(x);let o=!1;const r=n.map(e=>y.test(e)?e:(o=!0,`<span class="json-error">${e}</span>`)).join("");return o?t+r+s:e}),k}const z=e=>document.createElement(e);class q extends HTMLElement{constructor(){super(),this.lines=[],this.collapsedNodes=/* @__PURE__ */new Set,this.hiddenFeatures=/* @__PURE__ */new Set,this._nodeIdCounter=0,this._lineToNodeId=/* @__PURE__ */new Map,this._nodeIdToLines=/* @__PURE__ */new Map,this._openedNodeKeys=/* @__PURE__ */new Set,this.visibleLines=[],this.lineMetadata=/* @__PURE__ */new Map,this.featureRanges=/* @__PURE__ */new Map,this.viewportHeight=0,this.lineHeight=19.5,this.bufferLines=5,this._lastStartIndex=-1,this._lastEndIndex=-1,this._lastTotalLines=-1,this._scrollRaf=null,this.cursorLine=0,this.cursorColumn=0,this.selectionStart=null,this.selectionEnd=null,this.renderTimer=void 0,this.inputTimer=void 0,this.themes={dark:{},light:{}},this._undoStack=[],this._redoStack=[],this._maxHistorySize=100,this._lastActionTime=0,this._lastActionType=null,this._groupingDelay=500,this._isSelecting=!1,this._isComposing=!1,this._blockRender=!1,this._insertMode=!0,this._charWidth=null,this._contextMapCache=null,this._contextMapLinesLength=0,this._contextMapFirstLine=void 0,this._contextMapLastLine=void 0,this._errorLinesCache=null,this._viewport=null,this._linesContainer=null,this._scrollContent=null,this._hiddenTextarea=null,this._gutterContent=null,this._gutterScrollContent=null,this._gutterScroll=null,this._gutter=null,this._clearBtn=null,this._editorWrapper=null,this._placeholderLayer=null,this._editorPrefix=null,this._editorSuffix=null,this._errorNav=null,this._errorCount=null,this._prevErrorBtn=null,this._nextErrorBtn=null,this.attachShadow({mode:"open"})}_id(e){return this.shadowRoot.getElementById(e)}_invalidateRenderCache(){this._lastStartIndex=-1,this._lastEndIndex=-1,this._lastTotalLines=-1}_createSnapshot(){return{lines:[...this.lines],cursorLine:this.cursorLine,cursorColumn:this.cursorColumn,timestamp:Date.now()}}_restoreSnapshot(e){this.lines=[...e.lines],this.cursorLine=e.cursorLine,this.cursorColumn=e.cursorColumn,this.updateModel(),this._invalidateRenderCache(),this.scheduleRender(),this.updatePlaceholderVisibility(),this.emitChange()}_saveToHistory(e="edit"){const t=Date.now();if(!(e===this._lastActionType&&t-this._lastActionTime<this._groupingDelay)){const e=this._createSnapshot();this._undoStack.push(e),this._undoStack.length>this._maxHistorySize&&this._undoStack.shift(),this._redoStack=[]}this._lastActionTime=t,this._lastActionType=e}undo(){if(0===this._undoStack.length)return!1;this._redoStack.push(this._createSnapshot());const e=this._undoStack.pop();return e&&this._restoreSnapshot(e),this._lastActionType=null,this._lastActionTime=0,!0}redo(){if(0===this._redoStack.length)return!1;this._undoStack.push(this._createSnapshot());const e=this._redoStack.pop();return e&&this._restoreSnapshot(e),this._lastActionType=null,this._lastActionTime=0,!0}clearHistory(){this._undoStack=[],this._redoStack=[],this._lastActionType=null,this._lastActionTime=0}canUndo(){return this._undoStack.length>0}canRedo(){return this._redoStack.length>0}_generateNodeId(){return"node_"+ ++this._nodeIdCounter}_getCollapsedRangeForLine(e){for(const[t,i]of this._nodeIdToLines)if(this.collapsedNodes.has(t)&&e>i.startLine&&e<i.endLine)return{nodeId:t,...i};return null}_getCollapsedClosingLine(e){for(const[t,i]of this._nodeIdToLines)if(this.collapsedNodes.has(t)&&e===i.endLine)return{nodeId:t,...i};return null}_getClosingBracketPos(e){return Math.max(e.lastIndexOf("]"),e.lastIndexOf("}"))}_getCollapsedNodeAtLine(e){const t=this._lineToNodeId.get(e);if(t&&this.collapsedNodes.has(t)){const e=this._nodeIdToLines.get(t);if(e)return{nodeId:t,...e}}return null}_getContainingExpandedNode(e){let t=null;for(const[i,s]of this._nodeIdToLines)this.collapsedNodes.has(i)||e>=s.startLine&&e<=s.endLine&&(!t||s.endLine-s.startLine<t.endLine-t.startLine)&&(t={nodeId:i,...s});return t}_deleteCollapsedNode(e){this._saveToHistory("delete");const t=e.endLine-e.startLine+1;this.lines.splice(e.startLine,t),this.cursorLine=Math.min(e.startLine,this.lines.length-1),this.cursorColumn=0,this.formatAndUpdate()}_rebuildNodeIdMappings(){const e=/* @__PURE__ */new Set;for(const i of this.collapsedNodes){const t=this._nodeIdToLines.get(i);t?.uniqueKey&&e.add(t.uniqueKey)}this._nodeIdCounter=0,this._lineToNodeId.clear(),this._nodeIdToLines.clear(),this.collapsedNodes.clear();const t=/* @__PURE__ */new Map;for(let i=0;i<this.lines.length;i++){const s=this.lines[i],n=s.match(E),o=!n&&s.match(j);if(!n&&!o)continue;let r,l;if(n)r=n[1],l=n[2];else{if(!o)continue;l=o[1],r=`__root_${l}_${i}`}const a=K(s.substring(s.indexOf(l)+1),l);if(a.close>a.open)continue;const h=this._findClosingLine(i,l);if(-1===h||h===i)continue;const c=this._generateNodeId(),d=t.get(r)||0;t.set(r,d+1);const u=`${r}:${d}`;this._lineToNodeId.set(i,c),this._nodeIdToLines.set(c,{startLine:i,endLine:h,nodeKey:r,uniqueKey:u,isRootFeature:!!o}),e.has(u)&&!this._openedNodeKeys.has(u)&&this.collapsedNodes.add(c)}}static get observedAttributes(){return["readonly","value","placeholder","dark-selector","internal-add-shortcut"]}connectedCallback(){this.render(),this._cacheElements(),this.setupEventListeners(),this.updatePrefixSuffix(),this.updateThemeCSS(),this.value&&this.setValue(this.value),this.updatePlaceholderVisibility()}disconnectedCallback(){this.renderTimer&&clearTimeout(this.renderTimer),this.inputTimer&&clearTimeout(this.inputTimer);const e=document.querySelector(".geojson-color-picker-input");e&&(e._closeListener&&document.removeEventListener("click",e._closeListener,!0),e.remove())}attributeChangedCallback(e,t,i){if(t!==i)switch(e){case"value":this.setValue(i);break;case"readonly":this.updateReadonly();break;case"placeholder":this.updatePlaceholderContent();break;case"dark-selector":this.updateThemeCSS()}}get readonly(){return this.hasAttribute("readonly")}get value(){return this.getAttribute("value")||""}get placeholder(){return this.getAttribute("placeholder")||""}get internalAddShortcut(){return this.hasAttribute("internal-add-shortcut")}get prefix(){return'{"type": "FeatureCollection", "features": ['}get suffix(){return"]}"}render(){const e=this.shadowRoot,t=z("style");t.textContent=':host *,:host *:before,:host *:after{box-sizing:border-box;font: 13px/1.5 Courier New,Courier,monospace;font-variant:normal;letter-spacing:0;word-spacing:0;text-transform:none;text-decoration:none;text-indent:0}:host{--line-height: 19.5px;--gutter-width: 50px;--editor-padding-y: 8px;--editor-padding-x: 12px;display:flex;flex-direction:column;position:relative;width:100%;height:400px;border-radius:4px;overflow:hidden}:host([readonly]) .editor-wrapper:after{content:"";position:absolute;inset:0;pointer-events:none;background:repeating-linear-gradient(-45deg,rgba(128,128,128,.08),rgba(128,128,128,.08) 3px,transparent 3px,transparent 12px);z-index:1}:host([readonly]) .hidden-textarea{cursor:text}.editor-wrapper{position:relative;width:100%;flex:1;background:var(--bg-color, #fff);display:flex;overflow:hidden}.gutter{width:var(--gutter-width);background:var(--gutter-bg, #f0f0f0);border-right:1px solid var(--gutter-border, #e0e0e0);overflow:hidden;flex-shrink:0;position:relative}.gutter-scroll{position:absolute;inset:0;overflow:hidden;padding:8px 0}.gutter-scroll-content{position:relative;width:100%}.gutter-content{position:absolute;top:0;left:0;right:0;will-change:transform}.gutter-line{height:var(--line-height);display:flex;align-items:center;justify-content:flex-end;padding-right:4px;position:relative}.gutter-line.has-error:before{content:"";position:absolute;right:0;top:0;bottom:0;width:3px;background:var(--error-color, #dc3545)}.line-number{font-size:11px;color:var(--gutter-text, #999);-webkit-user-select:none;user-select:none;min-width:20px;text-align:right}.collapse-column{width:16px;display:flex;align-items:center;justify-content:center;flex-shrink:0}.collapse-button{width:12px;height:12px;border-radius:2px;cursor:pointer;flex-shrink:0;background:transparent;border:none;color:var(--json-punct, #a9b7c6);font-size:10px;display:flex;align-items:center;justify-content:center;-webkit-user-select:none;user-select:none;opacity:0;transition:transform .1s,opacity .15s}.collapse-button.collapsed,.gutter:hover .collapse-button{opacity:1}.collapse-button:hover{transform:scale(1.2)}@media(hover:none),(pointer:coarse){.collapse-button{opacity:1}}.editor-content{position:relative;flex:1;overflow:hidden}.hidden-textarea{position:absolute;top:0;left:0;width:100%;height:100%;opacity:0;padding:0;margin:0;border:none;outline:none;resize:none;overflow:hidden;z-index:-1;pointer-events:none;caret-color:transparent}.viewport{position:absolute;inset:0;overflow:auto;padding:var(--editor-padding-y) var(--editor-padding-x);overscroll-behavior:contain;scrollbar-width:thin;scrollbar-color:var(--control-border, #c0c0c0) var(--control-bg, #e8e8e8)}.scroll-content{position:relative;width:100%}.lines-container{position:absolute;top:0;left:0;right:0;will-change:transform}.line{height:var(--line-height);white-space:pre;display:block;position:relative}.cursor{position:absolute;width:2px;height:1em;background:var(--caret-color, #000);top:.15em;pointer-events:none;animation:cursor-blink 1s step-end infinite}.cursor.cursor-block{width:.6em;opacity:.5;animation:cursor-blink-block 1s step-end infinite}@keyframes cursor-blink-block{0%,to{opacity:.5}50%{opacity:.2}}@keyframes cursor-blink{0%,to{opacity:1}50%{opacity:0}}.selection{position:absolute;height:100%;top:0;background:var(--selection-color, rgba(51, 153, 255, .3));pointer-events:none;z-index:0}.line-hidden>span{opacity:.35;filter:grayscale(50%)}.placeholder-layer{position:absolute;top:var(--editor-padding-y);left:var(--editor-padding-x);color:#6a6a6a;pointer-events:none;z-index:0;white-space:pre}.json-key{color:var(--json-key, #660e7a)}.json-string{color:var(--json-string, #008000)}.json-number{color:var(--json-number, #00f)}.json-boolean,.json-null{color:var(--json-boolean, #000080)}.json-punctuation{color:var(--json-punct, #000)}.json-error{color:var(--json-error, #f00)}.geojson-key{color:var(--geojson-key, #660e7a);font-weight:600}.geojson-type{color:var(--geojson-type, #008000);font-weight:600}.geojson-type-invalid{color:var(--geojson-type-invalid, #f00);font-weight:600}.collapsed-bracket-array,.collapsed-bracket-object{color:var(--json-punct, #000)}.collapsed-bracket-array:after,.collapsed-bracket-object:after{content:"…";color:var(--json-punct, #888)}.prefix-wrapper,.suffix-wrapper{display:flex;flex-shrink:0;background:var(--bg-color, #fff)}.prefix-gutter,.suffix-gutter{width:var(--gutter-width);background:var(--gutter-bg, #f0f0f0);border-right:1px solid var(--gutter-border, #e0e0e0);flex-shrink:0}.editor-prefix,.editor-suffix{flex:1;padding:4px 12px;color:var(--text-color, #000);background:var(--bg-color, #fff);-webkit-user-select:none;user-select:none;white-space:pre-wrap;word-wrap:break-word;opacity:.6}.prefix-wrapper{border-bottom:1px solid rgba(255,255,255,.1);position:relative}.suffix-wrapper{border-top:1px solid rgba(255,255,255,.1);position:relative}.info-btn{position:absolute;right:.5rem;top:50%;transform:translateY(-50%);background:transparent;border:none;color:var(--text-color, #000);opacity:.15;cursor:pointer;font-size:.7rem;width:1rem;height:1rem;padding:0;border-radius:50%;display:flex;align-items:center;justify-content:center;transition:opacity .2s;font-family:sans-serif}.info-btn:hover{opacity:.5}.info-popup{display:none;position:absolute;top:2rem;right:.5rem;z-index:1000;background:var(--bg-color, #ffffff);border:1px solid var(--gutter-border, #e0e0e0);border-radius:6px;box-shadow:0 4px 12px #00000026;padding:12px 16px;min-width:200px;font-family:-apple-system,BlinkMacSystemFont,Segoe UI,Roboto,sans-serif}.info-popup.visible{display:block}.info-popup-content{text-align:center}.info-popup-title{font-weight:700;font-size:13px;color:var(--text-color, #000000);margin-bottom:4px}.info-popup-version{display:block;font-size:11px;color:var(--text-color, #000000);opacity:.6;margin-bottom:8px;text-decoration:none}.info-popup-version:hover{opacity:1;text-decoration:underline!important}.info-popup-copyright{font-size:10px;color:var(--text-color, #000000);opacity:.5}.clear-btn{position:absolute;right:.5rem;top:50%;transform:translateY(-50%);background:transparent;border:none;color:var(--text-color, #000);opacity:.3;cursor:pointer;font-size:.65rem;width:1rem;height:1rem;padding:.15rem 0 0;border-radius:3px;display:flex;align-items:center;justify-content:center;transition:opacity .2s,background .2s}.clear-btn:hover{opacity:.7;background:#ffffff1a}.clear-btn[hidden]{display:none}.error-nav{display:none;align-items:center;gap:4px;margin-right:24px}.error-nav.visible{display:flex}.error-nav-btn{background:transparent;border:none;color:var(--error-color, #dc3545);cursor:pointer;font-size:8px;width:16px;height:16px;padding:0;display:flex;align-items:center;justify-content:center;opacity:.7;transition:opacity .2s}.error-nav-btn:hover{opacity:1}.error-count{color:var(--error-color, #dc3545);font-size:11px;min-width:20px;text-align:center}.viewport::-webkit-scrollbar{width:10px;height:10px}.viewport::-webkit-scrollbar-track{background:var(--control-bg, #e8e8e8)}.viewport::-webkit-scrollbar-thumb{background:var(--control-border, #c0c0c0);border-radius:5px}.viewport::-webkit-scrollbar-thumb:hover{background:var(--control-color, #000080)}.json-color,.json-boolean{position:relative}.json-color:before,.json-boolean:before{content:"";position:absolute;left:-8px;top:50%;transform:translateY(-50%);margin-top:-1px;width:8px;height:8px;border-radius:2px;cursor:pointer}.json-color:hover:before,.json-boolean:hover:before{transform:translateY(-50%) scale(1.2);border-color:var(--control-color, #000080)}.json-color:before{background-color:var(--swatch-color);border:1px solid var(--json-punct, #a9b7c6)}.json-boolean:before{border:1.5px solid var(--control-border, #c0c0c0);background:transparent}.json-bool-true:before{content:"✔";border-color:var(--control-color, #000080);color:var(--control-color, #000080);font-size:8px;display:flex;align-items:center;justify-content:center;line-height:8px}.line.has-visibility:before{content:"👁";position:absolute;left:0;top:3px;font-size:10px;color:var(--control-color, #000080);opacity:.6;cursor:pointer;z-index:1}.line.has-visibility:hover:before{opacity:1;transform:scale(1.15)}.line.has-visibility.feature-hidden:before{opacity:.5}';const i=z("div");for(i.innerHTML=function(e="",t=""){return`\n <div class="prefix-wrapper">\n <div class="prefix-gutter"></div>\n <div class="editor-prefix" id="editorPrefix"></div>\n <button class="info-btn" id="infoBtn" title="@softwarity/geojson-editor v${t}" aria-label="About">ⓘ</button>\n </div>\n <div class="editor-wrapper">\n <div class="gutter">\n <div class="gutter-scroll" id="gutterScroll">\n <div class="gutter-scroll-content" id="gutterScrollContent">\n <div class="gutter-content" id="gutterContent"></div>\n </div>\n </div>\n </div>\n <div class="editor-content">\n <div class="placeholder-layer" id="placeholderLayer">${i=e,i?i.replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;"):""}</div>\n <textarea\n class="hidden-textarea"\n id="hiddenTextarea"\n spellcheck="false"\n autocomplete="off"\n autocorrect="off"\n autocapitalize="off"\n tabindex="0"\n ></textarea>\n <div class="viewport" id="viewport">\n <div class="scroll-content" id="scrollContent">\n <div class="lines-container" id="linesContainer"></div>\n </div>\n </div>\n </div>\n </div>\n <div class="suffix-wrapper">\n <div class="suffix-gutter"></div>\n <div class="editor-suffix" id="editorSuffix"></div>\n <div class="error-nav" id="errorNav">\n <button class="error-nav-btn" id="prevErrorBtn" title="Previous error">◀</button>\n <span class="error-count" id="errorCount"></span>\n <button class="error-nav-btn" id="nextErrorBtn" title="Next error">▶</button>\n </div>\n <button class="clear-btn" id="clearBtn" title="Clear editor">✕</button>\n </div>\n <div class="info-popup" id="infoPopup">\n <div class="info-popup-content">\n <div class="info-popup-title">GeoJSON Editor</div>\n <a class="info-popup-version" href="https://www.npmjs.com/package/@softwarity/geojson-editor/v/${t}" target="_blank" rel="noopener">v${t}</a>\n <div class="info-popup-copyright">© ${/* @__PURE__ */(new Date).getFullYear()} Softwarity</div>\n </div>\n </div>\n `;var i}(this.placeholder,"1.0.22"),e.innerHTML="",e.appendChild(t);i.firstChild;)e.appendChild(i.firstChild)}_cacheElements(){this._viewport=this._id("viewport"),this._linesContainer=this._id("linesContainer"),this._scrollContent=this._id("scrollContent"),this._hiddenTextarea=this._id("hiddenTextarea"),this._gutterContent=this._id("gutterContent"),this._gutterScrollContent=this._id("gutterScrollContent"),this._gutterScroll=this._id("gutterScroll"),this._gutter=this.shadowRoot.querySelector(".gutter"),this._clearBtn=this._id("clearBtn"),this._editorWrapper=this.shadowRoot.querySelector(".editor-wrapper"),this._placeholderLayer=this._id("placeholderLayer"),this._editorPrefix=this._id("editorPrefix"),this._editorSuffix=this._id("editorSuffix"),this._errorNav=this._id("errorNav"),this._errorCount=this._id("errorCount"),this._prevErrorBtn=this._id("prevErrorBtn"),this._nextErrorBtn=this._id("nextErrorBtn")}setupEventListeners(){const e=this._hiddenTextarea,t=this._viewport,i=this._gutterContent,s=this._gutter,n=this._clearBtn,o=this._editorWrapper;if(!(e&&t&&i&&s&&n&&o))return;this._isSelecting=!1,t.addEventListener("click",e=>{this.handleEditorClick(e)},!0),t.addEventListener("mousedown",t=>{const i=t.target,s=i.closest(".line.has-visibility");if(s){const e=s.getBoundingClientRect();if(t.clientX-e.left<14)return void(this._blockRender=!0)}if(i.classList.contains("json-color")||i.classList.contains("json-boolean")){const e=i.getBoundingClientRect(),s=t.clientX-e.left;if(s<0&&s>=-8)return void(this._blockRender=!0)}if(t.preventDefault(),2===t.detail){const i=this._getPositionFromClick(t);return this._selectWordAt(i.line,i.column),this._isSelecting=!1,e.focus(),this._invalidateRenderCache(),void this.scheduleRender()}const n=this._getPositionFromClick(t);t.shiftKey&&this.selectionStart?(this.selectionEnd=n,this.cursorLine=n.line,this.cursorColumn=n.column):(this.cursorLine=n.line,this.cursorColumn=n.column,this.selectionStart={line:n.line,column:n.column},this.selectionEnd=null,this._isSelecting=!0),e.focus(),this._invalidateRenderCache(),this.scheduleRender()});let r=null;const l=()=>{r&&(clearInterval(r),r=null)},a=e=>{l(),r=setInterval(()=>{this._isSelecting?("up"===e?t.scrollTop-=20:t.scrollTop+=20,this._updateSelectionFromScroll(e),this._invalidateRenderCache(),this.scheduleRender()):l()},50)};document.addEventListener("mousemove",e=>{if(!this._isSelecting)return;const i=t.getBoundingClientRect();if(e.clientY<i.top)a("up");else if(e.clientY>i.bottom)a("down");else{l();const s=this._getPositionFromClick(e);this.selectionEnd=s,this.cursorLine=s.line,this.cursorColumn=s.column,e.clientY<i.top+30?t.scrollTop-=20:e.clientY>i.bottom-30&&(t.scrollTop+=20),this._invalidateRenderCache(),this.scheduleRender()}}),document.addEventListener("mouseup",()=>{this._isSelecting=!1,l()}),e.addEventListener("focus",()=>{o.classList.add("focused"),this._invalidateRenderCache(),this.scheduleRender()}),e.addEventListener("blur",()=>{o.classList.remove("focused"),this._invalidateRenderCache(),this.scheduleRender()});let h=!1;t.addEventListener("scroll",()=>{h||(this.syncGutterScroll(),this._scrollRaf||(this._scrollRaf=requestAnimationFrame(()=>{this._scrollRaf=null,h=!0,this.renderViewport(),h=!1})))}),e.addEventListener("compositionstart",()=>{this._isComposing=!0}),e.addEventListener("compositionend",()=>{this._isComposing=!1,this.handleInput()}),e.addEventListener("input",()=>{this._isComposing||this.handleInput()}),e.addEventListener("keydown",e=>{this.handleKeydown(e)}),e.addEventListener("paste",e=>{this.handlePaste(e)}),e.addEventListener("copy",e=>{this.handleCopy(e)}),e.addEventListener("cut",e=>{this.handleCut(e)}),i.addEventListener("click",e=>{this.handleGutterClick(e)}),s.addEventListener("mousedown",e=>{e.preventDefault()}),s.addEventListener("wheel",e=>{e.preventDefault(),t.scrollTop+=e.deltaY}),n.addEventListener("click",()=>{this.removeAll()});const c=this._id("infoBtn"),d=this._id("infoPopup");c&&d&&(c.addEventListener("click",e=>{e.stopPropagation(),d.classList.toggle("visible")}),document.addEventListener("click",()=>{d.classList.remove("visible")})),this._prevErrorBtn?.addEventListener("click",()=>{this.goToPrevError()}),this._nextErrorBtn?.addEventListener("click",()=>{this.goToNextError()}),this.updateReadonly()}setValue(e,t=!0){if(this.lines.length>0&&this._saveToHistory("setValue"),e&&e.trim())try{const t="["+e+"]",i=JSON.parse(t),s=JSON.stringify(i,null,2).split("\n");this.lines=s.slice(1,-1)}catch(i){this.lines=e.split("\n")}else this.lines=[];this.collapsedNodes.clear(),this.hiddenFeatures.clear(),this._openedNodeKeys.clear(),this._lineToNodeId.clear(),this._nodeIdToLines.clear(),this.cursorLine=0,this.cursorColumn=0,this.updateModel(),this.scheduleRender(),this.updatePlaceholderVisibility(),t&&this.lines.length>0&&requestAnimationFrame(()=>{this.autoCollapseCoordinates()}),this.emitChange()}getContent(){return this.lines.join("\n")}updateModel(){this._contextMapCache=null,this._errorLinesCache=null,this._rebuildNodeIdMappings(),this.computeFeatureRanges(),this.computeLineMetadata(),this.computeVisibleLines()}updateView(){this.computeLineMetadata(),this.computeVisibleLines()}computeFeatureRanges(){this.featureRanges.clear();try{const e=this.lines.join("\n"),t=this.prefix+e+this.suffix,i=JSON.parse(t);if(!i.features)return;let s=0,n=0,o=!1,r=-1,l=null;for(let a=0;a<this.lines.length;a++){const e=this.lines[a];if(!o&&T.test(e)){let e=a;for(let t=a;t>=0;t--){const i=this.lines[t].trim();if("{"===i||"{,"===i){e=t;break}}r=e,o=!0,n=1;for(let t=e;t<=a;t++){const i=K(this.lines[t],"{");n+=t===e?i.open-1-i.close:i.open-i.close}s<i.features.length&&(l=M(i.features[s]))}else if(o){const t=K(e,"{");n+=t.open-t.close,n<=0&&(l&&this.featureRanges.set(l,{startLine:r,endLine:a,featureIndex:s}),s++,o=!1,l=null)}}}catch(e){}}computeLineMetadata(){this.lineMetadata.clear();const e=this._findCollapsibleRanges(),t=this._computeErrorLines();for(let i=0;i<this.lines.length;i++){const s=this.lines[i],n={colors:[],booleans:[],collapseButton:null,visibilityButton:null,isHidden:!1,isCollapsed:!1,featureKey:null,hasError:t.has(i)};let o;for(w.lastIndex=0;null!==(o=w.exec(s));){const[,e,t,i]=o;i?n.booleans.push({attributeName:e,value:"true"===i}):t&&(b.test(t)||D(t))&&n.colors.push({attributeName:e,color:t})}const r=e.find(e=>e.startLine===i);r&&(n.collapseButton={nodeKey:r.nodeKey,nodeId:r.nodeId,isCollapsed:this.collapsedNodes.has(r.nodeId)}),e.find(e=>this.collapsedNodes.has(e.nodeId)&&i>e.startLine&&i<e.endLine)&&(n.isCollapsed=!0);for(const[e,t]of this.featureRanges)if(i>=t.startLine&&i<=t.endLine){n.featureKey=e,this.hiddenFeatures.has(e)&&(n.isHidden=!0),i===t.startLine&&(n.visibilityButton={featureKey:e,isHidden:this.hiddenFeatures.has(e)});break}this.lineMetadata.set(i,n)}}_computeErrorLines(){if(null!==this._errorLinesCache)return this._errorLinesCache;const e=/* @__PURE__ */new Set;for(let i=0;i<this.lines.length;i++)V(this.lines[i],"",void 0).includes("json-error")&&e.add(i);try{const e="["+this.lines.join("\n")+"]";JSON.parse(e)}catch(t){if(t instanceof Error){const i=t.message.match(/line (\d+)/);if(i){const t=Math.max(0,parseInt(i[1],10)-1);e.add(t)}}}return this._errorLinesCache=e,e}_getErrorLines(){const e=[];for(const[t,i]of this.lineMetadata)i.hasError&&e.push(t);return e.sort((e,t)=>e-t)}goToNextError(){const e=this._getErrorLines();if(0===e.length)return!1;const t=e.find(e=>e>this.cursorLine),i=void 0!==t?t:e[0];return this._goToErrorLine(i)}goToPrevError(){const e=this._getErrorLines();if(0===e.length)return!1;const t=e.filter(e=>e<this.cursorLine),i=t.length>0?t[t.length-1]:e[e.length-1];return this._goToErrorLine(i)}_expandNodesContainingLine(e){let t=!1;for(const[i,s]of this._nodeIdToLines)this.collapsedNodes.has(i)&&e>s.startLine&&e<=s.endLine&&(this.collapsedNodes.delete(i),s.uniqueKey&&this._openedNodeKeys.add(s.uniqueKey),t=!0);return t}_goToErrorLine(e){return this._expandNodesContainingLine(e)&&this.updateView(),this.cursorLine=e,this.cursorColumn=0,this._invalidateRenderCache(),this._scrollToCursor(!0),this.renderViewport(),this._updateErrorDisplay(),this._hiddenTextarea?.focus(),!0}_expandErrorNodes(){const e=this._getErrorLines();if(0===e.length)return;let t=!1;for(const i of e)this._expandNodesContainingLine(i)&&(t=!0);t&&this.updateView()}computeVisibleLines(){this.visibleLines=[];for(let e=0;e<this.lines.length;e++){const t=this.lineMetadata.get(e);t&&t.isCollapsed||this.visibleLines.push({index:e,content:this.lines[e],meta:t})}this._invalidateRenderCache()}scheduleRender(){this.renderTimer||(this.renderTimer=requestAnimationFrame(()=>{this.renderTimer=void 0,this.renderViewport()}))}renderViewport(){if(this._blockRender)return;const e=this._viewport,t=this._linesContainer,i=this._scrollContent;if(!e||!t)return;this.viewportHeight=e.clientHeight;const s=this.visibleLines.length,n=s*this.lineHeight;if(i){i.style.height=`${n}px`;const e=this._getCharWidth(),t=this.lines.reduce((e,t)=>Math.max(e,t.length),0)*e+20;i.style.minWidth=`${t}px`}const o=e.scrollTop,r=Math.floor(o/this.lineHeight),l=Math.ceil(this.viewportHeight/this.lineHeight),a=Math.max(0,r-this.bufferLines),h=Math.min(s,r+l+this.bufferLines);if(s>0&&this._lastStartIndex===a&&this._lastEndIndex===h&&this._lastTotalLines===s)return;this._lastStartIndex=a,this._lastEndIndex=h,this._lastTotalLines=s;const c=a*this.lineHeight;t.style.transform=`translateY(${c}px)`;const d=this._buildContextMap(),u=this._editorWrapper?.classList.contains("focused"),p=document.createDocumentFragment();if(0===s){const e=z("div");return e.className="line empty-line",e.dataset.lineIndex="0",u&&(e.innerHTML=this._insertCursor(0)),p.appendChild(e),t.innerHTML="",t.appendChild(p),void this.renderGutter(0,0)}for(let g=a;g<h;g++){const e=this.visibleLines[g];if(!e)continue;const t=z("div");t.className="line",t.dataset.lineIndex=String(e.index),e.meta?.visibilityButton&&(t.classList.add("has-visibility"),t.dataset.featureKey=e.meta.visibilityButton.featureKey,e.meta.visibilityButton.isHidden&&t.classList.add("feature-hidden")),e.meta?.isHidden&&t.classList.add("line-hidden");const i=d.get(e.index)||"Feature";let s=V(e.content,i,e.meta);u&&this._hasSelection()&&(s=this._addSelectionHighlight(s,e.index,e.content)),u&&e.index===this.cursorLine&&(s+=this._insertCursor(this.cursorColumn)),t.innerHTML=s,p.appendChild(t)}t.innerHTML="",t.appendChild(p),this.renderGutter(a,h)}_insertCursor(e){const t=this._getCharWidth(),i=e*t;return this._insertMode?`<span class="cursor" style="left: ${i}px"></span>`:`<span class="cursor cursor-block" style="left: ${i}px; width: ${t}px"></span>`}_addSelectionHighlight(e,t,i){const s=this._normalizeSelection();if(!s)return e;const{start:n,end:o}=s;if(t<n.line||t>o.line)return e;const r=this._getCharWidth();let l,a;return t===n.line&&t===o.line?(l=n.column,a=o.column):t===n.line?(l=n.column,a=i.length):t===o.line?(l=0,a=o.column):(l=0,a=i.length),`<span class="selection" style="left: ${l*r}px; width: ${(a-l)*r}px"></span>`+e}_getCharWidth(){if(!this._charWidth){const e=z("canvas").getContext("2d");e?(e.font="13px 'Courier New', Courier, monospace",this._charWidth=e.measureText("M").width):this._charWidth=7.8}return this._charWidth}renderGutter(e,t){const i=this._gutterContent,s=this._gutterScrollContent;if(!i)return;const n=this.visibleLines.length*this.lineHeight;s&&(s.style.height=`${n}px`);const o=e*this.lineHeight;i.style.transform=`translateY(${o}px)`;const r=document.createDocumentFragment();for(let l=e;l<t;l++){const e=this.visibleLines[l];if(!e)continue;const t=z("div");t.className="gutter-line";const i=e.meta;i?.hasError&&t.classList.add("has-error");const s=z("span");s.className="line-number",s.textContent=String(e.index+1),t.appendChild(s);const n=z("div");if(n.className="collapse-column",i?.collapseButton){const t=z("div");t.className="collapse-button"+(i.collapseButton.isCollapsed?" collapsed":""),t.textContent=i.collapseButton.isCollapsed?"›":"⌄",t.dataset.line=String(e.index),t.dataset.nodeId=i.collapseButton.nodeId,t.title=i.collapseButton.isCollapsed?"Expand":"Collapse",n.appendChild(t)}t.appendChild(n),r.appendChild(t)}i.innerHTML="",i.appendChild(r)}syncGutterScroll(){this._gutterScroll&&this._viewport&&(this._gutterScroll.scrollTop=this._viewport.scrollTop)}handleInput(){const e=this._hiddenTextarea,t=e?.value;if(t)if(this._hasSelection()&&this._deleteSelection(),this._getCollapsedRangeForLine(this.cursorLine))e.value="";else{if(this._getCollapsedClosingLine(this.cursorLine)){const t=this.lines[this.cursorLine],i=this._getClosingBracketPos(t);if(this.cursorColumn<=i)return void(e.value="")}if(this._getCollapsedNodeAtLine(this.cursorLine)){const t=this.lines[this.cursorLine].search(R);if(this.cursorColumn>t)return void(e.value="")}if(this.cursorLine<this.lines.length){const e=this.lines[this.cursorLine],i=e.substring(0,this.cursorColumn),s=t.split("\n");if(1===s.length){if(this._insertMode){const s=e.substring(this.cursorColumn);this.lines[this.cursorLine]=i+t+s}else{const s=e.substring(this.cursorColumn+t.length);this.lines[this.cursorLine]=i+t+s}this.cursorColumn+=t.length}else{const t=e.substring(this.cursorColumn);this.lines[this.cursorLine]=i+s[0];for(let e=1;e<s.length-1;e++)this.lines.splice(this.cursorLine+e,0,s[e]);const n=s[s.length-1]+t;this.lines.splice(this.cursorLine+s.length-1,0,n),this.cursorLine+=s.length-1,this.cursorColumn=s[s.length-1].length}}else{const e=t.split("\n");this.lines.push(...e),this.cursorLine=this.lines.length-1,this.cursorColumn=this.lines[this.cursorLine].length}e.value="",clearTimeout(this.inputTimer),this.inputTimer=setTimeout(()=>{this.formatAndUpdate()},150)}}handleKeydown(e){const t={inCollapsedZone:this._getCollapsedRangeForLine(this.cursorLine),onCollapsedNode:this._getCollapsedNodeAtLine(this.cursorLine),onClosingLine:this._getCollapsedClosingLine(this.cursorLine)},i={Enter:()=>this._handleEnter(e.shiftKey,t),Backspace:()=>this._handleBackspace(t),Delete:()=>this._handleDelete(t),ArrowUp:()=>this._handleArrowKey(-1,0,e.shiftKey,e.ctrlKey||e.metaKey),ArrowDown:()=>this._handleArrowKey(1,0,e.shiftKey,e.ctrlKey||e.metaKey),ArrowLeft:()=>this._handleArrowKey(0,-1,e.shiftKey,e.ctrlKey||e.metaKey),ArrowRight:()=>this._handleArrowKey(0,1,e.shiftKey,e.ctrlKey||e.metaKey),Home:()=>this._handleHomeEnd("home",e.shiftKey,t.onClosingLine),End:()=>this._handleHomeEnd("end",e.shiftKey,t.onClosingLine),PageUp:()=>this._handlePageUpDown("up",e.shiftKey),PageDown:()=>this._handlePageUpDown("down",e.shiftKey),Tab:()=>this._handleTab(e.shiftKey,t),Insert:()=>{this._insertMode=!this._insertMode,this.scheduleRender()}},s={a:()=>this._selectAll(),z:()=>e.shiftKey?this.redo():this.undo(),y:()=>this.redo(),s:()=>this.save(),o:()=>!this.hasAttribute("readonly")&&this.open(),i:()=>this.internalAddShortcut&&!this.readonly&&this._handleAddFeaturePrompt()};if(i[e.key])return e.preventDefault(),e.stopPropagation(),void i[e.key]();(e.ctrlKey||e.metaKey)&&s[e.key]&&(e.preventDefault(),e.stopPropagation(),s[e.key]())}_handleEnter(e,t){if(e){const e=this._getContainingExpandedNode(this.cursorLine);if(e){const t=this.lines[e.startLine],i=t.search(R);this.toggleCollapse(e.nodeId),this.cursorLine=e.startLine,this.cursorColumn=i>=0?i+1:t.length,this._clearSelection(),this._scrollToCursor()}return}if(t.onCollapsedNode)this.toggleCollapse(t.onCollapsedNode.nodeId);else{if(t.onClosingLine){const e=this.lines[this.cursorLine],i=this._getClosingBracketPos(e);if(i>=0&&this.cursorColumn<=i)return void this.toggleCollapse(t.onClosingLine.nodeId)}t.inCollapsedZone}}_handleBackspace(e){if(this._hasSelection())return this._deleteSelection(),void this.formatAndUpdate();if(e.onClosingLine){const t=this.lines[this.cursorLine],i=this._getClosingBracketPos(t);return i>=0&&this.cursorColumn>i+1?void this.deleteBackward():void this._deleteCollapsedNode(e.onClosingLine)}if(e.onCollapsedNode&&0===this.cursorColumn)this._deleteCollapsedNode(e.onCollapsedNode);else if(!e.inCollapsedZone){if(e.onCollapsedNode){const t=this.lines[this.cursorLine].search(R);if(this.cursorColumn>t+1)return void this._deleteCollapsedNode(e.onCollapsedNode)}this.deleteBackward()}}_handleDelete(e){if(this._hasSelection())return this._deleteSelection(),void this.formatAndUpdate();if(e.onClosingLine){const t=this.lines[this.cursorLine],i=this._getClosingBracketPos(t);return i>=0&&this.cursorColumn>i?void this.deleteForward():void this._deleteCollapsedNode(e.onClosingLine)}if(e.onCollapsedNode){const t=this.lines[this.cursorLine].search(R);if(this.cursorColumn>t)return void this._deleteCollapsedNode(e.onCollapsedNode)}e.inCollapsedZone||this.deleteForward()}_handleTab(e,t){e?this._navigateToPrevAttribute():this._navigateToNextAttribute()}_navigateToNextAttribute(){const e=this.visibleLines.length;let t=this.visibleLines.findIndex(e=>e.index===this.cursorLine);t<0&&(t=0);for(let i=t;i<e;i++){const e=this.visibleLines[i],s=this.lines[e.index],n=i===t?this.cursorColumn:0,o=this._findNextAttributeOrBracket(s,n,e.index);if(null!==o)return this.cursorLine=e.index,this.cursorColumn=o.start,o.isBracket?this._clearSelection():(this.selectionStart={line:e.index,column:o.start},this.selectionEnd={line:e.index,column:o.end}),this._scrollToCursor(),this._invalidateRenderCache(),void this.scheduleRender()}for(let i=0;i<t;i++){const e=this.visibleLines[i],t=this.lines[e.index],s=this._findNextAttributeOrBracket(t,0,e.index);if(null!==s)return this.cursorLine=e.index,this.cursorColumn=s.start,s.isBracket?this._clearSelection():(this.selectionStart={line:e.index,column:s.start},this.selectionEnd={line:e.index,column:s.end}),this._scrollToCursor(),this._invalidateRenderCache(),void this.scheduleRender()}}_navigateToPrevAttribute(){const e=this.visibleLines.length;let t=this.visibleLines.findIndex(e=>e.index===this.cursorLine);t<0&&(t=e-1);for(let i=t;i>=0;i--){const e=this.visibleLines[i],s=this.lines[e.index],n=i===t?this.cursorColumn:s.length,o=this._findPrevAttributeOrBracket(s,n,e.index);if(null!==o)return this.cursorLine=e.index,this.cursorColumn=o.start,o.isBracket?this._clearSelection():(this.selectionStart={line:e.index,column:o.start},this.selectionEnd={line:e.index,column:o.end}),this._scrollToCursor(),this._invalidateRenderCache(),void this.scheduleRender()}for(let i=e-1;i>t;i--){const e=this.visibleLines[i],t=this.lines[e.index],s=this._findPrevAttributeOrBracket(t,t.length,e.index);if(null!==s)return this.cursorLine=e.index,this.cursorColumn=s.start,s.isBracket?this._clearSelection():(this.selectionStart={line:e.index,column:s.start},this.selectionEnd={line:e.index,column:s.end}),this._scrollToCursor(),this._invalidateRenderCache(),void this.scheduleRender()}}_findNextAttributeInLine(e,t){const i=[],s=/"([^"]+)"(?:\s*:\s*(?:"([^"]*)"|(-?\d+\.?\d*(?:e[+-]?\d+)?)|true|false|null))?/gi;let n;for(;null!==(n=s.exec(e));){const t=n.index+1,s=t+n[1].length;if(i.push({start:t,end:s}),void 0!==n[2]){const t=e.substring(n.index).match(/:\s*"([^"]*)"/);if(t){const e=n.index+(t.index||0)+t[0].indexOf('"')+1,s=e+n[2].length;i.push({start:e,end:s})}}else if(void 0!==n[3]){const t=e.substring(n.index).match(/:\s*(-?\d+\.?\d*(?:e[+-]?\d+)?)/i);if(t){const e=n.index+(t.index||0)+t[0].indexOf(t[1]),s=e+t[1].length;i.push({start:e,end:s})}}else{const t=e.substring(n.index).match(/:\s*(true|false|null)/);if(t){const e=n.index+(t.index||0)+t[0].indexOf(t[1]),s=e+t[1].length;i.push({start:e,end:s})}}}const o=/(?:^|[\[,\s])(-?\d+\.?\d*(?:e[+-]?\d+)?)\s*(?:[,\]]|$)/gi;for(;null!==(n=o.exec(e));){const e=n[1],t=n.index+n[0].indexOf(e),s=t+e.length;i.some(e=>e.start===t&&e.end===s)||i.push({start:t,end:s})}i.sort((e,t)=>e.start-t.start);for(const r of i)if(r.start>t)return r;return null}_findPrevAttributeInLine(e,t){const i=[],s=/"([^"]+)"(?:\s*:\s*(?:"([^"]*)"|(-?\d+\.?\d*(?:e[+-]?\d+)?)|true|false|null))?/gi;let n;for(;null!==(n=s.exec(e));){const t=n.index+1,s=t+n[1].length;if(i.push({start:t,end:s}),void 0!==n[2]){const t=e.substring(n.index).match(/:\s*"([^"]*)"/);if(t){const e=n.index+(t.index||0)+t[0].indexOf('"')+1,s=e+n[2].length;i.push({start:e,end:s})}}else if(void 0!==n[3]){const t=e.substring(n.index).match(/:\s*(-?\d+\.?\d*(?:e[+-]?\d+)?)/i);if(t){const e=n.index+(t.index||0)+t[0].indexOf(t[1]),s=e+t[1].length;i.push({start:e,end:s})}}else{const t=e.substring(n.index).match(/:\s*(true|false|null)/);if(t){const e=n.index+(t.index||0)+t[0].indexOf(t[1]),s=e+t[1].length;i.push({start:e,end:s})}}}const o=/(?:^|[\[,\s])(-?\d+\.?\d*(?:e[+-]?\d+)?)\s*(?:[,\]]|$)/gi;for(;null!==(n=o.exec(e));){const e=n[1],t=n.index+n[0].indexOf(e),s=t+e.length;i.some(e=>e.start===t&&e.end===s)||i.push({start:t,end:s})}i.sort((e,t)=>e.start-t.start);for(let r=i.length-1;r>=0;r--)if(i[r].end<t)return i[r];return null}_findBracketInLine(e){const t=e.match(/[\[{]\s*$/);return t&&void 0!==t.index?t.index+1:null}_findNextAttributeOrBracket(e,t,i){const s=this._findNextAttributeInLine(e,t),n=this._findBracketInLine(e);return null!==s&&null!==n?n>t&&n<s.start?{start:n,end:n,isBracket:!0}:{...s,isBracket:!1}:null!==s?{...s,isBracket:!1}:null!==n&&n>t?{start:n,end:n,isBracket:!0}:null}_findPrevAttributeOrBracket(e,t,i){const s=this._findPrevAttributeInLine(e,t),n=this._findBracketInLine(e);return null!==s&&null!==n?n<t&&n>s.end?{start:n,end:n,isBracket:!0}:{...s,isBracket:!1}:null!==s?{...s,isBracket:!1}:null!==n&&n<t?{start:n,end:n,isBracket:!0}:null}insertNewline(){if(this._saveToHistory("newline"),this.cursorLine<this.lines.length){const e=this.lines[this.cursorLine],t=e.substring(0,this.cursorColumn),i=e.substring(this.cursorColumn);this.lines[this.cursorLine]=t,this.lines.splice(this.cursorLine+1,0,i),this.cursorLine++,this.cursorColumn=0}else this.lines.push(""),this.cursorLine=this.lines.length-1,this.cursorColumn=0;this.formatAndUpdate()}deleteBackward(){if(this._saveToHistory("delete"),this.cursorColumn>0){const e=this.lines[this.cursorLine];this.lines[this.cursorLine]=e.substring(0,this.cursorColumn-1)+e.substring(this.cursorColumn),this.cursorColumn--}else if(this.cursorLine>0){const e=this.lines[this.cursorLine],t=this.lines[this.cursorLine-1];this.cursorColumn=t.length,this.lines[this.cursorLine-1]=t+e,this.lines.splice(this.cursorLine,1),this.cursorLine--}this.formatAndUpdate()}deleteForward(){if(this._saveToHistory("delete"),this.cursorLine<this.lines.length){const e=this.lines[this.cursorLine];this.cursorColumn<e.length?this.lines[this.cursorLine]=e.substring(0,this.cursorColumn)+e.substring(this.cursorColumn+1):this.cursorLine<this.lines.length-1&&(this.lines[this.cursorLine]=e+this.lines[this.cursorLine+1],this.lines.splice(this.cursorLine+1,1))}this.formatAndUpdate()}moveCursorSkipCollapsed(e){let t=this.cursorLine+e;for(;t>=0&&t<this.lines.length;){const i=this._getCollapsedRangeForLine(t);if(!i)break;t=e>0?i.endLine:i.startLine}this.cursorLine=Math.max(0,Math.min(this.lines.length-1,t));const i=this.lines[this.cursorLine]?.length||0;this.cursorColumn=Math.min(this.cursorColumn,i),this._invalidateRenderCache(),this._scrollToCursor(),this.scheduleRender()}moveCursorHorizontal(e){e>0?this._moveCursorRight():this._moveCursorLeft(),this._invalidateRenderCache(),this._scrollToCursor(),this.scheduleRender()}_moveCursorRight(){const e=this.lines[this.cursorLine],t=this._getCollapsedNodeAtLine(this.cursorLine);if(this._getCollapsedClosingLine(this.cursorLine)){const t=this._getClosingBracketPos(e);this.cursorColumn<t?this.cursorColumn=t:this.cursorColumn>=e.length?this.cursorLine<this.lines.length-1&&(this.cursorLine++,this.cursorColumn=0):this.cursorColumn++}else if(t){const i=e.search(R);this.cursorColumn<i?this.cursorColumn++:this.cursorColumn===i?this.cursorColumn=i+1:(this.cursorLine=t.endLine,this.cursorColumn=this._getClosingBracketPos(this.lines[this.cursorLine]))}else if(this.cursorColumn>=e.length){if(this.cursorLine<this.lines.length-1){this.cursorLine++,this.cursorColumn=0;const e=this._getCollapsedRangeForLine(this.cursorLine);e&&(this.cursorLine=e.endLine,this.cursorColumn=0)}}else this.cursorColumn++}_moveCursorLeft(){const e=this.lines[this.cursorLine],t=this._getCollapsedNodeAtLine(this.cursorLine),i=this._getCollapsedClosingLine(this.cursorLine);if(i){const t=this._getClosingBracketPos(e);if(this.cursorColumn>t+1)this.cursorColumn--;else{this.cursorLine=i.startLine;const e=this.lines[this.cursorLine];this.cursorColumn=e.search(R)+1}}else if(t){const t=e.search(R);this.cursorColumn>t+1?this.cursorColumn=t+1:this.cursorColumn===t+1?this.cursorColumn=t:this.cursorColumn>0?this.cursorColumn--:this.cursorLine>0&&(this.cursorLine--,this.cursorColumn=this.lines[this.cursorLine]?.length||0)}else if(this.cursorColumn>0)this.cursorColumn--;else if(this.cursorLine>0)if(this.cursorLine--,this._getCollapsedClosingLine(this.cursorLine))this.cursorColumn=this.lines[this.cursorLine]?.length||0;else{const e=this._getCollapsedRangeForLine(this.cursorLine);if(e){this.cursorLine=e.startLine;const t=this.lines[this.cursorLine];this.cursorColumn=t.search(R)+1}else this.cursorColumn=this.lines[this.cursorLine]?.length||0}}_scrollToCursor(e=!1){const t=this._viewport;if(!t)return;const i=this.visibleLines.findIndex(e=>e.index===this.cursorLine);if(-1===i)return;const s=i*this.lineHeight,n=t.clientHeight;if(e)t.scrollTop=Math.max(0,s-n/2+this.lineHeight/2);else{const e=t.scrollTop,i=e+n;s<e?t.scrollTop=s:s+this.lineHeight>i&&(t.scrollTop=s+this.lineHeight-n)}}_handleArrowKey(e,t,i,s=!1){i&&!this.selectionStart&&(this.selectionStart={line:this.cursorLine,column:this.cursorColumn}),0!==e?this.moveCursorSkipCollapsed(e):0!==t&&(s?this._moveCursorByWord(t):this.moveCursorHorizontal(t)),i?this.selectionEnd={line:this.cursorLine,column:this.cursorColumn}:(this.selectionStart=null,this.selectionEnd=null)}_moveCursorByWord(e){const t=this.lines[this.cursorLine]||"",i=e=>N.test(e),s=this._getCollapsedNodeAtLine(this.cursorLine);if(e>0){let e=this.cursorColumn;if(s){const i=t.search(R);if(i>=0&&e>=i)return this.cursorLine=s.endLine,this.cursorColumn=(this.lines[this.cursorLine]||"").length,this._invalidateRenderCache(),this._scrollToCursor(),void this.scheduleRender()}if(e>=t.length){if(this.cursorLine<this.lines.length-1){let e=this.cursorLine+1;const t=this._getCollapsedRangeForLine(e);t&&(e=t.endLine),this.cursorLine=Math.min(e,this.lines.length-1),this.cursorColumn=0}}else if(i(t[e])){for(;e<t.length&&i(t[e]);)e++;this.cursorColumn=e}else{for(;e<t.length&&!i(t[e]);)e++;this.cursorColumn=e}}else{let e=this.cursorColumn;const s=this._getCollapsedClosingLine(this.cursorLine);if(s){const i=this._getClosingBracketPos(t);if(i>=0&&e<=i+1){this.cursorLine=s.startLine;const e=(this.lines[this.cursorLine]||"").search(R);return this.cursorColumn=e>=0?e:0,this._invalidateRenderCache(),this._scrollToCursor(),void this.scheduleRender()}}if(0===e){if(this.cursorLine>0){let e=this.cursorLine-1;const t=this._getCollapsedRangeForLine(e);t&&(e=t.startLine),this.cursorLine=Math.max(e,0),this.cursorColumn=this.lines[this.cursorLine].length}}else if(e>0&&i(t[e-1])){for(;e>0&&i(t[e-1]);)e--;this.cursorColumn=e}else{for(;e>0&&!i(t[e-1]);)e--;for(;e>0&&i(t[e-1]);)e--;this.cursorColumn=e}}this._invalidateRenderCache(),this._scrollToCursor(),this.scheduleRender()}_handleHomeEnd(e,t,i){if(t&&!this.selectionStart&&(this.selectionStart={line:this.cursorLine,column:this.cursorColumn}),"home"===e)i?(this.cursorLine=i.startLine,this.cursorColumn=0):0===this.cursorColumn?(this.cursorLine=0,this.cursorColumn=0):this.cursorColumn=0;else{const e=this.lines[this.cursorLine]?.length||0;this.cursorColumn===e?(this.cursorLine=this.lines.length-1,this.cursorColumn=this.lines[this.cursorLine]?.length||0):this.cursorColumn=e}t?this.selectionEnd={line:this.cursorLine,column:this.cursorColumn}:(this.selectionStart=null,this.selectionEnd=null),this._invalidateRenderCache(),this._scrollToCursor(),this.scheduleRender()}_handlePageUpDown(e,t){t&&!this.selectionStart&&(this.selectionStart={line:this.cursorLine,column:this.cursorColumn});const i=this._viewport;if(!i)return;const s=Math.floor(i.clientHeight/this.lineHeight);if("up"===e){const e=this.visibleLines.findIndex(e=>e.index===this.cursorLine),t=Math.max(0,e-s);this.cursorLine=this.visibleLines[t]?.index||0}else{const e=this.visibleLines.findIndex(e=>e.index===this.cursorLine),t=Math.min(this.visibleLines.length-1,e+s);this.cursorLine=this.visibleLines[t]?.index||this.lines.length-1}this.cursorColumn=Math.min(this.cursorColumn,this.lines[this.cursorLine]?.length||0),t?this.selectionEnd={line:this.cursorLine,column:this.cursorColumn}:(this.selectionStart=null,this.selectionEnd=null),this._invalidateRenderCache(),this._scrollToCursor(),this.scheduleRender()}_selectAll(){this.selectionStart={line:0,column:0};const e=this.lines.length-1;this.selectionEnd={line:e,column:this.lines[e]?.length||0},this.cursorLine=e,this.cursorColumn=this.lines[e]?.length||0,this._invalidateRenderCache(),this.scheduleRender()}_getSelectedText(){const e=this._normalizeSelection();if(!e)return"";const{start:t,end:i}=e;if(t.line===i.line)return this.lines[t.line].substring(t.column,i.column);let s=this.lines[t.line].substring(t.column)+"\n";for(let n=t.line+1;n<i.line;n++)s+=this.lines[n]+"\n";return s+=this.lines[i.line].substring(0,i.column),s}_normalizeSelection(){if(!this.selectionStart||!this.selectionEnd)return null;const e=this.selectionStart,t=this.selectionEnd;return e.line<t.line||e.line===t.line&&e.column<=t.column?{start:e,end:t}:{start:t,end:e}}_hasSelection(){return!(!this.selectionStart||!this.selectionEnd||this.selectionStart.line===this.selectionEnd.line&&this.selectionStart.column===this.selectionEnd.column)}_clearSelection(){this.selectionStart=null,this.selectionEnd=null}_selectWordAt(e,t){if(e<0||e>=this.lines.length)return;const i=this.lines[e];if(!i||t>i.length)return;const s=e=>/[\w\-]/.test(e);let n=!1,o=-1,r=-1,l=!1;for(let c=0;c<i.length;c++){const s=i[c];if(l)l=!1;else if("\\"!==s){if('"'===s)if(n){if(r=c,t>=o&&t<=r)return this.selectionStart={line:e,column:o+1},this.selectionEnd={line:e,column:r},this.cursorLine=e,void(this.cursorColumn=r);n=!1,o=-1,r=-1}else n=!0,o=c}else l=!0}let a=t,h=t;for(;a>0&&s(i[a-1]);)a--;for(;h<i.length&&s(i[h]);)h++;a<h&&(this.selectionStart={line:e,column:a},this.selectionEnd={line:e,column:h},this.cursorLine=e,this.cursorColumn=h)}_deleteSelection(){const e=this._normalizeSelection();if(!e)return!1;const{start:t,end:i}=e;if(this._saveToHistory("delete"),t.line===i.line){const e=this.lines[t.line];this.lines[t.line]=e.substring(0,t.column)+e.substring(i.column)}else{const e=this.lines[t.line].substring(0,t.column),s=this.lines[i.line].substring(i.column);this.lines[t.line]=e+s,this.lines.splice(t.line+1,i.line-t.line)}return this.cursorLine=t.line,this.cursorColumn=t.column,this.selectionStart=null,this.selectionEnd=null,!0}insertText(e){if(this._hasSelection()&&this._deleteSelection(),!this._getCollapsedRangeForLine(this.cursorLine)){if(this._getCollapsedClosingLine(this.cursorLine)){const e=this.lines[this.cursorLine],t=this._getClosingBracketPos(e);if(this.cursorColumn<=t)return}if(this._getCollapsedNodeAtLine(this.cursorLine)){const e=this.lines[this.cursorLine].search(R);if(this.cursorColumn>e)return}if(this._saveToHistory("insert"),0===this.lines.length){const t=e.split("\n");this.lines=t,this.cursorLine=t.length-1,this.cursorColumn=t[t.length-1].length}else if(this.cursorLine<this.lines.length){const t=e.split("\n"),i=this.lines[this.cursorLine],s=i.substring(0,this.cursorColumn),n=i.substring(this.cursorColumn);if(1===t.length)this.lines[this.cursorLine]=s+e+n,this.cursorColumn+=e.length;else{const e=s+t[0],i=t[t.length-1]+n,o=t.slice(1,-1);this.lines.splice(this.cursorLine,1,e,...o,i),this.cursorLine+=t.length-1,this.cursorColumn=t[t.length-1].length}}this.formatAndUpdate()}}handlePaste(e){e.preventDefault();const t=e.clipboardData?.getData("text/plain");if(!t)return;const i=0===this.lines.length;try{const e=H(JSON.parse(t)).map(e=>JSON.stringify(e,null,2)).join(",\n");this.insertText(e)}catch{this.insertText(t)}this.renderTimer&&(cancelAnimationFrame(this.renderTimer),this.renderTimer=void 0),i&&this.lines.length>0&&this.autoCollapseCoordinates(),this._expandErrorNodes(),this.renderViewport()}handleCopy(e){e.preventDefault(),e.clipboardData&&(this._hasSelection()?e.clipboardData.setData("text/plain",this._getSelectedText()):e.clipboardData.setData("text/plain",this.getContent()))}handleCut(e){e.preventDefault(),e.clipboardData&&(this._hasSelection()?(e.clipboardData.setData("text/plain",this._getSelectedText()),this._saveToHistory("cut"),this._deleteSelection(),this.formatAndUpdate()):(e.clipboardData.setData("text/plain",this.getContent()),this._saveToHistory("cut"),this.lines=[],this.cursorLine=0,this.cursorColumn=0,this.formatAndUpdate()))}_getPositionFromClick(e){const t=this._viewport,i=this._linesContainer;if(!t)return{line:0,column:0};const s=t.getBoundingClientRect(),n=e.clientY-s.top+t.scrollTop-8,o=Math.floor(n/this.lineHeight);let r=0,l=0;if(o>=0&&o<this.visibleLines.length){const n=this.visibleLines[o];r=n.index;const a=i?.querySelector(`.line[data-line-index="${n.index}"]`),h=this._getCharWidth();if(a){const t=a.getBoundingClientRect(),i=e.clientX-t.left,s=Math.round(i/h),o=n.content?.length||0;l=Math.max(0,Math.min(s,o))}else{const i=12,o=e.clientX-s.left+t.scrollLeft-i,r=Math.round(o/h),a=n.content?.length||0;l=Math.max(0,Math.min(r,a))}}return{line:r,column:l}}_updateSelectionFromScroll(e){if(this.visibleLines.length)if("up"===e){const e=this.visibleLines[0];this.selectionEnd={line:e.index,column:0},this.cursorLine=e.index,this.cursorColumn=0}else{const e=this.visibleLines[this.visibleLines.length-1],t=e.content?.length||0;this.selectionEnd={line:e.index,column:t},this.cursorLine=e.index,this.cursorColumn=t}}handleGutterClick(e){const t=e.target;if(!t)return;const i=t.closest(".visibility-button");if(i)this.toggleFeatureVisibility(i.dataset.featureKey);else if(t.classList.contains("collapse-button")){const e=t.dataset.nodeId;return void(e&&this.toggleCollapse(e))}}handleEditorClick(e){const t=e.target;if(!t)return;this._blockRender=!1;const i=t.closest(".line.has-visibility");if(i){const t=i.getBoundingClientRect();if(e.clientX-t.left<14){e.preventDefault(),e.stopPropagation();const t=i.dataset.featureKey;return void(t&&this.toggleFeatureVisibility(t))}}if(t.classList.contains("json-color")){const i=t.getBoundingClientRect(),s=e.clientX-i.left;if(s<0&&s>=-8){e.preventDefault(),e.stopPropagation();const i=t.dataset.color,s=t.closest(".line");if(s){const e=parseInt(s.dataset.lineIndex||"0"),n=this.lines[e].match(k);n&&n[1]&&i&&this.showColorPicker(t,e,i,n[1])}return}}if(t.classList.contains("json-boolean")){const i=t.getBoundingClientRect(),s=e.clientX-i.left;if(s<0&&s>=-8){e.preventDefault(),e.stopPropagation();const i=t.closest(".line");if(i){const e=parseInt(i.dataset.lineIndex||"0"),t=this.lines[e].match(S);if(t){const i="true"===t[2];this.updateBooleanValue(e,!i,t[1])}}return}}}toggleCollapse(e){const t=this._nodeIdToLines.get(e);this.collapsedNodes.has(e)?(this.collapsedNodes.delete(e),t?.uniqueKey&&this._openedNodeKeys.add(t.uniqueKey)):(this.collapsedNodes.add(e),t?.uniqueKey&&this._openedNodeKeys.delete(t.uniqueKey)),this.updateView(),this._invalidateRenderCache(),this.scheduleRender()}autoCollapseCoordinates(){this._hasErrors()||this._applyCollapsedOption(["coordinates"])}_hasErrors(){try{const e="["+this.lines.join("\n")+"]";JSON.parse(e)}catch{return!0}for(const e of this.lines)if(V(e,"",void 0).includes("json-error"))return!0;return!1}_applyCollapsedFromOptions(e,t){if(this._hasErrors())return;const i=void 0!==e.collapsed?e.collapsed:["coordinates"];i&&(!Array.isArray(i)||i.length>0)&&this._applyCollapsedOption(i,t)}_applyCollapsedOption(e,t=null){const i=this._findCollapsibleRanges(),s=i.filter(e=>e.isRootFeature);for(const n of i){let i=!1;if("function"==typeof e){const o=s.findIndex(e=>n.startLine>=e.startLine&&n.endLine<=e.endLine),r=e(t?.[o]||null,o);i=n.isRootFeature?r.includes("$root"):r.includes(n.nodeKey)}else Array.isArray(e)&&(i=n.isRootFeature?e.includes("$root"):e.includes(n.nodeKey));i&&this.collapsedNodes.add(n.nodeId)}this.updateModel(),this.scheduleRender()}toggleFeatureVisibility(e){e&&(this.hiddenFeatures.has(e)?this.hiddenFeatures.delete(e):this.hiddenFeatures.add(e),this.updateView(),this.scheduleRender(),this.emitChange())}showColorPicker(e,t,i,s){const n=document.querySelector(".geojson-color-picker-anchor");n&&n.remove();const o=z("div");o.className="geojson-color-picker-anchor";const r=e.getBoundingClientRect();o.style.cssText=`\n position: fixed;\n left: ${r.left-8}px;\n top: ${r.top+r.height}px;\n width: 10px;\n height: 10px;\n z-index: 9998;\n `,document.body.appendChild(o);const l=z("input");l.type="color";let a=i;a=i.startsWith("#")?i.replace(L,"#$1$1$2$2$3$3"):function(e){const t=(O||(O=document.createElement("div"),O.style.display="none",document.body.appendChild(O)),O);t.style.color=e;const i=getComputedStyle(t).color;if(!i)return null;const s=i.match(/rgba?\((\d+),\s*(\d+),\s*(\d+)/);if(!s)return null;const[,n,o,r]=s;return"#"+parseInt(n,10).toString(16).padStart(2,"0")+parseInt(o,10).toString(16).padStart(2,"0")+parseInt(r,10).toString(16).padStart(2,"0")}(i)||"#000000",l.value=a,l.className="geojson-color-picker-input",l.style.cssText="\n position: absolute;\n left: 0;\n top: 0;\n width: 10px;\n height: 10px;\n opacity: 0;\n border: none;\n padding: 0;\n cursor: pointer;\n ",o.appendChild(l),l.addEventListener("input",e=>{this.updateColorValue(t,e.target.value,s)});const h=e=>{e.target!==l&&(document.removeEventListener("click",h,!0),o.remove())};l._closeListener=h,setTimeout(()=>{document.addEventListener("click",h,!0)},100),l.focus(),l.click()}updateColorValue(e,t,i){const s=new RegExp(`"${i}"\\s*:\\s*"(?:#[0-9a-fA-F]{3,6}|[a-zA-Z]+)"`);this.lines[e]=this.lines[e].replace(s,`"${i}": "${t}"`),this.updateView(),this.scheduleRender(),this.emitChange()}updateBooleanValue(e,t,i){const s=new RegExp(`"${i}"\\s*:\\s*(true|false)`);this.lines[e]=this.lines[e].replace(s,`"${i}": ${t}`),this.updateView(),this.scheduleRender(),this.emitChange()}_bestEffortFormat(e,t){const i=e.split("\n");if(void 0!==t&&t>=0&&t<i.length){const e=i[t],s=i.slice(0,t).join("\n"),n=s.trim()?this._formatChunk(s):[],o=this._computeDepthAtEnd(n)+this._computeBracketDelta(e),r=i.slice(t+1).join("\n"),l=r.trim()?this._formatChunk(r,o):[];return[...n,e,...l]}return this._formatChunk(e)}_computeBracketDelta(e){let t=0,i=!1,s=!1;for(const n of e)s?s=!1:"\\"===n&&i?s=!0:'"'!==n?i||("{"===n||"["===n?t++:"}"!==n&&"]"!==n||t--):i=!i;return t}_computeDepthAtEnd(e){let t=1;for(const i of e)for(const e of i)"{"===e||"["===e?t++:"}"!==e&&"]"!==e||(t=Math.max(0,t-1));return t}_formatChunk(e,t=1){const i=[];let s="",n=t,o=!1,r=!1;for(let l=0;l<e.length;l++){const t=e[l];if(r)s+=t,r=!1;else if("\\"===t&&o)s+=t,r=!0;else if('"'!==t)if(o)s+=t;else if("{"===t||"["===t)s+=t,i.push(" ".repeat(n)+s.trim()),n++,s="";else if("}"===t||"]"===t)s.trim()&&i.push(" ".repeat(n)+s.trim()),n=Math.max(0,n-1),s=t;else if(","===t)s+=t,i.push(" ".repeat(n)+s.trim()),s="";else if(":"===t){if(s+=": ",l++," "===e[l])continue;l--}else{if("\n"===t||"\r"===t)continue;s+=t}else o=!o,s+=t}return s.trim()&&i.push(" ".repeat(n)+s.trim()),i}formatAndUpdate(){const e=this.cursorLine,t=this.cursorColumn,i=this.lines.join("\n");try{const e="["+i+"]",t=JSON.parse(e),s=JSON.stringify(t,null,2).split("\n");this.lines=s.slice(1,-1)}catch{if(i.trim()){const t=(this.lines[e]||"").length<80?e:void 0,s=i.split("\n"),n=void 0!==t?this._formatChunk(s.slice(0,t).join("\n")).length:0;this.lines=this._bestEffortFormat(i,t),void 0!==t&&n>=0&&(this.cursorLine=n)}}if(this.lines.join("\n")===i)this.cursorLine=e,this.cursorColumn=t;else if(0===t)this.cursorColumn=0;else{const s=i.split("\n");let n=0;for(let t=0;t<e&&t<s.length;t++)n+=s[t].replace(/\s/g,"").length;n+=(s[e]||"").substring(0,t).replace(/\s/g,"").length;let o=0,r=0,l=0;for(let e=0;e<this.lines.length;e++){const t=this.lines[e];for(let i=0;i<=t.length;i++){if(o>=n){r=e,l=i;break}i<t.length&&!/\s/.test(t[i])&&o++}if(o>=n)break}this.cursorLine=r,this.cursorColumn=l}this.cursorLine=Math.min(this.cursorLine,Math.max(0,this.lines.length-1)),this.cursorColumn=Math.min(this.cursorColumn,this.lines[this.cursorLine]?.length||0),this.updateModel(),this._expandErrorNodes(),this.scheduleRender(),this.updatePlaceholderVisibility(),this._updateErrorDisplay(),this.emitChange()}emitChange(){const e=this.getContent(),i=this.prefix+e+this.suffix;try{let s=JSON.parse(i);this.hiddenFeatures.size>0&&(s.features=s.features.filter(e=>{const t=M(e);return!t||!this.hiddenFeatures.has(t)}));const n=function(e){const i=[];return e.features?(e.features.forEach((e,s)=>{"Feature"!==e.type&&i.push(`features[${s}]: type must be "Feature"`),e.geometry&&e.geometry.type&&(t.includes(e.geometry.type)||i.push(`features[${s}].geometry: invalid type "${e.geometry.type}"`))}),i):i}(s);n.length>0?this.dispatchEvent(new CustomEvent("error",{detail:{error:n.join("; "),errors:n,content:e},bubbles:!0,composed:!0})):this.dispatchEvent(new CustomEvent("change",{detail:s,bubbles:!0,composed:!0}))}catch(s){this.dispatchEvent(new CustomEvent("error",{detail:{error:s instanceof Error?s.message:"Unknown error",content:e},bubbles:!0,composed:!0}))}}updateReadonly(){this._hiddenTextarea&&(this._hiddenTextarea.readOnly=this.readonly),this._clearBtn&&(this._clearBtn.hidden=this.readonly)}updatePlaceholderVisibility(){this._placeholderLayer&&(this._placeholderLayer.style.display=this.lines.length>0?"none":"block")}_updateErrorDisplay(){const e=this._getErrorLines().length;this._errorNav&&this._errorNav.classList.toggle("visible",e>0),this._errorCount&&(this._errorCount.textContent=e>0?String(e):"")}updatePlaceholderContent(){this._placeholderLayer&&(this._placeholderLayer.textContent=this.placeholder),this.updatePlaceholderVisibility()}updatePrefixSuffix(){this._editorPrefix&&(this._editorPrefix.textContent=this.prefix),this._editorSuffix&&(this._editorSuffix.textContent=this.suffix)}updateThemeCSS(){const e=(t=this.getAttribute("dark-selector")||".dark").startsWith(".")&&!t.includes(" ")?`:host(${t})`:`:host-context(${t})`;var t;let i=this._id("theme-styles");i||(i=z("style"),i.id="theme-styles",this.shadowRoot.insertBefore(i,this.shadowRoot.firstChild)),F.lastIndex=0;const s=e=>Object.entries(e).filter(e=>void 0!==e[1]).map(([e,t])=>{return`--${i=e,i.replace(F,"-$1").toLowerCase()}: ${t};`;var i}).join("\n "),n=s(this.themes.light||{});let o=n?`:host {\n ${n}\n }\n`:"";o+=`${e} {\n ${s({bgColor:"#2b2b2b",textColor:"#a9b7c6",caretColor:"#bbb",gutterBg:"#313335",gutterBorder:"#3c3f41",gutterText:"#606366",jsonKey:"#9876aa",jsonString:"#6a8759",jsonNumber:"#6897bb",jsonBoolean:"#cc7832",jsonNull:"#cc7832",jsonPunct:"#a9b7c6",jsonError:"#ff6b68",controlColor:"#cc7832",controlBg:"#3c3f41",controlBorder:"#5a5a5a",geojsonKey:"#9876aa",geojsonType:"#6a8759",geojsonTypeInvalid:"#ff6b68",jsonKeyInvalid:"#ff6b68",...this.themes.dark})}\n }`,i.textContent=o}setTheme(e){e.dark&&(this.themes.dark={...this.themes.dark,...e.dark}),e.light&&(this.themes.light={...this.themes.light,...e.light}),this.updateThemeCSS()}resetTheme(){this.themes={dark:{},light:{}},this.updateThemeCSS()}getTheme(){return{...this.themes}}_findCollapsibleRanges(){const e=[];for(const[t,i]of this._lineToNodeId){const s=this._nodeIdToLines.get(i);if(!s)continue;const n=this.lines[t];if(!n)continue;const o=n.match(E),r=!o&&n.match(j);if(!o&&!r)continue;const l=o?o[2]:r?r[1]:"{";e.push({startLine:s.startLine,endLine:s.endLine,nodeKey:s.nodeKey||(o?o[1]:`__root_${t}`),nodeId:i,openBracket:l,isRootFeature:!!r})}return e.sort((e,t)=>e.startLine-t.startLine),e}_findClosingLine(e,t){let i=1;const s=this.lines[e],n=s.indexOf(t);if(-1!==n){const o=K(s.substring(n+1),t);if(i+=o.open-o.close,0===i)return e}for(let o=e+1;o<this.lines.length;o++){const e=K(this.lines[o],t);if(i+=e.open-e.close,0===i)return o}return-1}_buildContextMap(){const e=this.lines.length;if(this._contextMapCache&&this._contextMapLinesLength===e&&this._contextMapFirstLine===this.lines[0]&&this._contextMapLastLine===this.lines[e-1])return this._contextMapCache;const t=/* @__PURE__ */new Map,o=[];let r=null;for(let l=0;l<e;l++){const e=this.lines[l],a=o[o.length-1]?.context||"Feature";t.set(l,a),i.test(e)?r="geometry":s.test(e)?r="properties":n.test(e)&&(r="Feature"),I.lastIndex=0,$.lastIndex=0,A.lastIndex=0,B.lastIndex=0;const h=(e.match(I)||[]).length,c=(e.match($)||[]).length,d=(e.match(A)||[]).length,u=(e.match(B)||[]).length;for(let t=0;t<h+d;t++)o.push({context:r||a,isArray:t>=h}),r=null;for(let t=0;t<c+u&&o.length>0;t++)o.pop()}return this._contextMapCache=t,this._contextMapLinesLength=e,this._contextMapFirstLine=this.lines[0],this._contextMapLastLine=this.lines[e-1],t}set(e,t={}){const i=H(e);this._setFeaturesInternal(i,t)}add(e,t={}){const i=H(e),s=this._parseFeatures().length,n=[...this._parseFeatures(),...i];this._setFeaturesInternalPreserving(n,t,s)}insertAt(e,t,i={}){const s=H(e),n=this._parseFeatures(),o=t<0?n.length+t:t,r=Math.max(0,Math.min(o,n.length));n.splice(r,0,...s),this._setFeaturesInternalPreserving(n,i,r,s.length)}_setFeaturesInternal(e,t){const i=e.map(e=>JSON.stringify(e,null,2)).join(",\n");this.setValue(i,!1),this._applyCollapsedFromOptions(t,e)}_setFeaturesInternalPreserving(e,t,i,s){const n=/* @__PURE__ */new Set;for(const h of this.collapsedNodes){const e=this._nodeIdToLines.get(h);if(e?.nodeKey){const t=this._getFeatureIndexForLine(e.startLine);n.add(`${t}:${e.nodeKey}`)}}const o=e.map(e=>JSON.stringify(e,null,2)).join(",\n");this.setValue(o,!1);const r=this._findCollapsibleRanges(),l=r.filter(e=>e.isRootFeature),a=void 0!==s?s:e.length-i;for(const h of r){const e=l.findIndex(e=>h.startLine>=e.startLine&&h.endLine<=e.endLine);let t=e;if(e>=i&&e<i+a)continue;e>=i+a&&(t=e-a);const s=`${t}:${h.nodeKey}`;n.has(s)&&this.collapsedNodes.add(h.nodeId)}this._applyCollapsedToNewFeatures(t,e,i,a),this.updateView(),this.scheduleRender()}_applyCollapsedToNewFeatures(e,t,i,s){const n=void 0!==e.collapsed?e.collapsed:["coordinates"];if(!n||Array.isArray(n)&&0===n.length)return;const o=this._findCollapsibleRanges(),r=o.filter(e=>e.isRootFeature);for(const l of o){const e=r.findIndex(e=>l.startLine>=e.startLine&&l.endLine<=e.endLine);if(e<i||e>=i+s)continue;let o=!1;if("function"==typeof n){const i=n(t[e],e);o=l.isRootFeature?i.includes("$root"):i.includes(l.nodeKey)}else Array.isArray(n)&&(o=l.isRootFeature?n.includes("$root"):n.includes(l.nodeKey));o&&this.collapsedNodes.add(l.nodeId)}}_getFeatureIndexForLine(e){for(const[,t]of this.featureRanges)if(e>=t.startLine&&e<=t.endLine)return t.featureIndex;return-1}removeAt(e){const t=this._parseFeatures(),i=e<0?t.length+e:e;if(i>=0&&i<t.length){const e=/* @__PURE__ */new Set;for(const t of this.collapsedNodes){const s=this._nodeIdToLines.get(t);if(s?.nodeKey){const t=this._getFeatureIndexForLine(s.startLine);if(t===i)continue;const n=t>i?t-1:t;e.add(`${n}:${s.nodeKey}`)}}const s=t.splice(i,1)[0],n=t.map(e=>JSON.stringify(e,null,2)).join(",\n");this.setValue(n,!1);const o=this._findCollapsibleRanges(),r=o.filter(e=>e.isRootFeature);for(const t of o){const i=`${r.findIndex(e=>t.startLine>=e.startLine&&t.endLine<=e.endLine)}:${t.nodeKey}`;e.has(i)&&this.collapsedNodes.add(t.nodeId)}return this.updateView(),this.scheduleRender(),this.emitChange(),s}}removeAll(){this.lines.length>0&&this._saveToHistory("removeAll");const e=this._parseFeatures();return this.lines=[],this.collapsedNodes.clear(),this.hiddenFeatures.clear(),this.updateModel(),this.scheduleRender(),this.updatePlaceholderVisibility(),this.emitChange(),e}get(e){const t=this._parseFeatures();return t[e<0?t.length+e:e]}getAll(){return this._parseFeatures()}emit(){this.emitChange()}save(e="features.geojson"){try{const t={type:"FeatureCollection",features:this._parseFeatures()},i=JSON.stringify(t,null,2),s=new Blob([i],{type:"application/geo+json"}),n=URL.createObjectURL(s),o=z("a");return o.href=n,o.download=e,document.body.appendChild(o),o.click(),document.body.removeChild(o),URL.revokeObjectURL(n),!0}catch(t){return!1}}open(e={}){return new Promise(t=>{const i=z("input");i.type="file",i.accept=".geojson,.json,application/geo+json,application/json",i.style.display="none",i.addEventListener("change",s=>{const n=s.target.files?.[0];if(!n)return document.body.removeChild(i),void t(!1);const o=new FileReader;o.onload=s=>{try{const n=s.target?.result,o=H(JSON.parse(n));this._saveToHistory("open"),this.set(o,e),this.clearHistory(),document.body.removeChild(i),t(!0)}catch(n){document.body.removeChild(i),t(!1)}},o.onerror=()=>{document.body.removeChild(i),t(!1)},o.readAsText(n)}),i.addEventListener("cancel",()=>{document.body.removeChild(i),t(!1)}),document.body.appendChild(i),i.click()})}_handleAddFeaturePrompt(){const e=prompt("Enter GeoJSON (Feature, Feature[], or FeatureCollection):");if(e&&e.trim())try{const t=H(JSON.parse(e));t.length>0&&this.add(t)}catch{}}_parseFeatures(){try{const e=this.lines.join("\n");return e.trim()?JSON.parse("["+e+"]"):[]}catch(e){return[]}}}customElements.get("geojson-editor")||customElements.define("geojson-editor",q);export{q as default};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@softwarity/geojson-editor",
3
- "version": "1.0.20",
3
+ "version": "1.0.22",
4
4
  "description": "A feature-rich GeoJSON editor Web Component with syntax highlighting, collapsible nodes, and color picker",
5
5
  "type": "module",
6
6
  "main": "./dist/geojson-editor.js",
@@ -332,6 +332,51 @@
332
332
  }
333
333
  .info-btn:hover { opacity: 0.5; }
334
334
 
335
+ /* ========== Info Popup ========== */
336
+ .info-popup {
337
+ display: none;
338
+ position: absolute;
339
+ top: 2rem;
340
+ right: 0.5rem;
341
+ z-index: 1000;
342
+ background: var(--bg-color, #ffffff);
343
+ border: 1px solid var(--gutter-border, #e0e0e0);
344
+ border-radius: 6px;
345
+ box-shadow: 0 4px 12px rgba(0,0,0,0.15);
346
+ padding: 12px 16px;
347
+ min-width: 200px;
348
+ font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
349
+ }
350
+ .info-popup.visible {
351
+ display: block;
352
+ }
353
+ .info-popup-content {
354
+ text-align: center;
355
+ }
356
+ .info-popup-title {
357
+ font-weight: bold;
358
+ font-size: 13px;
359
+ color: var(--text-color, #000000);
360
+ margin-bottom: 4px;
361
+ }
362
+ .info-popup-version {
363
+ display: block;
364
+ font-size: 11px;
365
+ color: var(--text-color, #000000);
366
+ opacity: 0.6;
367
+ margin-bottom: 8px;
368
+ text-decoration: none;
369
+ }
370
+ .info-popup-version:hover {
371
+ opacity: 1;
372
+ text-decoration: underline !important;
373
+ }
374
+ .info-popup-copyright {
375
+ font-size: 10px;
376
+ color: var(--text-color, #000000);
377
+ opacity: 0.5;
378
+ }
379
+
335
380
  .clear-btn {
336
381
  position: absolute;
337
382
  right: 0.5rem;
@@ -44,6 +44,13 @@ export function getTemplate(placeholder: string = '', version: string = ''): str
44
44
  </div>
45
45
  <button class="clear-btn" id="clearBtn" title="Clear editor">✕</button>
46
46
  </div>
47
+ <div class="info-popup" id="infoPopup">
48
+ <div class="info-popup-content">
49
+ <div class="info-popup-title">GeoJSON Editor</div>
50
+ <a class="info-popup-version" href="https://www.npmjs.com/package/@softwarity/geojson-editor/v/${version}" target="_blank" rel="noopener">v${version}</a>
51
+ <div class="info-popup-copyright">© ${new Date().getFullYear()} Softwarity</div>
52
+ </div>
53
+ </div>
47
54
  `;
48
55
  }
49
56
 
@@ -440,7 +440,7 @@ class GeoJsonEditor extends HTMLElement {
440
440
 
441
441
  // ========== Observed Attributes ==========
442
442
  static get observedAttributes() {
443
- return ['readonly', 'value', 'placeholder', 'dark-selector'];
443
+ return ['readonly', 'value', 'placeholder', 'dark-selector', 'internal-add-shortcut'];
444
444
  }
445
445
 
446
446
  // ========== Lifecycle ==========
@@ -494,6 +494,7 @@ class GeoJsonEditor extends HTMLElement {
494
494
  get readonly() { return this.hasAttribute('readonly'); }
495
495
  get value() { return this.getAttribute('value') || ''; }
496
496
  get placeholder() { return this.getAttribute('placeholder') || ''; }
497
+ get internalAddShortcut() { return this.hasAttribute('internal-add-shortcut'); }
497
498
  get prefix() { return '{"type": "FeatureCollection", "features": ['; }
498
499
  get suffix() { return ']}'; }
499
500
 
@@ -620,34 +621,75 @@ class GeoJsonEditor extends HTMLElement {
620
621
  this.scheduleRender();
621
622
  });
622
623
 
623
- // Mouse move for drag selection
624
- viewport.addEventListener('mousemove', (e: MouseEvent) => {
624
+ // Auto-scroll interval for drag selection outside editor
625
+ let autoScrollInterval: ReturnType<typeof setInterval> | null = null;
626
+
627
+ const stopAutoScroll = () => {
628
+ if (autoScrollInterval) {
629
+ clearInterval(autoScrollInterval);
630
+ autoScrollInterval = null;
631
+ }
632
+ };
633
+
634
+ const startAutoScroll = (direction: 'up' | 'down') => {
635
+ stopAutoScroll();
636
+ const scrollSpeed = 20;
637
+ autoScrollInterval = setInterval(() => {
638
+ if (!this._isSelecting) {
639
+ stopAutoScroll();
640
+ return;
641
+ }
642
+ if (direction === 'up') {
643
+ viewport.scrollTop -= scrollSpeed;
644
+ } else {
645
+ viewport.scrollTop += scrollSpeed;
646
+ }
647
+ // Update selection based on scroll position
648
+ this._updateSelectionFromScroll(direction);
649
+ this._invalidateRenderCache();
650
+ this.scheduleRender();
651
+ }, 50);
652
+ };
653
+
654
+ // Mouse move for drag selection - listen on document to handle drag outside editor
655
+ document.addEventListener('mousemove', (e: MouseEvent) => {
625
656
  if (!this._isSelecting) return;
626
- const pos = this._getPositionFromClick(e);
627
- this.selectionEnd = pos;
628
- this.cursorLine = pos.line;
629
- this.cursorColumn = pos.column;
630
657
 
631
- // Auto-scroll when near edges
632
658
  const rect = viewport.getBoundingClientRect();
633
- const scrollMargin = 30; // pixels from edge to start scrolling
634
- const scrollSpeed = 20; // pixels to scroll per frame
659
+ const scrollMargin = 30;
660
+ const scrollSpeed = 20;
661
+
662
+ // Check if mouse is outside the viewport
663
+ if (e.clientY < rect.top) {
664
+ // Mouse above viewport - start continuous scroll up
665
+ startAutoScroll('up');
666
+ } else if (e.clientY > rect.bottom) {
667
+ // Mouse below viewport - start continuous scroll down
668
+ startAutoScroll('down');
669
+ } else {
670
+ // Mouse inside viewport - stop auto-scroll and update normally
671
+ stopAutoScroll();
672
+ const pos = this._getPositionFromClick(e);
673
+ this.selectionEnd = pos;
674
+ this.cursorLine = pos.line;
675
+ this.cursorColumn = pos.column;
635
676
 
636
- if (e.clientY < rect.top + scrollMargin) {
637
- // Near top edge, scroll up
638
- viewport.scrollTop -= scrollSpeed;
639
- } else if (e.clientY > rect.bottom - scrollMargin) {
640
- // Near bottom edge, scroll down
641
- viewport.scrollTop += scrollSpeed;
642
- }
677
+ // Auto-scroll when near edges (inside viewport)
678
+ if (e.clientY < rect.top + scrollMargin) {
679
+ viewport.scrollTop -= scrollSpeed;
680
+ } else if (e.clientY > rect.bottom - scrollMargin) {
681
+ viewport.scrollTop += scrollSpeed;
682
+ }
643
683
 
644
- this._invalidateRenderCache();
645
- this.scheduleRender();
684
+ this._invalidateRenderCache();
685
+ this.scheduleRender();
686
+ }
646
687
  });
647
-
688
+
648
689
  // Mouse up to end selection
649
690
  document.addEventListener('mouseup', () => {
650
691
  this._isSelecting = false;
692
+ stopAutoScroll();
651
693
  });
652
694
 
653
695
  // Focus/blur handling to show/hide cursor
@@ -738,6 +780,20 @@ class GeoJsonEditor extends HTMLElement {
738
780
  this.removeAll();
739
781
  });
740
782
 
783
+ // Info button - toggle popup
784
+ const infoBtn = this._id('infoBtn');
785
+ const infoPopup = this._id('infoPopup');
786
+ if (infoBtn && infoPopup) {
787
+ infoBtn.addEventListener('click', (e: MouseEvent) => {
788
+ e.stopPropagation();
789
+ infoPopup.classList.toggle('visible');
790
+ });
791
+ // Close popup when clicking outside
792
+ document.addEventListener('click', () => {
793
+ infoPopup.classList.remove('visible');
794
+ });
795
+ }
796
+
741
797
  // Error navigation buttons
742
798
  this._prevErrorBtn?.addEventListener('click', () => {
743
799
  this.goToPrevError();
@@ -1542,12 +1598,14 @@ class GeoJsonEditor extends HTMLElement {
1542
1598
  'z': () => e.shiftKey ? this.redo() : this.undo(),
1543
1599
  'y': () => this.redo(),
1544
1600
  's': () => this.save(),
1545
- 'o': () => !this.hasAttribute('readonly') && this.open()
1601
+ 'o': () => !this.hasAttribute('readonly') && this.open(),
1602
+ 'i': () => this.internalAddShortcut && !this.readonly && this._handleAddFeaturePrompt()
1546
1603
  };
1547
1604
 
1548
1605
  // Check for direct key match
1549
1606
  if (keyHandlers[e.key]) {
1550
1607
  e.preventDefault();
1608
+ e.stopPropagation();
1551
1609
  keyHandlers[e.key]();
1552
1610
  return;
1553
1611
  }
@@ -1555,6 +1613,7 @@ class GeoJsonEditor extends HTMLElement {
1555
1613
  // Check for modifier key combinations
1556
1614
  if ((e.ctrlKey || e.metaKey) && modifierHandlers[e.key]) {
1557
1615
  e.preventDefault();
1616
+ e.stopPropagation();
1558
1617
  modifierHandlers[e.key]();
1559
1618
  }
1560
1619
  }
@@ -2659,9 +2718,25 @@ class GeoJsonEditor extends HTMLElement {
2659
2718
  this.cursorLine = textLines.length - 1;
2660
2719
  this.cursorColumn = textLines[textLines.length - 1].length;
2661
2720
  } else if (this.cursorLine < this.lines.length) {
2721
+ const textLines = text.split('\n');
2662
2722
  const line = this.lines[this.cursorLine];
2663
- this.lines[this.cursorLine] = line.substring(0, this.cursorColumn) + text + line.substring(this.cursorColumn);
2664
- this.cursorColumn += text.length;
2723
+ const before = line.substring(0, this.cursorColumn);
2724
+ const after = line.substring(this.cursorColumn);
2725
+
2726
+ if (textLines.length === 1) {
2727
+ // Single line insertion
2728
+ this.lines[this.cursorLine] = before + text + after;
2729
+ this.cursorColumn += text.length;
2730
+ } else {
2731
+ // Multi-line insertion
2732
+ const firstLine = before + textLines[0];
2733
+ const lastLine = textLines[textLines.length - 1] + after;
2734
+ const middleLines = textLines.slice(1, -1);
2735
+
2736
+ this.lines.splice(this.cursorLine, 1, firstLine, ...middleLines, lastLine);
2737
+ this.cursorLine += textLines.length - 1;
2738
+ this.cursorColumn = textLines[textLines.length - 1].length;
2739
+ }
2665
2740
  }
2666
2741
  this.formatAndUpdate();
2667
2742
  }
@@ -2778,6 +2853,28 @@ class GeoJsonEditor extends HTMLElement {
2778
2853
  return { line, column };
2779
2854
  }
2780
2855
 
2856
+ /**
2857
+ * Update selection during auto-scroll when dragging outside editor
2858
+ */
2859
+ private _updateSelectionFromScroll(direction: 'up' | 'down'): void {
2860
+ if (!this.visibleLines.length) return;
2861
+
2862
+ if (direction === 'up') {
2863
+ // Scrolling up - select to first visible line
2864
+ const firstVisible = this.visibleLines[0];
2865
+ this.selectionEnd = { line: firstVisible.index, column: 0 };
2866
+ this.cursorLine = firstVisible.index;
2867
+ this.cursorColumn = 0;
2868
+ } else {
2869
+ // Scrolling down - select to last visible line
2870
+ const lastVisible = this.visibleLines[this.visibleLines.length - 1];
2871
+ const lineLength = lastVisible.content?.length || 0;
2872
+ this.selectionEnd = { line: lastVisible.index, column: lineLength };
2873
+ this.cursorLine = lastVisible.index;
2874
+ this.cursorColumn = lineLength;
2875
+ }
2876
+ }
2877
+
2781
2878
  // ========== Gutter Interactions ==========
2782
2879
 
2783
2880
  handleGutterClick(e: MouseEvent): void {
@@ -3267,7 +3364,19 @@ class GeoJsonEditor extends HTMLElement {
3267
3364
  // If cursor line is short, likely typing. Long lines = paste
3268
3365
  const isSmallEdit = cursorLineContent.length < 80;
3269
3366
  const skipLine = isSmallEdit ? oldCursorLine : undefined;
3367
+
3368
+ // Format and track where the skipped line ends up
3369
+ const sourceLines = oldContent.split('\n');
3370
+ const beforeLinesCount = skipLine !== undefined
3371
+ ? this._formatChunk(sourceLines.slice(0, skipLine).join('\n')).length
3372
+ : 0;
3373
+
3270
3374
  this.lines = this._bestEffortFormat(oldContent, skipLine);
3375
+
3376
+ // Update cursor line to point to the skipped line's new position
3377
+ if (skipLine !== undefined && beforeLinesCount >= 0) {
3378
+ this.cursorLine = beforeLinesCount;
3379
+ }
3271
3380
  }
3272
3381
  }
3273
3382
 
@@ -3283,23 +3392,43 @@ class GeoJsonEditor extends HTMLElement {
3283
3392
  // operation (insertText, insertNewline, etc.) BEFORE formatAndUpdate was called.
3284
3393
  // We need to adjust for indentation changes while keeping the logical position.
3285
3394
 
3286
- // If cursor is at column 0 (e.g., after newline), keep it there
3287
- // This preserves expected behavior for newline insertion
3288
- if (this.cursorColumn === 0) {
3289
- // Just keep line, column 0 - indentation will be handled by auto-indent
3395
+ // Special case: cursor at column 0 means we're at start of a line (after newline)
3396
+ // Keep both line and column as set by the calling operation
3397
+ if (oldCursorColumn === 0) {
3398
+ // cursorLine was already set by the calling operation, keep column at 0
3399
+ this.cursorColumn = 0;
3290
3400
  } else {
3291
- // For other cases, try to maintain position relative to content (not indentation)
3401
+ // Calculate character offset in old content (ignoring whitespace for comparison)
3292
3402
  const oldLines = oldContent.split('\n');
3403
+ let oldCharOffset = 0;
3404
+ for (let i = 0; i < oldCursorLine && i < oldLines.length; i++) {
3405
+ oldCharOffset += oldLines[i].replace(/\s/g, '').length;
3406
+ }
3293
3407
  const oldLineContent = oldLines[oldCursorLine] || '';
3294
- const oldLeadingSpaces = oldLineContent.length - oldLineContent.trimStart().length;
3295
- const oldColumnInContent = Math.max(0, oldCursorColumn - oldLeadingSpaces);
3296
-
3297
- // Apply same offset to new line's indentation
3298
- if (this.cursorLine < this.lines.length) {
3299
- const newLineContent = this.lines[this.cursorLine];
3300
- const newLeadingSpaces = newLineContent.length - newLineContent.trimStart().length;
3301
- this.cursorColumn = newLeadingSpaces + oldColumnInContent;
3408
+ const oldLineUpToCursor = oldLineContent.substring(0, oldCursorColumn);
3409
+ oldCharOffset += oldLineUpToCursor.replace(/\s/g, '').length;
3410
+
3411
+ // Find corresponding position in new content
3412
+ let charCount = 0;
3413
+ let newLine = 0;
3414
+ let newCol = 0;
3415
+ for (let i = 0; i < this.lines.length; i++) {
3416
+ const lineContent = this.lines[i];
3417
+ for (let j = 0; j <= lineContent.length; j++) {
3418
+ if (charCount >= oldCharOffset) {
3419
+ newLine = i;
3420
+ newCol = j;
3421
+ break;
3422
+ }
3423
+ if (j < lineContent.length && !/\s/.test(lineContent[j])) {
3424
+ charCount++;
3425
+ }
3426
+ }
3427
+ if (charCount >= oldCharOffset) break;
3302
3428
  }
3429
+
3430
+ this.cursorLine = newLine;
3431
+ this.cursorColumn = newCol;
3303
3432
  }
3304
3433
  }
3305
3434
 
@@ -3611,8 +3740,10 @@ class GeoJsonEditor extends HTMLElement {
3611
3740
  */
3612
3741
  add(input: FeatureInput, options: SetOptions = {}): void {
3613
3742
  const newFeatures = normalizeToFeatures(input);
3743
+ const existingCount = this._parseFeatures().length;
3614
3744
  const allFeatures = [...this._parseFeatures(), ...newFeatures];
3615
- this._setFeaturesInternal(allFeatures, options);
3745
+ // Preserve collapsed state for existing features, apply options only to new ones
3746
+ this._setFeaturesInternalPreserving(allFeatures, options, existingCount);
3616
3747
  }
3617
3748
 
3618
3749
  /**
@@ -3628,8 +3759,10 @@ class GeoJsonEditor extends HTMLElement {
3628
3759
  const newFeatures = normalizeToFeatures(input);
3629
3760
  const features = this._parseFeatures();
3630
3761
  const idx = index < 0 ? features.length + index : index;
3631
- features.splice(Math.max(0, Math.min(idx, features.length)), 0, ...newFeatures);
3632
- this._setFeaturesInternal(features, options);
3762
+ const insertIdx = Math.max(0, Math.min(idx, features.length));
3763
+ features.splice(insertIdx, 0, ...newFeatures);
3764
+ // Preserve collapsed state, apply options only to inserted features
3765
+ this._setFeaturesInternalPreserving(features, options, insertIdx, newFeatures.length);
3633
3766
  }
3634
3767
 
3635
3768
  /**
@@ -3641,12 +3774,169 @@ class GeoJsonEditor extends HTMLElement {
3641
3774
  this._applyCollapsedFromOptions(options, features);
3642
3775
  }
3643
3776
 
3777
+ /**
3778
+ * Internal method to set features while preserving collapsed state of existing features
3779
+ * @param features All features (existing + new)
3780
+ * @param options Collapse options for new features
3781
+ * @param newStartIndex Index where new features start (for add) or were inserted (for insertAt)
3782
+ * @param newCount Number of new features (optional, defaults to features from newStartIndex to end)
3783
+ */
3784
+ private _setFeaturesInternalPreserving(
3785
+ features: Feature[],
3786
+ options: SetOptions,
3787
+ newStartIndex: number,
3788
+ newCount?: number
3789
+ ): void {
3790
+ // Save collapsed state by nodeKey before modification
3791
+ const collapsedKeys = new Set<string>();
3792
+ for (const nodeId of this.collapsedNodes) {
3793
+ const nodeInfo = this._nodeIdToLines.get(nodeId);
3794
+ if (nodeInfo?.nodeKey) {
3795
+ // Include feature index in key to handle multiple features with same structure
3796
+ const featureIndex = this._getFeatureIndexForLine(nodeInfo.startLine);
3797
+ collapsedKeys.add(`${featureIndex}:${nodeInfo.nodeKey}`);
3798
+ }
3799
+ }
3800
+
3801
+ // Format and set content
3802
+ const formatted = features.map(f => JSON.stringify(f, null, 2)).join(',\n');
3803
+ this.setValue(formatted, false);
3804
+
3805
+ // Restore collapsed state for existing features
3806
+ const ranges = this._findCollapsibleRanges();
3807
+ const featureRanges = ranges.filter(r => r.isRootFeature);
3808
+ const actualNewCount = newCount !== undefined ? newCount : features.length - newStartIndex;
3809
+
3810
+ for (const range of ranges) {
3811
+ // Find which feature this range belongs to
3812
+ const featureIndex = featureRanges.findIndex(fr =>
3813
+ range.startLine >= fr.startLine && range.endLine <= fr.endLine
3814
+ );
3815
+
3816
+ // Determine if this is an existing feature (adjust index for insertAt case)
3817
+ let originalFeatureIndex = featureIndex;
3818
+ if (featureIndex >= newStartIndex && featureIndex < newStartIndex + actualNewCount) {
3819
+ // This is a new feature - apply options
3820
+ continue;
3821
+ } else if (featureIndex >= newStartIndex + actualNewCount) {
3822
+ // Feature was shifted by insertion - adjust index
3823
+ originalFeatureIndex = featureIndex - actualNewCount;
3824
+ }
3825
+
3826
+ // Check if this node was collapsed before
3827
+ const key = `${originalFeatureIndex}:${range.nodeKey}`;
3828
+ if (collapsedKeys.has(key)) {
3829
+ this.collapsedNodes.add(range.nodeId);
3830
+ }
3831
+ }
3832
+
3833
+ // Apply collapse options to new features only
3834
+ this._applyCollapsedToNewFeatures(options, features, newStartIndex, actualNewCount);
3835
+
3836
+ // Use updateView instead of updateModel since setValue already rebuilt mappings
3837
+ // and we just need to recompute visible lines with new collapsed state
3838
+ this.updateView();
3839
+ this.scheduleRender();
3840
+ }
3841
+
3842
+ /**
3843
+ * Apply collapsed options only to specific new features
3844
+ */
3845
+ private _applyCollapsedToNewFeatures(
3846
+ options: SetOptions,
3847
+ features: Feature[],
3848
+ startIndex: number,
3849
+ count: number
3850
+ ): void {
3851
+ const collapsed = options.collapsed !== undefined ? options.collapsed : ['coordinates'];
3852
+ if (!collapsed || (Array.isArray(collapsed) && collapsed.length === 0)) return;
3853
+
3854
+ const ranges = this._findCollapsibleRanges();
3855
+ const featureRanges = ranges.filter(r => r.isRootFeature);
3856
+
3857
+ for (const range of ranges) {
3858
+ // Find which feature this range belongs to
3859
+ const featureIndex = featureRanges.findIndex(fr =>
3860
+ range.startLine >= fr.startLine && range.endLine <= fr.endLine
3861
+ );
3862
+
3863
+ // Only process new features
3864
+ if (featureIndex < startIndex || featureIndex >= startIndex + count) continue;
3865
+
3866
+ let shouldCollapse = false;
3867
+ if (typeof collapsed === 'function') {
3868
+ const feature = features[featureIndex];
3869
+ const collapsedAttrs = collapsed(feature, featureIndex);
3870
+ shouldCollapse = range.isRootFeature
3871
+ ? collapsedAttrs.includes('$root')
3872
+ : collapsedAttrs.includes(range.nodeKey);
3873
+ } else if (Array.isArray(collapsed)) {
3874
+ shouldCollapse = range.isRootFeature
3875
+ ? collapsed.includes('$root')
3876
+ : collapsed.includes(range.nodeKey);
3877
+ }
3878
+
3879
+ if (shouldCollapse) {
3880
+ this.collapsedNodes.add(range.nodeId);
3881
+ }
3882
+ }
3883
+ }
3884
+
3885
+ /**
3886
+ * Get feature index for a given line
3887
+ */
3888
+ private _getFeatureIndexForLine(line: number): number {
3889
+ let index = 0;
3890
+ for (const [, range] of this.featureRanges) {
3891
+ if (line >= range.startLine && line <= range.endLine) {
3892
+ return range.featureIndex;
3893
+ }
3894
+ index++;
3895
+ }
3896
+ return -1;
3897
+ }
3898
+
3644
3899
  removeAt(index: number): Feature | undefined {
3645
3900
  const features = this._parseFeatures();
3646
3901
  const idx = index < 0 ? features.length + index : index;
3647
3902
  if (idx >= 0 && idx < features.length) {
3903
+ // Save collapsed state by nodeKey before modification
3904
+ const collapsedKeys = new Set<string>();
3905
+ for (const nodeId of this.collapsedNodes) {
3906
+ const nodeInfo = this._nodeIdToLines.get(nodeId);
3907
+ if (nodeInfo?.nodeKey) {
3908
+ const featureIndex = this._getFeatureIndexForLine(nodeInfo.startLine);
3909
+ // Skip the feature being removed, adjust indices for features after it
3910
+ if (featureIndex === idx) continue;
3911
+ const adjustedIndex = featureIndex > idx ? featureIndex - 1 : featureIndex;
3912
+ collapsedKeys.add(`${adjustedIndex}:${nodeInfo.nodeKey}`);
3913
+ }
3914
+ }
3915
+
3648
3916
  const removed = features.splice(idx, 1)[0];
3649
- this.set(features);
3917
+
3918
+ // Format and set content
3919
+ const formatted = features.map((f: Feature) => JSON.stringify(f, null, 2)).join(',\n');
3920
+ this.setValue(formatted, false);
3921
+
3922
+ // Restore collapsed state for remaining features
3923
+ const ranges = this._findCollapsibleRanges();
3924
+ const featureRanges = ranges.filter(r => r.isRootFeature);
3925
+
3926
+ for (const range of ranges) {
3927
+ const featureIndex = featureRanges.findIndex(fr =>
3928
+ range.startLine >= fr.startLine && range.endLine <= fr.endLine
3929
+ );
3930
+ const key = `${featureIndex}:${range.nodeKey}`;
3931
+ if (collapsedKeys.has(key)) {
3932
+ this.collapsedNodes.add(range.nodeId);
3933
+ }
3934
+ }
3935
+
3936
+ // Use updateView instead of updateModel since setValue already rebuilt mappings
3937
+ this.updateView();
3938
+ this.scheduleRender();
3939
+ this.emitChange();
3650
3940
  return removed;
3651
3941
  }
3652
3942
  return undefined;
@@ -3771,6 +4061,24 @@ class GeoJsonEditor extends HTMLElement {
3771
4061
  });
3772
4062
  }
3773
4063
 
4064
+ /**
4065
+ * Handle Ctrl+I shortcut - prompt user to add Feature/Features/FeatureCollection
4066
+ */
4067
+ private _handleAddFeaturePrompt(): void {
4068
+ const input = prompt('Enter GeoJSON (Feature, Feature[], or FeatureCollection):');
4069
+ if (!input || !input.trim()) return;
4070
+
4071
+ try {
4072
+ const parsed = JSON.parse(input);
4073
+ const features = normalizeToFeatures(parsed);
4074
+ if (features.length > 0) {
4075
+ this.add(features);
4076
+ }
4077
+ } catch {
4078
+ // Invalid JSON - ignore silently
4079
+ }
4080
+ }
4081
+
3774
4082
  private _parseFeatures() {
3775
4083
  try {
3776
4084
  const content = this.lines.join('\n');