bitwrench 1.2.16 → 2.0.7

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.
Files changed (130) hide show
  1. package/README.md +160 -158
  2. package/bin/bitwrench.js +3 -0
  3. package/dist/bitwrench-code-edit.cjs.js +639 -0
  4. package/dist/bitwrench-code-edit.es5.js +875 -0
  5. package/dist/bitwrench-code-edit.es5.min.js +15 -0
  6. package/dist/bitwrench-code-edit.esm.js +628 -0
  7. package/dist/bitwrench-code-edit.esm.min.js +15 -0
  8. package/dist/bitwrench-code-edit.umd.js +645 -0
  9. package/dist/bitwrench-code-edit.umd.min.js +15 -0
  10. package/dist/bitwrench.cjs.js +6983 -0
  11. package/dist/bitwrench.cjs.min.js +62 -0
  12. package/dist/bitwrench.css +5100 -0
  13. package/dist/bitwrench.es5.js +8446 -0
  14. package/dist/bitwrench.es5.min.js +31 -0
  15. package/dist/bitwrench.esm.js +6981 -0
  16. package/dist/bitwrench.esm.min.js +62 -0
  17. package/dist/bitwrench.umd.js +6989 -0
  18. package/dist/bitwrench.umd.min.js +62 -0
  19. package/dist/builds.json +127 -0
  20. package/dist/sri.json +18 -0
  21. package/package.json +86 -24
  22. package/readme.html +288 -0
  23. package/src/bitwrench-code-edit.js +627 -0
  24. package/src/bitwrench-color-utils.js +311 -0
  25. package/src/bitwrench-component-base.js +736 -0
  26. package/src/bitwrench-components-inline.js +374 -0
  27. package/src/bitwrench-components-v2.js +1879 -0
  28. package/src/bitwrench-components.js +610 -0
  29. package/src/bitwrench-styles.js +3240 -0
  30. package/src/bitwrench.js +3367 -0
  31. package/src/cli/convert.js +205 -0
  32. package/src/cli/index.js +122 -0
  33. package/src/cli/inject.js +55 -0
  34. package/src/cli/layout-default.js +142 -0
  35. package/src/generate-css.js +381 -0
  36. package/src/vendor/quikdown.js +654 -0
  37. package/src/version.js +16 -0
  38. package/.eslintrc.json +0 -27
  39. package/.github/workflows/codeql-analysis.yml +0 -72
  40. package/.travis.yml +0 -34
  41. package/bitwrench.css +0 -92
  42. package/bitwrench.js +0 -3348
  43. package/bitwrench.js_sri.txt +0 -1
  44. package/bitwrench.min.js +0 -1
  45. package/bitwrench.min.js_sri.txt +0 -1
  46. package/bitwrench_ESM.js +0 -3207
  47. package/bitwrench_ESM.js_sri.txt +0 -1
  48. package/bitwrench_ESM.min.js +0 -1
  49. package/bitwrench_ESM.min.js_sri.txt +0 -1
  50. package/dev/bitwrench-todo.md +0 -215
  51. package/dev/css-arrows.md +0 -23
  52. package/dev/docStringDev.js +0 -124
  53. package/dev/docStringParseDev.js +0 -171
  54. package/dev/example11-load-mjs-page.html +0 -17
  55. package/dev/figures.html +0 -37
  56. package/dev/html_gen.js +0 -349
  57. package/dev/htmld.md +0 -250
  58. package/dev/htmldev.html +0 -45
  59. package/dev/index-old.html +0 -87
  60. package/dev/misc-notes.md +0 -21
  61. package/dev/norm.css +0 -30
  62. package/dev/notes.md +0 -2
  63. package/dev/pageData.mjs +0 -69
  64. package/dev/sizes.html +0 -49
  65. package/dev/universal-js-module.js +0 -37
  66. package/examples/example1.html +0 -78
  67. package/examples/example10.html +0 -84
  68. package/examples/example11.html +0 -17
  69. package/examples/example12.html +0 -18
  70. package/examples/example2.html +0 -44
  71. package/examples/example3.html +0 -50
  72. package/examples/example4.html +0 -22
  73. package/examples/example5.html +0 -82
  74. package/examples/example6.html +0 -128
  75. package/examples/example7.html +0 -91
  76. package/examples/example8.html +0 -27
  77. package/examples/example9.html +0 -102
  78. package/examples/examplePageData12.mjs +0 -73
  79. package/examples/pageData.mjs +0 -69
  80. package/examples/pico.min.css +0 -5
  81. package/icon/bitwrench-dark-tall.png +0 -0
  82. package/icon/bitwrench-dark.png +0 -0
  83. package/icon/bitwrench-icon-lt-grey.png +0 -0
  84. package/icon/bitwrench-icon.vsd +0 -0
  85. package/icon/bitwrench-logo-dark.png +0 -0
  86. package/icon/bitwrench-logo-full.png +0 -0
  87. package/icon/bitwrench-logo-green.png +0 -0
  88. package/icon/bitwrench-logo-grey.png +0 -0
  89. package/icon/bitwrench-logo-white.png +0 -0
  90. package/icon/bitwrench-logos-colors.png +0 -0
  91. package/icon/bitwrench-thick-logo.png +0 -0
  92. package/icon/bitwrench-thick-teal/android-chrome-192x192.png +0 -0
  93. package/icon/bitwrench-thick-teal/android-chrome-512x512.png +0 -0
  94. package/icon/bitwrench-thick-teal/apple-touch-icon.png +0 -0
  95. package/icon/bitwrench-thick-teal/browserconfig.xml +0 -9
  96. package/icon/bitwrench-thick-teal/favicon-16x16.png +0 -0
  97. package/icon/bitwrench-thick-teal/favicon-32x32.png +0 -0
  98. package/icon/bitwrench-thick-teal/favicon.ico +0 -0
  99. package/icon/bitwrench-thick-teal/mstile-144x144.png +0 -0
  100. package/icon/bitwrench-thick-teal/mstile-150x150.png +0 -0
  101. package/icon/bitwrench-thick-teal/mstile-310x150.png +0 -0
  102. package/icon/bitwrench-thick-teal/mstile-310x310.png +0 -0
  103. package/icon/bitwrench-thick-teal/mstile-70x70.png +0 -0
  104. package/icon/bitwrench-thick-teal/site.webmanifest +0 -19
  105. package/icon/bitwrench-thick-teal.ico +0 -0
  106. package/icon/bitwrench-thick-teal.svg +0 -44
  107. package/icon/bitwrench-thick-teal.zip +0 -0
  108. package/icon/favicon-test.html +0 -20
  109. package/icon/logos-test.PNG +0 -0
  110. package/images/bitwrench-512x512.png +0 -0
  111. package/images/bitwrench-logo-med.png +0 -0
  112. package/images/bitwrench-thick-logo.png +0 -0
  113. package/images/bitwrench-thick-logo.svg +0 -64
  114. package/images/bitwrench-thick-teal.ico +0 -0
  115. package/images/favicon.ico +0 -0
  116. package/index.html +0 -282
  117. package/instr_tmp/bitwrench.js +0 -1350
  118. package/karma.conf.js +0 -140
  119. package/makefile +0 -21
  120. package/quick-docs.html +0 -206
  121. package/test/bitwrench_test.js +0 -1255
  122. package/test/karma-test.js +0 -1081
  123. package/tools/bw_deprecatedNames.js +0 -19
  124. package/tools/bwconsole.js +0 -20
  125. package/tools/createSimpleHTMLPage.js +0 -41
  126. package/tools/emitreadme.sh +0 -4
  127. package/tools/export-bw-default-css.js +0 -41
  128. package/tools/umd2ModuleHack.js +0 -32
  129. package/tools/update-bw-package.js +0 -36
  130. package/tools/updatereadme.js +0 -34
