markdown-actions 1.0.1 → 1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/markdown-actions.esm.js +84 -49
- package/dist/markdown-actions.esm.js.map +4 -4
- package/dist/markdown-actions.js +84 -49
- package/dist/markdown-actions.js.map +4 -4
- package/dist/markdown-actions.min.js +20 -20
- package/package.json +1 -1
- package/src/core/insertion.js +37 -21
- package/src/debug.js +25 -0
- package/src/index.js +9 -2
|
@@ -1,22 +1,22 @@
|
|
|
1
|
-
var markdownActions=(()=>{var
|
|
2
|
-
`).length>1}function
|
|
3
|
-
`),t=0;for(let l=0;l<n.length;l++){let i=n[l].length+1;e.selectionStart>=t&&e.selectionStart<t+i&&(e.selectionStart=t),e.selectionEnd>=t&&e.selectionEnd<t+i&&(l===n.length-1?e.selectionEnd=Math.min(t+n[l].length,e.value.length):e.selectionEnd=t+i-1),t+=i}}function
|
|
4
|
-
`.repeat(2-o)),t.match(/\S/)&&
|
|
5
|
-
`.repeat(2-
|
|
6
|
-
`;)
|
|
7
|
-
`;)c++;e.selectionStart=
|
|
8
|
-
`:i,m=
|
|
9
|
-
${s}`:o;if(
|
|
10
|
-
`);if(
|
|
11
|
-
`),s=
|
|
12
|
-
`),i){let{newlinesToAppend:c,newlinesToPrepend:
|
|
1
|
+
var markdownActions=(()=>{var R=Object.defineProperty;var be=Object.getOwnPropertyDescriptor;var Le=Object.getOwnPropertyNames,z=Object.getOwnPropertySymbols;var X=Object.prototype.hasOwnProperty,ve=Object.prototype.propertyIsEnumerable;var G=(e,n,t)=>n in e?R(e,n,{enumerable:!0,configurable:!0,writable:!0,value:t}):e[n]=t,P=(e,n)=>{for(var t in n||(n={}))X.call(n,t)&&G(e,t,n[t]);if(z)for(var t of z(n))ve.call(n,t)&&G(e,t,n[t]);return e};var Te=(e,n)=>{for(var t in n)R(e,t,{get:n[t],enumerable:!0})},ye=(e,n,t,l)=>{if(n&&typeof n=="object"||typeof n=="function")for(let i of Le(n))!X.call(e,i)&&i!==t&&R(e,i,{get:()=>n[i],enumerable:!(l=be(n,i))||l.enumerable});return e};var ae=e=>ye(R({},"__esModule",{value:!0}),e);var Ne={};Te(Ne,{applyCustomFormat:()=>Ee,default:()=>We,expandSelection:()=>me,getActiveFormats:()=>he,getDebugMode:()=>W,hasFormat:()=>Se,insertHeader:()=>$,insertLink:()=>se,preserveSelection:()=>V,setDebugMode:()=>B,setUndoMethod:()=>D,toggleBold:()=>le,toggleBulletList:()=>ce,toggleCode:()=>oe,toggleH1:()=>fe,toggleH2:()=>pe,toggleH3:()=>ge,toggleItalic:()=>ie,toggleNumberedList:()=>re,toggleQuote:()=>ue,toggleTaskList:()=>de});var w={bold:{prefix:"**",suffix:"**",trimFirst:!0},italic:{prefix:"_",suffix:"_",trimFirst:!0},code:{prefix:"`",suffix:"`",blockPrefix:"```",blockSuffix:"```"},link:{prefix:"[",suffix:"](url)",replaceNext:"url",scanFor:"https?://"},bulletList:{prefix:"- ",multiline:!0,unorderedList:!0},numberedList:{prefix:"1. ",multiline:!0,orderedList:!0},quote:{prefix:"> ",multiline:!0,surroundWithNewlines:!0},taskList:{prefix:"- [ ] ",multiline:!0,surroundWithNewlines:!0},header1:{prefix:"# "},header2:{prefix:"## "},header3:{prefix:"### "},header4:{prefix:"#### "},header5:{prefix:"##### "},header6:{prefix:"###### "}};function Oe(){return{prefix:"",suffix:"",blockPrefix:"",blockSuffix:"",multiline:!1,replaceNext:"",prefixSpace:!1,scanFor:"",surroundWithNewlines:!1,orderedList:!1,unorderedList:!1,trimFirst:!1}}function T(e){return P(P({},Oe()),e)}var x=!1;function B(e){x=e}function W(){return x}function I(e,n,t){x&&(console.group(`\u{1F50D} ${e}`),console.log(n),t&&console.log("Data:",t),console.groupEnd())}function N(e,n){if(!x)return;let t=e.value.slice(e.selectionStart,e.selectionEnd);console.group(`\u{1F4CD} Selection: ${n}`),console.log("Position:",`${e.selectionStart}-${e.selectionEnd}`),console.log("Selected text:",JSON.stringify(t)),console.log("Length:",t.length);let l=e.value.slice(Math.max(0,e.selectionStart-10),e.selectionStart),i=e.value.slice(e.selectionEnd,Math.min(e.value.length,e.selectionEnd+10));console.log("Context:",JSON.stringify(l)+"[SELECTION]"+JSON.stringify(i)),console.groupEnd()}function U(e){x&&(console.group("\u{1F4DD} Result"),console.log("Text to insert:",JSON.stringify(e.text)),console.log("New selection:",`${e.selectionStart}-${e.selectionEnd}`),console.groupEnd())}var y=null;function a(e,{text:n,selectionStart:t,selectionEnd:l}){let i=W();i&&(console.group("\u{1F527} insertText"),console.log("Current selection:",`${e.selectionStart}-${e.selectionEnd}`),console.log("Text to insert:",JSON.stringify(n)),console.log("New selection to set:",t,"-",l)),e.focus();let o=e.selectionStart,f=e.selectionEnd,s=e.value.slice(0,o),r=e.value.slice(f);i&&(console.log("Before text (last 20):",JSON.stringify(s.slice(-20))),console.log("After text (first 20):",JSON.stringify(r.slice(0,20))),console.log("Selected text being replaced:",JSON.stringify(e.value.slice(o,f))));let d=e.value,c=o!==f;if(!c&&(y===null||y===!0)){e.contentEditable="true";try{y=document.execCommand("insertText",!1,n)}catch(u){y=!1}e.contentEditable="false"}else c&&(i&&console.log("Has selection, skipping execCommand and using manual mode"),y=!1);if(i&&(console.log("canInsertText before:",y),console.log("execCommand result:",y)),y){let u=s+n+r,p=e.value;i&&(console.log("Expected length:",u.length),console.log("Actual length:",p.length)),p!==u&&i&&(console.log("execCommand changed the value but not as expected"),console.log("Expected:",JSON.stringify(u.slice(0,100))),console.log("Actual:",JSON.stringify(p.slice(0,100))))}if(!y)if(i&&console.log("Using manual insertion"),e.value===d){i&&console.log("Value unchanged, doing manual replacement");try{document.execCommand("ms-beginUndoUnit")}catch(u){}e.value=s+n+r;try{document.execCommand("ms-endUndoUnit")}catch(u){}e.dispatchEvent(new CustomEvent("input",{bubbles:!0,cancelable:!0}))}else i&&console.log("Value was changed by execCommand, skipping manual insertion");i&&console.log("Setting selection range:",t,l),t!=null&&l!=null?e.setSelectionRange(t,l):e.setSelectionRange(o,e.selectionEnd),i&&(console.log("Final value length:",e.value.length),console.groupEnd())}function D(e){switch(e){case"native":y=!0;break;case"manual":y=!1;break;case"auto":y=null;break}}function k(e){return e.trim().split(`
|
|
2
|
+
`).length>1}function we(e,n){let t=n;for(;e[t]&&e[t-1]!=null&&!e[t-1].match(/\s/);)t--;return t}function Me(e,n,t){let l=n,i=t?/\n/:/\s/;for(;e[l]&&!e[l].match(i);)l++;return l}function J(e){let n=e.value.split(`
|
|
3
|
+
`),t=0;for(let l=0;l<n.length;l++){let i=n[l].length+1;e.selectionStart>=t&&e.selectionStart<t+i&&(e.selectionStart=t),e.selectionEnd>=t&&e.selectionEnd<t+i&&(l===n.length-1?e.selectionEnd=Math.min(t+n[l].length,e.value.length):e.selectionEnd=t+i-1),t+=i}}function Y(e,n,t,l=!1){if(e.selectionStart===e.selectionEnd)e.selectionStart=we(e.value,e.selectionStart),e.selectionEnd=Me(e.value,e.selectionEnd,l);else{let i=e.selectionStart-n.length,o=e.selectionEnd+t.length,f=e.value.slice(i,e.selectionStart)===n,s=e.value.slice(e.selectionEnd,o)===t;f&&s&&(e.selectionStart=i,e.selectionEnd=o)}return e.value.slice(e.selectionStart,e.selectionEnd)}function C(e){let n=e.value.slice(0,e.selectionStart),t=e.value.slice(e.selectionEnd),l=n.match(/\n*$/),i=t.match(/^\n*/),o=l?l[0].length:0,f=i?i[0].length:0,s="",r="";return n.match(/\S/)&&o<2&&(s=`
|
|
4
|
+
`.repeat(2-o)),t.match(/\S/)&&f<2&&(r=`
|
|
5
|
+
`.repeat(2-f)),{newlinesToAppend:s,newlinesToPrepend:r}}function V(e,n){let t=e.selectionStart,l=e.selectionEnd,i=e.scrollTop;n(),e.selectionStart=t,e.selectionEnd=l,e.scrollTop=i}function _(e,n,t={}){let l=e.selectionStart,i=e.selectionEnd,o=l===i;if(o){let s=e.value,r=l,d=r,c=r;for(;d>0&&s[d-1]!==`
|
|
6
|
+
`;)d--;for(;c<s.length&&s[c]!==`
|
|
7
|
+
`;)c++;e.selectionStart=d,e.selectionEnd=c}else J(e);let f=n(e);return o&&t.prefix&&(e.value.slice(e.selectionStart,e.selectionEnd).startsWith(t.prefix)?(f.selectionStart=Math.max(l-t.prefix.length,e.selectionStart),f.selectionEnd=f.selectionStart):(f.selectionStart=l+t.prefix.length,f.selectionEnd=f.selectionStart)),f}function M(e,n){let t,l,{prefix:i,suffix:o,blockPrefix:f,blockSuffix:s,replaceNext:r,prefixSpace:d,scanFor:c,surroundWithNewlines:u,trimFirst:p}=n,b=e.selectionStart,v=e.selectionEnd,S=e.value.slice(e.selectionStart,e.selectionEnd),h=k(S)&&f&&f.length>0?`${f}
|
|
8
|
+
`:i,m=k(S)&&s&&s.length>0?`
|
|
9
|
+
${s}`:o;if(d){let E=e.value[e.selectionStart-1];e.selectionStart!==0&&E!=null&&!E.match(/\s/)&&(h=` ${h}`)}S=Y(e,h,m,n.multiline);let g=e.selectionStart,L=e.selectionEnd,q=r&&r.length>0&&m.indexOf(r)>-1&&S.length>0;if(u){let E=C(e);t=E.newlinesToAppend,l=E.newlinesToPrepend,h=t+i,m+=l}if(S.startsWith(h)&&S.endsWith(m)){let E=S.slice(h.length,S.length-m.length);if(b===v){let O=b-h.length;O=Math.max(O,g),O=Math.min(O,g+E.length),g=L=O}else L=g+E.length;return{text:E,selectionStart:g,selectionEnd:L}}else if(q)if(c&&c.length>0&&S.match(c)){m=m.replace(r,S);let E=h+m;return g=L=g+h.length,{text:E,selectionStart:g,selectionEnd:L}}else{let E=h+S+m;return g=g+h.length+S.length+m.indexOf(r),L=g+r.length,{text:E,selectionStart:g,selectionEnd:L}}else{let E=h+S+m;g=b+h.length,L=v+h.length;let O=S.match(/^\s*|\s*$/g);if(p&&O){let Q=O[0]||"",K=O[1]||"";E=Q+h+S.trim()+m+K,g+=Q.length,L-=K.length}return{text:E,selectionStart:g,selectionEnd:L}}}function F(e,n){let{prefix:t,suffix:l,surroundWithNewlines:i}=n,o=e.value.slice(e.selectionStart,e.selectionEnd),f=e.selectionStart,s=e.selectionEnd,r=o.split(`
|
|
10
|
+
`);if(r.every(c=>c.startsWith(t)&&(!l||c.endsWith(l))))o=r.map(c=>{let u=c.slice(t.length);return l&&(u=u.slice(0,u.length-l.length)),u}).join(`
|
|
11
|
+
`),s=f+o.length;else if(o=r.map(c=>t+c+(l||"")).join(`
|
|
12
|
+
`),i){let{newlinesToAppend:c,newlinesToPrepend:u}=C(e);f+=c.length,s=f+o.length,o=c+o+u}return{text:o,selectionStart:f,selectionEnd:s}}function Z(e){let n=e.split(`
|
|
13
13
|
`),t=/^\d+\.\s+/,l=n.every(o=>t.test(o)),i=n;return l&&(i=n.map(o=>o.replace(t,""))),{text:i.join(`
|
|
14
|
-
`),processed:l}}function
|
|
14
|
+
`),processed:l}}function ee(e){let n=e.split(`
|
|
15
15
|
`),t="- ",l=n.every(o=>o.startsWith(t)),i=n;return l&&(i=n.map(o=>o.slice(t.length))),{text:i.join(`
|
|
16
|
-
`),processed:l}}function
|
|
17
|
-
`).map((S,
|
|
18
|
-
`)+b;return t?(l=Math.max(l+
|
|
19
|
-
`),
|
|
20
|
-
`),
|
|
21
|
-
`;)
|
|
22
|
-
`;)c++;e.selectionStart=
|
|
16
|
+
`),processed:l}}function A(e,n){return n?"- ":`${e+1}. `}function ke(e,n){let t,l,i;return e.orderedList?(t=Z(n),l=ee(t.text),i=l.text):(t=ee(n),l=Z(t.text),i=l.text),[t,l,i]}function xe(e,n){let t=e.selectionStart===e.selectionEnd,l=e.selectionStart,i=e.selectionEnd;J(e);let o=e.value.slice(e.selectionStart,e.selectionEnd),[f,s,r]=ke(n,o),d=r.split(`
|
|
17
|
+
`).map((S,h)=>`${A(h,n.unorderedList)}${S}`),c=d.reduce((S,h,m)=>S+A(m,n.unorderedList).length,0),u=d.reduce((S,h,m)=>S+A(m,!n.unorderedList).length,0);if(f.processed)return t?(l=Math.max(l-A(0,n.unorderedList).length,0),i=l):(l=e.selectionStart,i=e.selectionEnd-c),{text:r,selectionStart:l,selectionEnd:i};let{newlinesToAppend:p,newlinesToPrepend:b}=C(e),v=p+d.join(`
|
|
18
|
+
`)+b;return t?(l=Math.max(l+A(0,n.unorderedList).length+p.length,0),i=l):s.processed?(l=Math.max(e.selectionStart+p.length,0),i=e.selectionEnd+p.length+c-u):(l=Math.max(e.selectionStart+p.length,0),i=e.selectionEnd+p.length+c),{text:v,selectionStart:l,selectionEnd:i}}function H(e,n){let t=e.selectionStart,l=e.selectionEnd,i=xe(e,n);a(e,i)}function j(e){if(!e)return[];let n=[],{selectionStart:t,selectionEnd:l,value:i}=e,o=i.split(`
|
|
19
|
+
`),f=0,s="";for(let u of o){if(t>=f&&t<=f+u.length){s=u;break}f+=u.length+1}s.startsWith("- ")&&(s.startsWith("- [ ] ")||s.startsWith("- [x] ")?n.push("task-list"):n.push("bullet-list")),/^\d+\.\s/.test(s)&&n.push("numbered-list"),s.startsWith("> ")&&n.push("quote"),s.startsWith("# ")&&n.push("header"),s.startsWith("## ")&&n.push("header-2"),s.startsWith("### ")&&n.push("header-3");let r=Math.max(0,t-10),d=Math.min(i.length,l+10),c=i.slice(r,d);if(c.includes("**")){let u=i.slice(Math.max(0,t-100),t),p=i.slice(l,Math.min(i.length,l+100)),b=u.lastIndexOf("**"),v=p.indexOf("**");b!==-1&&v!==-1&&n.push("bold")}if(c.includes("_")){let u=i.slice(Math.max(0,t-100),t),p=i.slice(l,Math.min(i.length,l+100)),b=u.lastIndexOf("_"),v=p.indexOf("_");b!==-1&&v!==-1&&n.push("italic")}if(c.includes("`")){let u=i.slice(Math.max(0,t-100),t),p=i.slice(l,Math.min(i.length,l+100));u.includes("`")&&p.includes("`")&&n.push("code")}if(c.includes("[")&&c.includes("]")){let u=i.slice(Math.max(0,t-100),t),p=i.slice(l,Math.min(i.length,l+100)),b=u.lastIndexOf("["),v=p.indexOf("]");b!==-1&&v!==-1&&i.slice(l+v+1,l+v+10).startsWith("(")&&n.push("link")}return n}function ne(e,n){return j(e).includes(n)}function te(e,n={}){if(!e)return;let{toWord:t,toLine:l,toFormat:i}=n,{selectionStart:o,selectionEnd:f,value:s}=e;if(l){let r=s.split(`
|
|
20
|
+
`),d=0,c=0,u=0;for(let p of r){if(o>=u&&o<=u+p.length){d=u,c=u+p.length;break}u+=p.length+1}e.selectionStart=d,e.selectionEnd=c}else if(t&&o===f){let r=o,d=f;for(;r>0&&!/\s/.test(s[r-1]);)r--;for(;d<s.length&&!/\s/.test(s[d]);)d++;e.selectionStart=r,e.selectionEnd=d}}function le(e){if(!e||e.disabled||e.readOnly)return;I("toggleBold","Starting"),N(e,"Before");let n=T(w.bold),t=M(e,n);U(t),a(e,t),N(e,"After")}function ie(e){if(!e||e.disabled||e.readOnly)return;let n=T(w.italic),t=M(e,n);a(e,t)}function oe(e){if(!e||e.disabled||e.readOnly)return;let n=e.value.slice(e.selectionStart,e.selectionEnd),t=T(w.code),l;k(n)?l=F(e,T({prefix:"```\n",suffix:"\n```",surroundWithNewlines:!0})):l=M(e,t),a(e,l)}function se(e,n={}){if(!e||e.disabled||e.readOnly)return;let t=e.value.slice(e.selectionStart,e.selectionEnd),l=T(w.link);if(n.url&&(l.suffix=`](${n.url})`,l.replaceNext=""),n.text&&!t){let o=e.selectionStart;e.value=e.value.slice(0,o)+n.text+e.value.slice(o),e.selectionStart=o,e.selectionEnd=o+n.text.length}let i=M(e,l);a(e,i)}function ce(e){if(!e||e.disabled||e.readOnly)return;let n=T(w.bulletList);H(e,n)}function re(e){if(!e||e.disabled||e.readOnly)return;let n=T(w.numberedList);H(e,n)}function ue(e){if(!e||e.disabled||e.readOnly)return;I("toggleQuote","Starting"),N(e,"Initial");let n=T(w.quote),t=_(e,l=>F(l,n),{prefix:n.prefix});U(t),a(e,t),N(e,"Final")}function de(e){if(!e||e.disabled||e.readOnly)return;let n=T(w.taskList),t=_(e,l=>F(l,n),{prefix:n.prefix});a(e,t)}function $(e,n=1,t=!1){if(!e||e.disabled||e.readOnly)return;(n<1||n>6)&&(n=1);let l=e.selectionStart,i=e.selectionEnd,o=l===i,f=`header${n===1?"1":n}`,s=T(w[f]||w.header1),r=e.value,d=l,c=l;for(;d>0&&r[d-1]!==`
|
|
21
|
+
`;)d--;for(;c<r.length&&r[c]!==`
|
|
22
|
+
`;)c++;e.selectionStart=d,e.selectionEnd=c;let u=r.slice(d,c),p=u.match(/^(#{1,6})\s*/),b=p?p[0].length:0,v=p?p[1].length:0,S=u.replace(/^#{1,6}\s*/,""),h,m=!1;t&&v===n?(h=S,m=!0):h=s.prefix+S;let g,L;if(o){let E=l-d;if(m)E<b?g=d:g=l-b;else if(b>0)if(E<b)g=d+Math.min(E,s.prefix.length);else{let O=s.prefix.length-b;g=l+O}else g=l+s.prefix.length;g=Math.min(g,d+h.length),L=g}else m?(g=d,L=d+h.length):(g=d+s.prefix.length,L=d+h.length);a(e,{text:h,selectionStart:g,selectionEnd:L})}function fe(e){$(e,1,!0)}function pe(e){$(e,2,!0)}function ge(e){$(e,3,!0)}function he(e){return j(e)}function Se(e,n){return ne(e,n)}function me(e,n={}){te(e,n)}function Ee(e,n){if(!e||e.disabled||e.readOnly)return;let t=T(n),l;if(t.multiline){let i=e.value.slice(e.selectionStart,e.selectionEnd);k(i)?l=F(e,t):l=M(e,t)}else l=M(e,t);a(e,l)}var We={toggleBold:le,toggleItalic:ie,toggleCode:oe,insertLink:se,toggleBulletList:ce,toggleNumberedList:re,toggleQuote:ue,toggleTaskList:de,insertHeader:$,toggleH1:fe,toggleH2:pe,toggleH3:ge,getActiveFormats:he,hasFormat:Se,expandSelection:me,applyCustomFormat:Ee,preserveSelection:V,setUndoMethod:D,setDebugMode:B,getDebugMode:W};return ae(Ne);})();
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "markdown-actions",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.2",
|
|
4
4
|
"description": "A lightweight, zero-dependency JavaScript library for adding markdown formatting functionality to any textarea element",
|
|
5
5
|
"main": "dist/markdown-actions.js",
|
|
6
6
|
"module": "dist/markdown-actions.esm.js",
|
package/src/core/insertion.js
CHANGED
|
@@ -3,6 +3,8 @@
|
|
|
3
3
|
* Extracted and adapted from GitHub's markdown-toolbar-element
|
|
4
4
|
*/
|
|
5
5
|
|
|
6
|
+
import { getDebugMode } from '../debug.js'
|
|
7
|
+
|
|
6
8
|
let canInsertText = null
|
|
7
9
|
|
|
8
10
|
/**
|
|
@@ -14,10 +16,14 @@ let canInsertText = null
|
|
|
14
16
|
* @param {number} [options.selectionEnd] - New selection end
|
|
15
17
|
*/
|
|
16
18
|
export function insertText(textarea, { text, selectionStart, selectionEnd }) {
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
19
|
+
const debugMode = getDebugMode();
|
|
20
|
+
|
|
21
|
+
if (debugMode) {
|
|
22
|
+
console.group('🔧 insertText');
|
|
23
|
+
console.log('Current selection:', `${textarea.selectionStart}-${textarea.selectionEnd}`);
|
|
24
|
+
console.log('Text to insert:', JSON.stringify(text));
|
|
25
|
+
console.log('New selection to set:', selectionStart, '-', selectionEnd);
|
|
26
|
+
}
|
|
21
27
|
|
|
22
28
|
// Make sure the textarea is focused
|
|
23
29
|
textarea.focus();
|
|
@@ -27,9 +33,11 @@ export function insertText(textarea, { text, selectionStart, selectionEnd }) {
|
|
|
27
33
|
const before = textarea.value.slice(0, originalSelectionStart)
|
|
28
34
|
const after = textarea.value.slice(originalSelectionEnd)
|
|
29
35
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
36
|
+
if (debugMode) {
|
|
37
|
+
console.log('Before text (last 20):', JSON.stringify(before.slice(-20)));
|
|
38
|
+
console.log('After text (first 20):', JSON.stringify(after.slice(0, 20)));
|
|
39
|
+
console.log('Selected text being replaced:', JSON.stringify(textarea.value.slice(originalSelectionStart, originalSelectionEnd)));
|
|
40
|
+
}
|
|
33
41
|
|
|
34
42
|
// Store the original value to check if execCommand actually changed it
|
|
35
43
|
const originalValue = textarea.value
|
|
@@ -47,35 +55,41 @@ export function insertText(textarea, { text, selectionStart, selectionEnd }) {
|
|
|
47
55
|
}
|
|
48
56
|
textarea.contentEditable = 'false'
|
|
49
57
|
} else if (hasSelection) {
|
|
50
|
-
console.log('Has selection, skipping execCommand and using manual mode');
|
|
58
|
+
if (debugMode) console.log('Has selection, skipping execCommand and using manual mode');
|
|
51
59
|
canInsertText = false
|
|
52
60
|
}
|
|
53
61
|
|
|
54
|
-
|
|
55
|
-
|
|
62
|
+
if (debugMode) {
|
|
63
|
+
console.log('canInsertText before:', canInsertText);
|
|
64
|
+
console.log('execCommand result:', canInsertText);
|
|
65
|
+
}
|
|
56
66
|
|
|
57
67
|
// Check if execCommand actually worked by comparing the value
|
|
58
68
|
if (canInsertText) {
|
|
59
69
|
const expectedValue = before + text + after
|
|
60
70
|
const actualValue = textarea.value
|
|
61
71
|
|
|
62
|
-
|
|
63
|
-
|
|
72
|
+
if (debugMode) {
|
|
73
|
+
console.log('Expected length:', expectedValue.length);
|
|
74
|
+
console.log('Actual length:', actualValue.length);
|
|
75
|
+
}
|
|
64
76
|
|
|
65
77
|
if (actualValue !== expectedValue) {
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
78
|
+
if (debugMode) {
|
|
79
|
+
console.log('execCommand changed the value but not as expected');
|
|
80
|
+
console.log('Expected:', JSON.stringify(expectedValue.slice(0, 100)));
|
|
81
|
+
console.log('Actual:', JSON.stringify(actualValue.slice(0, 100)));
|
|
82
|
+
}
|
|
69
83
|
// Don't set canInsertText to false here - execCommand did work
|
|
70
84
|
// We just need to not double-insert
|
|
71
85
|
}
|
|
72
86
|
}
|
|
73
87
|
|
|
74
88
|
if (!canInsertText) {
|
|
75
|
-
console.log('Using manual insertion');
|
|
89
|
+
if (debugMode) console.log('Using manual insertion');
|
|
76
90
|
// Only do manual insertion if execCommand didn't change the value
|
|
77
91
|
if (textarea.value === originalValue) {
|
|
78
|
-
console.log('Value unchanged, doing manual replacement');
|
|
92
|
+
if (debugMode) console.log('Value unchanged, doing manual replacement');
|
|
79
93
|
try {
|
|
80
94
|
document.execCommand('ms-beginUndoUnit')
|
|
81
95
|
} catch (e) {
|
|
@@ -89,19 +103,21 @@ export function insertText(textarea, { text, selectionStart, selectionEnd }) {
|
|
|
89
103
|
}
|
|
90
104
|
textarea.dispatchEvent(new CustomEvent('input', { bubbles: true, cancelable: true }))
|
|
91
105
|
} else {
|
|
92
|
-
console.log('Value was changed by execCommand, skipping manual insertion');
|
|
106
|
+
if (debugMode) console.log('Value was changed by execCommand, skipping manual insertion');
|
|
93
107
|
}
|
|
94
108
|
}
|
|
95
109
|
|
|
96
|
-
console.log('Setting selection range:', selectionStart, selectionEnd);
|
|
110
|
+
if (debugMode) console.log('Setting selection range:', selectionStart, selectionEnd);
|
|
97
111
|
if (selectionStart != null && selectionEnd != null) {
|
|
98
112
|
textarea.setSelectionRange(selectionStart, selectionEnd)
|
|
99
113
|
} else {
|
|
100
114
|
textarea.setSelectionRange(originalSelectionStart, textarea.selectionEnd)
|
|
101
115
|
}
|
|
102
116
|
|
|
103
|
-
|
|
104
|
-
|
|
117
|
+
if (debugMode) {
|
|
118
|
+
console.log('Final value length:', textarea.value.length);
|
|
119
|
+
console.groupEnd();
|
|
120
|
+
}
|
|
105
121
|
}
|
|
106
122
|
|
|
107
123
|
/**
|
package/src/debug.js
CHANGED
|
@@ -3,7 +3,28 @@
|
|
|
3
3
|
* Add console logging to track what's happening
|
|
4
4
|
*/
|
|
5
5
|
|
|
6
|
+
// Debug mode flag - disabled by default
|
|
7
|
+
let debugMode = false;
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Enable or disable debug mode
|
|
11
|
+
* @param {boolean} enabled - Whether to enable debug mode
|
|
12
|
+
*/
|
|
13
|
+
export function setDebugMode(enabled) {
|
|
14
|
+
debugMode = enabled;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Get current debug mode status
|
|
19
|
+
* @returns {boolean} Whether debug mode is enabled
|
|
20
|
+
*/
|
|
21
|
+
export function getDebugMode() {
|
|
22
|
+
return debugMode;
|
|
23
|
+
}
|
|
24
|
+
|
|
6
25
|
export function debugLog(funcName, message, data) {
|
|
26
|
+
if (!debugMode) return;
|
|
27
|
+
|
|
7
28
|
console.group(`🔍 ${funcName}`);
|
|
8
29
|
console.log(message);
|
|
9
30
|
if (data) {
|
|
@@ -13,6 +34,8 @@ export function debugLog(funcName, message, data) {
|
|
|
13
34
|
}
|
|
14
35
|
|
|
15
36
|
export function debugSelection(textarea, label) {
|
|
37
|
+
if (!debugMode) return;
|
|
38
|
+
|
|
16
39
|
const selected = textarea.value.slice(textarea.selectionStart, textarea.selectionEnd);
|
|
17
40
|
console.group(`📍 Selection: ${label}`);
|
|
18
41
|
console.log('Position:', `${textarea.selectionStart}-${textarea.selectionEnd}`);
|
|
@@ -27,6 +50,8 @@ export function debugSelection(textarea, label) {
|
|
|
27
50
|
}
|
|
28
51
|
|
|
29
52
|
export function debugResult(result) {
|
|
53
|
+
if (!debugMode) return;
|
|
54
|
+
|
|
30
55
|
console.group('📝 Result');
|
|
31
56
|
console.log('Text to insert:', JSON.stringify(result.text));
|
|
32
57
|
console.log('New selection:', `${result.selectionStart}-${result.selectionEnd}`);
|
package/src/index.js
CHANGED
|
@@ -9,7 +9,7 @@ import { preserveSelection, isMultipleLines, expandSelectionToLine, applyLineOpe
|
|
|
9
9
|
import { blockStyle, multilineStyle } from './operations/block.js'
|
|
10
10
|
import { applyListStyle } from './operations/list.js'
|
|
11
11
|
import { getActiveFormats as getActive, hasFormat as has, expandSelection as expand } from './core/detection.js'
|
|
12
|
-
import { debugLog, debugSelection, debugResult } from './debug.js'
|
|
12
|
+
import { debugLog, debugSelection, debugResult, setDebugMode, getDebugMode } from './debug.js'
|
|
13
13
|
|
|
14
14
|
/**
|
|
15
15
|
* Toggle bold formatting
|
|
@@ -336,6 +336,11 @@ export { preserveSelection }
|
|
|
336
336
|
*/
|
|
337
337
|
export { setUndoMethod }
|
|
338
338
|
|
|
339
|
+
/**
|
|
340
|
+
* Debug mode control
|
|
341
|
+
*/
|
|
342
|
+
export { setDebugMode, getDebugMode }
|
|
343
|
+
|
|
339
344
|
/**
|
|
340
345
|
* Default export with all functions
|
|
341
346
|
*/
|
|
@@ -357,5 +362,7 @@ export default {
|
|
|
357
362
|
expandSelection,
|
|
358
363
|
applyCustomFormat,
|
|
359
364
|
preserveSelection,
|
|
360
|
-
setUndoMethod
|
|
365
|
+
setUndoMethod,
|
|
366
|
+
setDebugMode,
|
|
367
|
+
getDebugMode
|
|
361
368
|
}
|