@@ -0,0 +1,15 @@
1
+ /*! bitwrench v2.0.7 | BSD-2-Clause | http://deftio.com/bitwrench */
2
+ !function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).bwCodeEdit={})}(this,(function(e){"use strict";
3
+ /**
4
+ * bitwrench-code-edit.js - syntax-highlighted contenteditable code editor addon
5
+ *
6
+ * Provides bw.highlight() for tokenizing JS/CSS/HTML into TACO spans,
7
+ * and bw.codeEditor() for a live editable code block with syntax coloring.
8
+ *
9
+ * Can be loaded standalone (browser script tag after bitwrench.umd.js),
10
+ * or imported as an ES module / CJS module.
11
+ *
12
+ * @module bitwrench-code-edit
13
+ * @license BSD-2-Clause
14
+ */var t=!1,o='.bw-ce{background:#1e293b;border-radius:6px;border:1px solid rgba(255,255,255,0.08);overflow:auto}.bw-ce pre{margin:0;padding:0}.bw-ce code{font-family:"SF Mono",Monaco,"Cascadia Code",Consolas,monospace;font-size:0.875rem;line-height:1.6;color:#e2e8f0;outline:none;white-space:pre-wrap;display:block;padding:0.75rem 1rem}.bw-ce code:empty::before{content:"\\200b"}.bw-ce .bw-ce-keyword{color:#c792ea}.bw-ce .bw-ce-string{color:#c3e88d}.bw-ce .bw-ce-comment{color:#546e7a;font-style:italic}.bw-ce .bw-ce-number{color:#f78c6c}.bw-ce .bw-ce-operator{color:#89ddff}.bw-ce .bw-ce-punctuation{color:#89ddff}.bw-ce .bw-ce-property{color:#82aaff}.bw-ce .bw-ce-function{color:#82aaff}.bw-ce .bw-ce-tag{color:#f07178}.bw-ce .bw-ce-attr-name{color:#ffcb6b}.bw-ce .bw-ce-attr-value{color:#c3e88d}.bw-ce .bw-ce-selector{color:#c792ea}.bw-ce .bw-ce-css-prop{color:#82aaff}.bw-ce .bw-ce-css-value{color:#f78c6c}.bw-ce .bw-ce-at-rule{color:#c792ea;font-style:italic}.bw-ce .bw-ce-color{color:#f78c6c}.bw-ce .bw-ce-template-interp{color:#89ddff}.bw-ce-light.bw-ce{background:#fafafa;border-color:#d8d8d8}.bw-ce-light.bw-ce code{color:#1a1a1a}.bw-ce-light.bw-ce .bw-ce-keyword{color:#7c3aed}.bw-ce-light.bw-ce .bw-ce-string{color:#16a34a}.bw-ce-light.bw-ce .bw-ce-comment{color:#9ca3af;font-style:italic}.bw-ce-light.bw-ce .bw-ce-number{color:#ea580c}.bw-ce-light.bw-ce .bw-ce-operator{color:#0891b2}.bw-ce-light.bw-ce .bw-ce-punctuation{color:#6b7280}.bw-ce-light.bw-ce .bw-ce-property{color:#2563eb}.bw-ce-light.bw-ce .bw-ce-function{color:#2563eb}.bw-ce-light.bw-ce .bw-ce-tag{color:#dc2626}.bw-ce-light.bw-ce .bw-ce-attr-name{color:#d97706}.bw-ce-light.bw-ce .bw-ce-attr-value{color:#16a34a}.bw-ce-light.bw-ce .bw-ce-selector{color:#7c3aed}.bw-ce-light.bw-ce .bw-ce-css-prop{color:#2563eb}.bw-ce-light.bw-ce .bw-ce-css-value{color:#ea580c}.bw-ce-light.bw-ce .bw-ce-at-rule{color:#7c3aed}.bw-ce-light.bw-ce .bw-ce-color{color:#ea580c}.bw-ce-light.bw-ce .bw-ce-template-interp{color:#0891b2}';var c={};function r(e){var t=[],o=0,n=e.length,a="";function l(e){a.length&&(t.push({type:e,text:a}),a="")}for(;o<n;){var i=e[o],s=e[o+1];if("/"!==i||"/"!==s)if("/"!==i||"*"!==s){if("/"===i&&"/"!==s&&"*"!==s){var p=t.length?t[t.length-1]:null,u=!1;if(a.trim().length||p&&"operator"!==p.type&&"punctuation"!==p.type&&"keyword"!==p.type||(u=!0),u){l("plain");for(var f="/",b=o+1,w=!1,d=!1;b<n;){var h=e[b];if(w)f+=h,w=!1,b++;else if("\\"!==h)if("["!==h)if("]"!==h){if("/"===h&&!d){f+=h,b++;break}if("\n"===h)break;f+=h,b++}else d=!1,f+=h,b++;else d=!0,f+=h,b++;else w=!0,f+=h,b++}for(;b<n&&/[gimsuvy]/.test(e[b]);)f+=e[b],b++;t.push({type:"string",text:f}),o=b;continue}}if('"'!==i&&"'"!==i&&"`"!==i)if(/[0-9]/.test(i)||"."===i&&s&&/[0-9]/.test(s)){l("plain");var g="";if("0"===i&&s&&/[xXbBoO]/.test(s))for(g=i+s,o+=2;o<n&&/[0-9a-fA-F_]/.test(e[o]);)g+=e[o],o++;else for(;o<n&&/[0-9._eE]/.test(e[o]);)"e"!==e[o]&&"E"!==e[o]||!e[o+1]||!/[+\-0-9]/.test(e[o+1])?(g+=e[o],o++):(g+=e[o]+e[o+1],o+=2);t.push({type:"number",text:g})}else if(/[a-zA-Z_$]/.test(i)){l("plain");for(var v="";o<n&&/[a-zA-Z0-9_$]/.test(e[o]);)v+=e[o],o++;for(var y=o;y<n&&(" "===e[y]||"\t"===e[y]);)y++;var x=t.length?t[t.length-1]:null,m=x&&"punctuation"===x.type&&"."===x.text;c[v]?t.push({type:"keyword",text:v}):m?y<n&&"("===e[y]?t.push({type:"function",text:v}):t.push({type:"property",text:v}):y<n&&"("===e[y]?t.push({type:"function",text:v}):t.push({type:"plain",text:v})}else if(-1==="=+-*/%!<>&|^~?:".indexOf(i))-1==="(){}[];,.".indexOf(i)?(a+=i,o++):(l("plain"),t.push({type:"punctuation",text:i}),o++);else{l("plain");var k=i;++o<n&&-1!=="=+-*/%!<>&|^~?:".indexOf(e[o])&&(k+=e[o],++o<n&&-1!=="=>&|".indexOf(e[o])&&(k+=e[o],o++)),t.push({type:"operator",text:k})}else{l("plain");var C=i,S=i,T=o+1;if("`"===C){for(;T<n;){var A=e[T];if("\\"!==A)if("$"!==A||"{"!==e[T+1]){if("`"===A){S+=A,T++;break}S+=A,T++}else{S.length&&t.push({type:"string",text:S}),S="",t.push({type:"template-interp",text:"${"}),T+=2;for(var E=1,O="";T<n&&E>0;){if("{"===e[T])E++;else if("}"===e[T]&&0===--E)break;O+=e[T],T++}var z=r(O);(t=t.concat(z)).push({type:"template-interp",text:"}"}),T++}else S+=A+(e[T+1]||""),T+=2}S.length&&t.push({type:"string",text:S})}else{for(;T<n;){var _=e[T];if("\\"!==_){if(_===C){S+=_,T++;break}if("\n"===_)break;S+=_,T++}else S+=_+(e[T+1]||""),T+=2}t.push({type:"string",text:S})}o=T}}else{l("plain");var R=e.indexOf("*/",o+2);-1===R&&(R=n-2),t.push({type:"comment",text:e.substring(o,R+2)}),o=R+2}else{l("plain");var Z=e.indexOf("\n",o);-1===Z&&(Z=n),t.push({type:"comment",text:e.substring(o,Z)}),o=Z}}return l("plain"),t}function n(e){var t=[],o=0,c=e.length,r="selector",n="";function a(e){n.length&&(t.push({type:e||"plain",text:n}),n="")}for(;o<c;){var l=e[o],i=e[o+1];if("/"!==l||"*"!==i)if('"'!==l&&"'"!==l||"value"!==r&&"selector"!==r)if("@"!==l||"selector"!==r)if("#"!==l||"value"!==r)if("value"===r&&/[0-9]/.test(l)){a("css-value");for(var s="";o<c&&/[0-9.]/.test(e[o]);)s+=e[o],o++;for(var p="";o<c&&/[a-zA-Z%]/.test(e[o]);)p+=e[o],o++;t.push({type:"number",text:s+p})}else"{"!==l?"}"!==l?":"!==l||"prop"!==r?";"!==l?","!==l?(n+=l,o++):(a("selector"===r?"selector":"value"===r?"css-value":"plain"),t.push({type:"punctuation",text:l}),o++):(a("value"===r?"css-value":"plain"),t.push({type:"punctuation",text:l}),r="prop",o++):(a("css-prop"),t.push({type:"punctuation",text:l}),r="value",o++):(a("prop"===r?"css-prop":"value"===r?"css-value":"plain"),t.push({type:"punctuation",text:l}),r="selector",o++):(a("selector"===r?"selector":"plain"),t.push({type:"punctuation",text:l}),r="prop",o++);else{a("css-value");var u="#";for(o++;o<c&&/[0-9a-fA-F]/.test(e[o]);)u+=e[o],o++;t.push({type:"color",text:u})}else{a("selector");var f="@";for(o++;o<c&&/[a-zA-Z\-]/.test(e[o]);)f+=e[o],o++;t.push({type:"at-rule",text:f})}else{a("selector"===r?"selector":"css-value");var b=l,w=l;for(o++;o<c;)if("\\"!==e[o]){if(e[o]===b){w+=e[o],o++;break}w+=e[o],o++}else w+=e[o]+(e[o+1]||""),o+=2;t.push({type:"string",text:w})}else{a("selector"===r?"selector":"prop"===r?"css-prop":"css-value");var d=e.indexOf("*/",o+2);-1===d&&(d=c-2),t.push({type:"comment",text:e.substring(o,d+2)}),o=d+2}}return a("selector"===r?"selector":"prop"===r?"css-prop":"css-value"),t}function a(e){var t=[],o=0,c=e.length,r="";function n(e){r.length&&(t.push({type:e,text:r}),r="")}for(;o<c;){var a=e[o];if("<"!==a||"\x3c!--"!==e.substring(o,o+4))if("<"!==a)if("&"!==a)r+=a,o++;else{n("plain");var l="&";for(o++;o<c&&";"!==e[o]&&/[a-zA-Z0-9#]/.test(e[o]);)l+=e[o],o++;o<c&&";"===e[o]&&(l+=";",o++),t.push({type:"string",text:l})}else{n("plain");var i="<";for(++o<c&&"/"===e[o]&&(i+="/",o++);o<c&&/[a-zA-Z0-9\-]/.test(e[o]);)i+=e[o],o++;for(t.push({type:"tag",text:i});o<c&&">"!==e[o]&&("/"!==e[o]||">"!==e[o+1]);)if(/\s/.test(e[o])){for(var s="";o<c&&/\s/.test(e[o]);)s+=e[o],o++;t.push({type:"plain",text:s})}else if(/[a-zA-Z_\-@:]/.test(e[o])){for(var p="";o<c&&/[a-zA-Z0-9_\-@:]/.test(e[o]);)p+=e[o],o++;if(t.push({type:"attr-name",text:p}),o<c&&"="===e[o])if(t.push({type:"punctuation",text:"="}),++o<c&&('"'===e[o]||"'"===e[o])){var u=e[o],f=u;for(o++;o<c&&e[o]!==u;)f+=e[o],o++;o<c&&(f+=e[o],o++),t.push({type:"attr-value",text:f})}else{for(var b="";o<c&&!/[\s>]/.test(e[o]);)b+=e[o],o++;t.push({type:"attr-value",text:b})}}else r+=e[o],o++,n("plain");var w="";o<c&&"/"===e[o]&&(w+="/",o++),o<c&&">"===e[o]&&(w+=">",o++),w&&t.push({type:"tag",text:w})}else{n("plain");var d=e.indexOf("--\x3e",o+4);-1===d&&(d=c-3),t.push({type:"comment",text:e.substring(o,d+3)}),o=d+3}}return n("plain"),t}"var,const,let,function,return,if,else,for,while,do,switch,case,break,continue,new,typeof,instanceof,this,class,extends,import,export,default,from,true,false,null,undefined,try,catch,throw,finally,async,await,yield,of,in,delete,void,with,super,static,get,set,debugger".split(",").forEach((function(e){c[e]=!0}));var l={js:r,javascript:r,css:n,html:a};function i(e,t){return function(e){for(var t=[],o=0;o<e.length;o++){var c=e[o];"plain"===c.type?t.push(c.text):t.push({t:"span",a:{class:"bw-ce-"+c.type},c:c.text})}return t}((l[t]||r)(e))}function s(e){var t=(e=e||{}).code||"",o=e.lang||"js",c=e.height||"180px",r=!!e.readOnly,n="bw-ce"+(e.className?" "+e.className:""),a=i(t,o),l={spellcheck:"false",class:"bw-ce-code"};return r||(l.contenteditable="true"),{t:"div",a:{class:n,style:"max-height:"+c+";overflow:auto"},c:[{t:"pre",c:{t:"code",a:l,c:a}}],o:{mounted:function(c){var n=c.querySelector(".bw-ce-code");if(n){var a=t,l=null,s="undefined"!=typeof window&&window.bw||{};c._bwCodeEdit={getValue:p,setValue:function(e){a=e;var t=i(e,o);s.html&&(n.innerHTML=s.html({t:"span",c:t}))}},r||(n.addEventListener("input",(function(){clearTimeout(l),l=setTimeout(u,50)})),n.addEventListener("keydown",(function(e){"Tab"===e.key&&(e.preventDefault(),document.execCommand("insertText",!1," "))})))}function p(){return n.textContent||""}function u(){var t=p();if(t!==a){a=t;var c=function(e){var t=window.getSelection();if(!t.rangeCount)return 0;var o=t.getRangeAt(0).cloneRange();return o.selectNodeContents(e),o.setEnd(t.getRangeAt(0).startContainer,t.getRangeAt(0).startOffset),o.toString().length}(n),r=i(t,o);s.html&&(n.innerHTML=s.html({t:"span",c:r})),function(e,t){for(var o,c=window.getSelection(),r=document.createRange(),n=document.createTreeWalker(e,NodeFilter.SHOW_TEXT,null,!1),a=0;o=n.nextNode();){var l=o.textContent.length;if(a+l>=t)return r.setStart(o,t-a),r.collapse(!0),c.removeAllRanges(),void c.addRange(r);a+=l}r.selectNodeContents(e),r.collapse(!1),c.removeAllRanges(),c.addRange(r)}(n,c),e.onChange&&e.onChange(t)}}}}}}function p(e){e&&(e.highlight=i,e.codeEditor=function(c){return function(e){t||(t=!0,e&&e.injectCSS&&e.injectCSS(o,{id:"bw-code-edit-styles"}))}(e),s(c)})}"undefined"!=typeof window&&window.bw&&p(window.bw);var u={highlight:i,codeEditor:s,install:p,CSS_TEXT:o};e.CSS_TEXT=o,e.codeEditor=s,e.default=u,e.highlight=i,e.install=p,e.tokenizeCSS=n,e.tokenizeHTML=a,e.tokenizeJS=r,Object.defineProperty(e,"__esModule",{value:!0})}));
15
+ //# sourceMappingURL=bitwrench-code-edit.es5.min.js.map
@@ -0,0 +1,628 @@
1
+ /*! bitwrench v2.0.7 | BSD-2-Clause | http://deftio.com/bitwrench */
2
+ /**
3
+ * bitwrench-code-edit.js - syntax-highlighted contenteditable code editor addon
4
+ *
5
+ * Provides bw.highlight() for tokenizing JS/CSS/HTML into TACO spans,
6
+ * and bw.codeEditor() for a live editable code block with syntax coloring.
7
+ *
8
+ * Can be loaded standalone (browser script tag after bitwrench.umd.js),
9
+ * or imported as an ES module / CJS module.
10
+ *
11
+ * @module bitwrench-code-edit
12
+ * @license BSD-2-Clause
13
+ */
14
+
15
+ // -- CSS (injected once) ----------------------------------------------
16
+ var _cssInjected = false;
17
+ var CSS_TEXT =
18
+ '.bw-ce{background:#1e293b;border-radius:6px;border:1px solid rgba(255,255,255,0.08);overflow:auto}' +
19
+ '.bw-ce pre{margin:0;padding:0}' +
20
+ '.bw-ce code{font-family:"SF Mono",Monaco,"Cascadia Code",Consolas,monospace;font-size:0.875rem;line-height:1.6;color:#e2e8f0;outline:none;white-space:pre-wrap;display:block;padding:0.75rem 1rem}' +
21
+ '.bw-ce code:empty::before{content:"\\200b"}' +
22
+ '.bw-ce .bw-ce-keyword{color:#c792ea}' +
23
+ '.bw-ce .bw-ce-string{color:#c3e88d}' +
24
+ '.bw-ce .bw-ce-comment{color:#546e7a;font-style:italic}' +
25
+ '.bw-ce .bw-ce-number{color:#f78c6c}' +
26
+ '.bw-ce .bw-ce-operator{color:#89ddff}' +
27
+ '.bw-ce .bw-ce-punctuation{color:#89ddff}' +
28
+ '.bw-ce .bw-ce-property{color:#82aaff}' +
29
+ '.bw-ce .bw-ce-function{color:#82aaff}' +
30
+ '.bw-ce .bw-ce-tag{color:#f07178}' +
31
+ '.bw-ce .bw-ce-attr-name{color:#ffcb6b}' +
32
+ '.bw-ce .bw-ce-attr-value{color:#c3e88d}' +
33
+ '.bw-ce .bw-ce-selector{color:#c792ea}' +
34
+ '.bw-ce .bw-ce-css-prop{color:#82aaff}' +
35
+ '.bw-ce .bw-ce-css-value{color:#f78c6c}' +
36
+ '.bw-ce .bw-ce-at-rule{color:#c792ea;font-style:italic}' +
37
+ '.bw-ce .bw-ce-color{color:#f78c6c}' +
38
+ '.bw-ce .bw-ce-template-interp{color:#89ddff}' +
39
+ // Light theme
40
+ '.bw-ce-light.bw-ce{background:#fafafa;border-color:#d8d8d8}' +
41
+ '.bw-ce-light.bw-ce code{color:#1a1a1a}' +
42
+ '.bw-ce-light.bw-ce .bw-ce-keyword{color:#7c3aed}' +
43
+ '.bw-ce-light.bw-ce .bw-ce-string{color:#16a34a}' +
44
+ '.bw-ce-light.bw-ce .bw-ce-comment{color:#9ca3af;font-style:italic}' +
45
+ '.bw-ce-light.bw-ce .bw-ce-number{color:#ea580c}' +
46
+ '.bw-ce-light.bw-ce .bw-ce-operator{color:#0891b2}' +
47
+ '.bw-ce-light.bw-ce .bw-ce-punctuation{color:#6b7280}' +
48
+ '.bw-ce-light.bw-ce .bw-ce-property{color:#2563eb}' +
49
+ '.bw-ce-light.bw-ce .bw-ce-function{color:#2563eb}' +
50
+ '.bw-ce-light.bw-ce .bw-ce-tag{color:#dc2626}' +
51
+ '.bw-ce-light.bw-ce .bw-ce-attr-name{color:#d97706}' +
52
+ '.bw-ce-light.bw-ce .bw-ce-attr-value{color:#16a34a}' +
53
+ '.bw-ce-light.bw-ce .bw-ce-selector{color:#7c3aed}' +
54
+ '.bw-ce-light.bw-ce .bw-ce-css-prop{color:#2563eb}' +
55
+ '.bw-ce-light.bw-ce .bw-ce-css-value{color:#ea580c}' +
56
+ '.bw-ce-light.bw-ce .bw-ce-at-rule{color:#7c3aed}' +
57
+ '.bw-ce-light.bw-ce .bw-ce-color{color:#ea580c}' +
58
+ '.bw-ce-light.bw-ce .bw-ce-template-interp{color:#0891b2}';
59
+
60
+ function ensureCSS(bw) {
61
+ if (_cssInjected) return;
62
+ _cssInjected = true;
63
+ if (bw && bw.injectCSS) {
64
+ bw.injectCSS(CSS_TEXT, { id: 'bw-code-edit-styles' });
65
+ }
66
+ }
67
+
68
+ // -- JS keywords ------------------------------------------------------
69
+ var JS_KEYWORDS = {};
70
+ 'var,const,let,function,return,if,else,for,while,do,switch,case,break,continue,new,typeof,instanceof,this,class,extends,import,export,default,from,true,false,null,undefined,try,catch,throw,finally,async,await,yield,of,in,delete,void,with,super,static,get,set,debugger'.split(',').forEach(function(k) { JS_KEYWORDS[k] = true; });
71
+
72
+ // -- JS Tokenizer -----------------------------------------------------
73
+ function tokenizeJS(code) {
74
+ var tokens = [];
75
+ var i = 0, len = code.length;
76
+ var buf = '';
77
+
78
+ function flush(type) {
79
+ if (buf.length) { tokens.push({ type: type, text: buf }); buf = ''; }
80
+ }
81
+
82
+ while (i < len) {
83
+ var ch = code[i];
84
+ var next = code[i + 1];
85
+
86
+ // Line comment
87
+ if (ch === '/' && next === '/') {
88
+ flush('plain');
89
+ var end = code.indexOf('\n', i);
90
+ if (end === -1) end = len;
91
+ tokens.push({ type: 'comment', text: code.substring(i, end) });
92
+ i = end;
93
+ continue;
94
+ }
95
+
96
+ // Block comment
97
+ if (ch === '/' && next === '*') {
98
+ flush('plain');
99
+ var end2 = code.indexOf('*/', i + 2);
100
+ if (end2 === -1) end2 = len - 2;
101
+ tokens.push({ type: 'comment', text: code.substring(i, end2 + 2) });
102
+ i = end2 + 2;
103
+ continue;
104
+ }
105
+
106
+ // Regex (simple heuristic: / after operator or start-of-line)
107
+ if (ch === '/' && next !== '/' && next !== '*') {
108
+ var prevToken = tokens.length ? tokens[tokens.length - 1] : null;
109
+ var prevBuf = buf.trim();
110
+ var isRegex = false;
111
+ if (!prevBuf.length) {
112
+ if (!prevToken) isRegex = true;
113
+ else if (prevToken.type === 'operator' || prevToken.type === 'punctuation' || prevToken.type === 'keyword') isRegex = true;
114
+ }
115
+ if (isRegex) {
116
+ flush('plain');
117
+ var rBuf = '/';
118
+ var ri = i + 1;
119
+ var escaped = false;
120
+ var inCharClass = false;
121
+ while (ri < len) {
122
+ var rc = code[ri];
123
+ if (escaped) { rBuf += rc; escaped = false; ri++; continue; }
124
+ if (rc === '\\') { escaped = true; rBuf += rc; ri++; continue; }
125
+ if (rc === '[') { inCharClass = true; rBuf += rc; ri++; continue; }
126
+ if (rc === ']') { inCharClass = false; rBuf += rc; ri++; continue; }
127
+ if (rc === '/' && !inCharClass) { rBuf += rc; ri++; break; }
128
+ if (rc === '\n') break;
129
+ rBuf += rc; ri++;
130
+ }
131
+ // Flags
132
+ while (ri < len && /[gimsuvy]/.test(code[ri])) { rBuf += code[ri]; ri++; }
133
+ tokens.push({ type: 'string', text: rBuf });
134
+ i = ri;
135
+ continue;
136
+ }
137
+ }
138
+
139
+ // Strings
140
+ if (ch === '"' || ch === "'" || ch === '`') {
141
+ flush('plain');
142
+ var quote = ch;
143
+ var sBuf = ch;
144
+ var si = i + 1;
145
+ if (quote === '`') {
146
+ // Template literal
147
+ while (si < len) {
148
+ var sc = code[si];
149
+ if (sc === '\\') { sBuf += sc + (code[si + 1] || ''); si += 2; continue; }
150
+ if (sc === '$' && code[si + 1] === '{') {
151
+ // Flush string part so far
152
+ if (sBuf.length) tokens.push({ type: 'string', text: sBuf }); sBuf = '';
153
+ // Find matching brace (simple: count braces)
154
+ tokens.push({ type: 'template-interp', text: '${' });
155
+ si += 2;
156
+ var depth = 1;
157
+ var interpBuf = '';
158
+ while (si < len && depth > 0) {
159
+ if (code[si] === '{') depth++;
160
+ else if (code[si] === '}') { depth--; if (depth === 0) break; }
161
+ interpBuf += code[si]; si++;
162
+ }
163
+ // Tokenize the interpolation content recursively
164
+ var interpTokens = tokenizeJS(interpBuf);
165
+ tokens = tokens.concat(interpTokens);
166
+ tokens.push({ type: 'template-interp', text: '}' });
167
+ si++; // skip closing }
168
+ continue;
169
+ }
170
+ if (sc === '`') { sBuf += sc; si++; break; }
171
+ sBuf += sc; si++;
172
+ }
173
+ if (sBuf.length) tokens.push({ type: 'string', text: sBuf });
174
+ } else {
175
+ while (si < len) {
176
+ var sc2 = code[si];
177
+ if (sc2 === '\\') { sBuf += sc2 + (code[si + 1] || ''); si += 2; continue; }
178
+ if (sc2 === quote) { sBuf += sc2; si++; break; }
179
+ if (sc2 === '\n') break;
180
+ sBuf += sc2; si++;
181
+ }
182
+ tokens.push({ type: 'string', text: sBuf });
183
+ }
184
+ i = si;
185
+ continue;
186
+ }
187
+
188
+ // Numbers
189
+ if (/[0-9]/.test(ch) || (ch === '.' && next && /[0-9]/.test(next))) {
190
+ flush('plain');
191
+ var nBuf = '';
192
+ if (ch === '0' && next && /[xXbBoO]/.test(next)) {
193
+ nBuf = ch + next; i += 2;
194
+ while (i < len && /[0-9a-fA-F_]/.test(code[i])) { nBuf += code[i]; i++; }
195
+ } else {
196
+ while (i < len && /[0-9._eE]/.test(code[i])) {
197
+ if ((code[i] === 'e' || code[i] === 'E') && code[i + 1] && /[+\-0-9]/.test(code[i + 1])) {
198
+ nBuf += code[i] + code[i + 1]; i += 2; continue;
199
+ }
200
+ nBuf += code[i]; i++;
201
+ }
202
+ }
203
+ tokens.push({ type: 'number', text: nBuf });
204
+ continue;
205
+ }
206
+
207
+ // Identifiers / keywords
208
+ if (/[a-zA-Z_$]/.test(ch)) {
209
+ flush('plain');
210
+ var wBuf = '';
211
+ while (i < len && /[a-zA-Z0-9_$]/.test(code[i])) { wBuf += code[i]; i++; }
212
+ // Look-ahead: is it a function call?
213
+ var la = i;
214
+ while (la < len && (code[la] === ' ' || code[la] === '\t')) la++;
215
+ // Look-back: is it a property (after dot)?
216
+ var prevTok = tokens.length ? tokens[tokens.length - 1] : null;
217
+ var isDot = prevTok && prevTok.type === 'punctuation' && prevTok.text === '.';
218
+
219
+ if (JS_KEYWORDS[wBuf]) {
220
+ tokens.push({ type: 'keyword', text: wBuf });
221
+ } else if (isDot) {
222
+ if (la < len && code[la] === '(') {
223
+ tokens.push({ type: 'function', text: wBuf });
224
+ } else {
225
+ tokens.push({ type: 'property', text: wBuf });
226
+ }
227
+ } else if (la < len && code[la] === '(') {
228
+ tokens.push({ type: 'function', text: wBuf });
229
+ } else {
230
+ tokens.push({ type: 'plain', text: wBuf });
231
+ }
232
+ continue;
233
+ }
234
+
235
+ // Operators
236
+ if ('=+-*/%!<>&|^~?:'.indexOf(ch) !== -1) {
237
+ flush('plain');
238
+ // Consume multi-char operators
239
+ var opBuf = ch;
240
+ i++;
241
+ if (i < len && '=+-*/%!<>&|^~?:'.indexOf(code[i]) !== -1) {
242
+ opBuf += code[i]; i++;
243
+ if (i < len && '=>&|'.indexOf(code[i]) !== -1) { opBuf += code[i]; i++; }
244
+ }
245
+ tokens.push({ type: 'operator', text: opBuf });
246
+ continue;
247
+ }
248
+
249
+ // Punctuation
250
+ if ('(){}[];,.'.indexOf(ch) !== -1) {
251
+ flush('plain');
252
+ tokens.push({ type: 'punctuation', text: ch });
253
+ i++;
254
+ continue;
255
+ }
256
+
257
+ // Plain (whitespace + anything else)
258
+ buf += ch;
259
+ i++;
260
+ }
261
+ flush('plain');
262
+ return tokens;
263
+ }
264
+
265
+ // -- CSS Tokenizer ----------------------------------------------------
266
+ function tokenizeCSS(code) {
267
+ var tokens = [];
268
+ var i = 0, len = code.length;
269
+ var state = 'selector'; // selector | prop | value
270
+ var buf = '';
271
+
272
+ function flush(type) {
273
+ if (buf.length) { tokens.push({ type: type || 'plain', text: buf }); buf = ''; }
274
+ }
275
+
276
+ while (i < len) {
277
+ var ch = code[i];
278
+ var next = code[i + 1];
279
+
280
+ // Block comment
281
+ if (ch === '/' && next === '*') {
282
+ flush(state === 'selector' ? 'selector' : state === 'prop' ? 'css-prop' : 'css-value');
283
+ var end = code.indexOf('*/', i + 2);
284
+ if (end === -1) end = len - 2;
285
+ tokens.push({ type: 'comment', text: code.substring(i, end + 2) });
286
+ i = end + 2;
287
+ continue;
288
+ }
289
+
290
+ // Strings in values
291
+ if ((ch === '"' || ch === "'") && (state === 'value' || state === 'selector')) {
292
+ flush(state === 'selector' ? 'selector' : 'css-value');
293
+ var quote = ch;
294
+ var sBuf = ch;
295
+ i++;
296
+ while (i < len) {
297
+ if (code[i] === '\\') { sBuf += code[i] + (code[i + 1] || ''); i += 2; continue; }
298
+ if (code[i] === quote) { sBuf += code[i]; i++; break; }
299
+ sBuf += code[i]; i++;
300
+ }
301
+ tokens.push({ type: 'string', text: sBuf });
302
+ continue;
303
+ }
304
+
305
+ // At-rules
306
+ if (ch === '@' && state === 'selector') {
307
+ flush('selector');
308
+ var aBuf = '@';
309
+ i++;
310
+ while (i < len && /[a-zA-Z\-]/.test(code[i])) { aBuf += code[i]; i++; }
311
+ tokens.push({ type: 'at-rule', text: aBuf });
312
+ continue;
313
+ }
314
+
315
+ // Hex colors in values
316
+ if (ch === '#' && state === 'value') {
317
+ flush('css-value');
318
+ var hBuf = '#';
319
+ i++;
320
+ while (i < len && /[0-9a-fA-F]/.test(code[i])) { hBuf += code[i]; i++; }
321
+ tokens.push({ type: 'color', text: hBuf });
322
+ continue;
323
+ }
324
+
325
+ // Numbers in values
326
+ if (state === 'value' && /[0-9]/.test(ch)) {
327
+ flush('css-value');
328
+ var nBuf2 = '';
329
+ while (i < len && /[0-9.]/.test(code[i])) { nBuf2 += code[i]; i++; }
330
+ // Unit
331
+ var uBuf = '';
332
+ while (i < len && /[a-zA-Z%]/.test(code[i])) { uBuf += code[i]; i++; }
333
+ tokens.push({ type: 'number', text: nBuf2 + uBuf });
334
+ continue;
335
+ }
336
+
337
+ // Punctuation and state transitions
338
+ if (ch === '{') {
339
+ flush(state === 'selector' ? 'selector' : 'plain');
340
+ tokens.push({ type: 'punctuation', text: ch });
341
+ state = 'prop';
342
+ i++; continue;
343
+ }
344
+ if (ch === '}') {
345
+ flush(state === 'prop' ? 'css-prop' : state === 'value' ? 'css-value' : 'plain');
346
+ tokens.push({ type: 'punctuation', text: ch });
347
+ state = 'selector';
348
+ i++; continue;
349
+ }
350
+ if (ch === ':' && state === 'prop') {
351
+ flush('css-prop');
352
+ tokens.push({ type: 'punctuation', text: ch });
353
+ state = 'value';
354
+ i++; continue;
355
+ }
356
+ if (ch === ';') {
357
+ flush(state === 'value' ? 'css-value' : 'plain');
358
+ tokens.push({ type: 'punctuation', text: ch });
359
+ state = 'prop';
360
+ i++; continue;
361
+ }
362
+ if (ch === ',') {
363
+ flush(state === 'selector' ? 'selector' : state === 'value' ? 'css-value' : 'plain');
364
+ tokens.push({ type: 'punctuation', text: ch });
365
+ i++; continue;
366
+ }
367
+
368
+ // Accumulate into buffer
369
+ buf += ch;
370
+ i++;
371
+ }
372
+ flush(state === 'selector' ? 'selector' : state === 'prop' ? 'css-prop' : 'css-value');
373
+ return tokens;
374
+ }
375
+
376
+ // -- HTML Tokenizer ---------------------------------------------------
377
+ function tokenizeHTML(code) {
378
+ var tokens = [];
379
+ var i = 0, len = code.length;
380
+ var buf = '';
381
+
382
+ function flush(type) {
383
+ if (buf.length) { tokens.push({ type: type, text: buf }); buf = ''; }
384
+ }
385
+
386
+ while (i < len) {
387
+ var ch = code[i];
388
+
389
+ // Comment
390
+ if (ch === '<' && code.substring(i, i + 4) === '<!--') {
391
+ flush('plain');
392
+ var end = code.indexOf('-->', i + 4);
393
+ if (end === -1) end = len - 3;
394
+ tokens.push({ type: 'comment', text: code.substring(i, end + 3) });
395
+ i = end + 3;
396
+ continue;
397
+ }
398
+
399
+ // Tag
400
+ if (ch === '<') {
401
+ flush('plain');
402
+ // Consume < and optional /
403
+ var tBuf = '<';
404
+ i++;
405
+ if (i < len && code[i] === '/') { tBuf += '/'; i++; }
406
+ // Tag name
407
+ while (i < len && /[a-zA-Z0-9\-]/.test(code[i])) { tBuf += code[i]; i++; }
408
+ tokens.push({ type: 'tag', text: tBuf });
409
+
410
+ // Attributes
411
+ while (i < len && code[i] !== '>' && !(code[i] === '/' && code[i + 1] === '>')) {
412
+ // Whitespace
413
+ if (/\s/.test(code[i])) {
414
+ var wBuf = '';
415
+ while (i < len && /\s/.test(code[i])) { wBuf += code[i]; i++; }
416
+ tokens.push({ type: 'plain', text: wBuf });
417
+ continue;
418
+ }
419
+ // Attribute name
420
+ if (/[a-zA-Z_\-@:]/.test(code[i])) {
421
+ var aBuf = '';
422
+ while (i < len && /[a-zA-Z0-9_\-@:]/.test(code[i])) { aBuf += code[i]; i++; }
423
+ tokens.push({ type: 'attr-name', text: aBuf });
424
+ // = sign
425
+ if (i < len && code[i] === '=') {
426
+ tokens.push({ type: 'punctuation', text: '=' });
427
+ i++;
428
+ // Attribute value
429
+ if (i < len && (code[i] === '"' || code[i] === "'")) {
430
+ var q = code[i];
431
+ var vBuf = q;
432
+ i++;
433
+ while (i < len && code[i] !== q) { vBuf += code[i]; i++; }
434
+ if (i < len) { vBuf += code[i]; i++; }
435
+ tokens.push({ type: 'attr-value', text: vBuf });
436
+ } else {
437
+ // Unquoted value
438
+ var uBuf2 = '';
439
+ while (i < len && !/[\s>]/.test(code[i])) { uBuf2 += code[i]; i++; }
440
+ tokens.push({ type: 'attr-value', text: uBuf2 });
441
+ }
442
+ }
443
+ continue;
444
+ }
445
+ // Anything else in tag
446
+ buf += code[i]; i++;
447
+ flush('plain');
448
+ }
449
+
450
+ // Close of tag
451
+ var closeBuf = '';
452
+ if (i < len && code[i] === '/') { closeBuf += '/'; i++; }
453
+ if (i < len && code[i] === '>') { closeBuf += '>'; i++; }
454
+ if (closeBuf) tokens.push({ type: 'tag', text: closeBuf });
455
+ continue;
456
+ }
457
+
458
+ // Entity
459
+ if (ch === '&') {
460
+ flush('plain');
461
+ var eBuf = '&';
462
+ i++;
463
+ while (i < len && code[i] !== ';' && /[a-zA-Z0-9#]/.test(code[i])) { eBuf += code[i]; i++; }
464
+ if (i < len && code[i] === ';') { eBuf += ';'; i++; }
465
+ tokens.push({ type: 'string', text: eBuf });
466
+ continue;
467
+ }
468
+
469
+ // Plain text
470
+ buf += ch;
471
+ i++;
472
+ }
473
+ flush('plain');
474
+ return tokens;
475
+ }
476
+
477
+ // -- Token to TACO conversion -----------------------------------------
478
+ var TOKENIZERS = { js: tokenizeJS, javascript: tokenizeJS, css: tokenizeCSS, html: tokenizeHTML };
479
+
480
+ function tokensToTACO(tokArr) {
481
+ var result = [];
482
+ for (var i = 0; i < tokArr.length; i++) {
483
+ var tok = tokArr[i];
484
+ if (tok.type === 'plain') {
485
+ result.push(tok.text);
486
+ } else {
487
+ result.push({ t: 'span', a: { class: 'bw-ce-' + tok.type }, c: tok.text });
488
+ }
489
+ }
490
+ return result;
491
+ }
492
+
493
+ // -- Public: highlight ------------------------------------------------
494
+ function highlight(code, lang) {
495
+ var tokenizer = TOKENIZERS[lang] || tokenizeJS;
496
+ var tokens = tokenizer(code);
497
+ return tokensToTACO(tokens);
498
+ }
499
+
500
+ // -- Caret save/restore -----------------------------------------------
501
+ function getCaretOffset(el) {
502
+ var sel = window.getSelection();
503
+ if (!sel.rangeCount) return 0;
504
+ var range = sel.getRangeAt(0).cloneRange();
505
+ range.selectNodeContents(el);
506
+ range.setEnd(sel.getRangeAt(0).startContainer, sel.getRangeAt(0).startOffset);
507
+ return range.toString().length;
508
+ }
509
+
510
+ function setCaretOffset(el, offset) {
511
+ var sel = window.getSelection();
512
+ var range = document.createRange();
513
+ var walker = document.createTreeWalker(el, NodeFilter.SHOW_TEXT, null, false);
514
+ var pos = 0;
515
+ var node;
516
+ while ((node = walker.nextNode())) {
517
+ var nodeLen = node.textContent.length;
518
+ if (pos + nodeLen >= offset) {
519
+ range.setStart(node, offset - pos);
520
+ range.collapse(true);
521
+ sel.removeAllRanges();
522
+ sel.addRange(range);
523
+ return;
524
+ }
525
+ pos += nodeLen;
526
+ }
527
+ // If offset exceeds content, place at end
528
+ range.selectNodeContents(el);
529
+ range.collapse(false);
530
+ sel.removeAllRanges();
531
+ sel.addRange(range);
532
+ }
533
+
534
+ // -- Public: codeEditor -----------------------------------------------
535
+ function codeEditor(opts) {
536
+ opts = opts || {};
537
+ var code = opts.code || '';
538
+ var lang = opts.lang || 'js';
539
+ var height = opts.height || '180px';
540
+ var readOnly = !!opts.readOnly;
541
+ var className = 'bw-ce' + (opts.className ? ' ' + opts.className : '');
542
+
543
+ var highlighted = highlight(code, lang);
544
+
545
+ var codeAttrs = {
546
+ spellcheck: 'false',
547
+ class: 'bw-ce-code'
548
+ };
549
+ if (!readOnly) {
550
+ codeAttrs.contenteditable = 'true';
551
+ }
552
+
553
+ return {
554
+ t: 'div',
555
+ a: { class: className, style: 'max-height:' + height + ';overflow:auto' },
556
+ c: [
557
+ { t: 'pre', c: { t: 'code', a: codeAttrs, c: highlighted } }
558
+ ],
559
+ o: {
560
+ mounted: function(el) {
561
+ var codeEl = el.querySelector('.bw-ce-code');
562
+ if (!codeEl) return;
563
+
564
+ var currentCode = code;
565
+ var debounceTimer = null;
566
+
567
+ // Resolve bw from global or import context
568
+ var bw = (typeof window !== 'undefined' && window.bw) || {};
569
+
570
+ function getValue() { return codeEl.textContent || ''; }
571
+
572
+ function setValue(newCode) {
573
+ currentCode = newCode;
574
+ var tacos = highlight(newCode, lang);
575
+ if (bw.html) codeEl.innerHTML = bw.html({ t: 'span', c: tacos });
576
+ }
577
+
578
+ // Expose API on the element
579
+ el._bwCodeEdit = { getValue: getValue, setValue: setValue };
580
+
581
+ if (readOnly) return;
582
+
583
+ function rehighlight() {
584
+ var newCode = getValue();
585
+ if (newCode === currentCode) return;
586
+ currentCode = newCode;
587
+ var offset = getCaretOffset(codeEl);
588
+ var tacos = highlight(newCode, lang);
589
+ if (bw.html) codeEl.innerHTML = bw.html({ t: 'span', c: tacos });
590
+ setCaretOffset(codeEl, offset);
591
+ if (opts.onChange) opts.onChange(newCode);
592
+ }
593
+
594
+ codeEl.addEventListener('input', function() {
595
+ clearTimeout(debounceTimer);
596
+ debounceTimer = setTimeout(rehighlight, 50);
597
+ });
598
+
599
+ // Tab key: insert 2 spaces
600
+ codeEl.addEventListener('keydown', function(e) {
601
+ if (e.key === 'Tab') {
602
+ e.preventDefault();
603
+ document.execCommand('insertText', false, ' ');
604
+ }
605
+ });
606
+ }
607
+ }
608
+ };
609
+ }
610
+
611
+ // -- Auto-attach to bw when loaded as script tag ----------------------
612
+ function install(bw) {
613
+ if (!bw) return;
614
+ bw.highlight = highlight;
615
+ bw.codeEditor = function(opts) {
616
+ ensureCSS(bw);
617
+ return codeEditor(opts);
618
+ };
619
+ }
620
+
621
+ // Auto-install if bw is on window (script tag usage)
622
+ if (typeof window !== 'undefined' && window.bw) {
623
+ install(window.bw);
624
+ }
625
+ var bitwrenchCodeEdit = { highlight, codeEditor, install, CSS_TEXT };
626
+
627
+ export { CSS_TEXT, codeEditor, bitwrenchCodeEdit as default, highlight, install, tokenizeCSS, tokenizeHTML, tokenizeJS };
628
+ //# sourceMappingURL=bitwrench-code-edit.esm.js.map