react-marky-markdown
Advanced tools
Comparing version 0.4.0 to 0.5.0
@@ -7,2 +7,6 @@ # Changelog | ||
## 0.5.0 2019-11-07 | ||
- Add autocomplete for enclosing types like `(` and `[` | ||
- Double enter when in a list will now exit the list. | ||
## 0.4.0 2019-10-29 | ||
@@ -9,0 +13,0 @@ ### Added |
@@ -18,2 +18,3 @@ import TextareaAutosize from "react-autosize-textarea"; | ||
valueUpToStart: string; | ||
lastSelectionStart: number; | ||
} | ||
@@ -47,2 +48,6 @@ interface State { | ||
}; | ||
declare type ClearListAction = { | ||
type: 'ClearList'; | ||
payload: HTMLTextAreaElement; | ||
}; | ||
declare type BoldAction = { | ||
@@ -56,3 +61,10 @@ type: 'ToggleBold'; | ||
}; | ||
declare type Action = EditorDataAction | EditorSizeAction | TabAction | ListItemAction | BoldAction | ItalicAction; | ||
declare type EncloseAction = { | ||
type: 'Enclose'; | ||
payload: { | ||
el: HTMLTextAreaElement; | ||
type: '[' | '(' | '{'; | ||
}; | ||
}; | ||
declare type Action = EditorDataAction | EditorSizeAction | TabAction | ListItemAction | ClearListAction | BoldAction | ItalicAction | EncloseAction; | ||
interface EditorContextValues { | ||
@@ -59,0 +71,0 @@ state: State; |
@@ -137,5 +137,6 @@ 'use strict'; | ||
} | ||
function getRowAtPosition(value, position) { | ||
function getRows(value, position) { | ||
var letterIndex = 0; | ||
var rows = value.split(/\n/g); | ||
var currentIndex = 0; | ||
@@ -147,3 +148,3 @@ for (var i = 0; i < rows.length; i++) { | ||
if (letterIndex >= position) { | ||
return row; | ||
currentIndex = i; | ||
} | ||
@@ -154,4 +155,14 @@ | ||
return ''; | ||
return { | ||
rows: rows, | ||
currentIndex: currentIndex | ||
}; | ||
} | ||
function getRowAtPosition(value, position) { | ||
var _getRows = getRows(value, position), | ||
rows = _getRows.rows, | ||
currentIndex = _getRows.currentIndex; | ||
return rows[currentIndex]; | ||
} | ||
function isCtrlCmd(event) { | ||
@@ -226,3 +237,5 @@ var isMacLike = /(Mac|iPhone|iPod|iPad)/i.test(navigator.platform); | ||
if (hasSelected) { | ||
el.setSelectionRange(caretStart - (insertStart || '').length, caretEnd + (insertEnd || '').length); | ||
caretStart = caretStart - (insertStart || '').length; | ||
caretEnd = caretEnd + (insertEnd || '').length; | ||
el.setSelectionRange(caretStart, caretEnd); | ||
} else { | ||
@@ -239,3 +252,4 @@ el.setSelectionRange(caretStart, caretStart); | ||
currentWord: currentWord, | ||
valueUpToStart: valueUpToStart | ||
valueUpToStart: valueUpToStart, | ||
lastSelectionStart: caretStart | ||
}; | ||
@@ -278,2 +292,14 @@ } | ||
case 'ClearList': | ||
var _getRows = getRows(action.payload.value, action.payload.selectionStart), | ||
rows = _getRows.rows, | ||
currentIndex = _getRows.currentIndex; | ||
var updatedRows = [].concat(rows); | ||
updatedRows[currentIndex] = ''; | ||
action.payload.value = updatedRows.join('\n'); | ||
return _extends({}, state, { | ||
editor: _extends({}, getEditorData(action.payload)) | ||
}); | ||
case 'ToggleBold': | ||
@@ -289,2 +315,24 @@ return _extends({}, state, { | ||
case 'Enclose': | ||
var _getWordAtPosition2 = getWordAtPosition(action.payload.el.value, action.payload.el.selectionStart, action.payload.el.selectionEnd), | ||
currentWord = _getWordAtPosition2[0], | ||
currentWordEnd = _getWordAtPosition2[2]; | ||
var startChar = action.payload.type; | ||
var endChar = startChar === '[' ? ']' : startChar === '(' ? ')' : '}'; | ||
if (action.payload.el.selectionStart !== action.payload.el.selectionEnd || currentWord === startChar) { | ||
return _extends({}, state, { | ||
editor: _extends({}, getEditorData(action.payload.el, startChar, endChar)) | ||
}); | ||
} else if (currentWordEnd === action.payload.el.selectionEnd) { | ||
return _extends({}, state, { | ||
editor: _extends({}, getEditorData(action.payload.el, startChar, endChar)) | ||
}); | ||
} | ||
return _extends({}, state, { | ||
editor: _extends({}, getEditorData(action.payload.el, action.payload.type)) | ||
}); | ||
default: | ||
@@ -299,3 +347,4 @@ return state; | ||
value: '', | ||
valueUpToStart: '' | ||
valueUpToStart: '', | ||
lastSelectionStart: 0 | ||
}, | ||
@@ -375,2 +424,10 @@ width: 0, | ||
if (isUL || isOL) { | ||
if (isUL && row === rowStartingChar || isOL && row.substr(0, 3) === row) { | ||
dispatch({ | ||
type: 'ClearList', | ||
payload: editor | ||
}); | ||
return; | ||
} | ||
dispatch({ | ||
@@ -414,2 +471,32 @@ type: 'AddListItem', | ||
} | ||
if (['[', '(', '{'].includes(event.key)) { | ||
dispatch({ | ||
type: 'Enclose', | ||
payload: { | ||
el: editor, | ||
type: event.key | ||
} | ||
}); | ||
event.preventDefault(); | ||
} | ||
if ([']', ')', '}'].includes(event.key)) { | ||
var noSelection = editor.selectionStart === editor.selectionEnd; | ||
var currentPosition = editor.selectionStart; | ||
var lastPosition = state.editor.lastSelectionStart; | ||
var lastCharacter = editor.value.substring(currentPosition - 1, currentPosition); | ||
var matching = { | ||
')': '(', | ||
']': '[', | ||
'}': '{' | ||
}; | ||
var eventKey = event.key; | ||
var matchingCharacter = matching[eventKey]; | ||
if (noSelection && currentPosition === lastPosition && lastCharacter === matchingCharacter) { | ||
editor.setSelectionRange(currentPosition + 1, currentPosition + 1); | ||
event.preventDefault(); | ||
} | ||
} | ||
} | ||
@@ -416,0 +503,0 @@ }; |
@@ -1,2 +0,2 @@ | ||
"use strict";function e(e){return e&&"object"==typeof e&&"default"in e?e.default:e}var t=require("react"),n=e(require("react-autosize-textarea")),r=e(require("resize-observer-polyfill"));function a(){return(a=Object.assign||function(e){for(var t=1;t<arguments.length;t++){var n=arguments[t];for(var r in n)Object.prototype.hasOwnProperty.call(n,r)&&(e[r]=n[r])}return e}).apply(this,arguments)}var o={x:0,y:0,width:0,height:0,top:0,right:0,bottom:0,left:0};function i(e,t,n){var r=0,a=e.replace(/\n/g," ").split(" ");if("number"==typeof n&&t!==n)return[e.substring(t,n),t,n];for(var o=0;o<a.length;o++){var i=a[o],u=r;if((r+=i.length)>=t)return[i.replace(/\r?\n|\r/g,"").trim(),u,u+i.length];r+=1}return["",0,0]}function u(e,t){for(var n=0,r=e.split(/\n/g),a=0;a<r.length;a++){var o=r[a];if((n+=o.length)>=t)return o;n+=1}return""}function l(e,t){var n=e.selectionStart,r=e.selectionEnd,a=n!==r,o=i(e.value,n,r),u=o[0],l=o[1],s=o[2],c=u.substr(0,t.length),d=u.substr(-t.length),f=e.value.substr(0,l),v=e.value.substr(s,e.value.length);if(c!==t||d!==t)e.value=""+f+t+u+t+v,a?e.setSelectionRange(n,r+2*t.length):e.setSelectionRange(n+t.length,r+t.length);else{var p=u.substr(t.length,u.length-2*t.length);e.value=""+f+p+v,a?e.setSelectionRange(n,r-2*t.length):e.setSelectionRange(n-t.length,r-t.length)}return e}function s(e,t,n){var r=e.selectionStart!==e.selectionEnd,a=e.value,o=e.selectionStart,u=e.selectionEnd,l=a.substr(0,o);if(t){var s=a.substr(o,a.length);o=(l+t).length,u+=t.length,e.value=a=l+t+s}if(n){var c=a.substr(u,a.length);u=((l=a.substr(0,u))+n).length,e.value=a=l+n+c}return(t||n)&&(r?e.setSelectionRange(o-(t||"").length,u+(n||"").length):e.setSelectionRange(o,o)),{value:a,currentWord:i(a,o)[0],valueUpToStart:l}}function c(e,t){switch(t.type){case"EditorData":return a({},e,{editor:a({},s(t.payload))});case"EditorSize":return a({},e,{},t.payload);case"AddTab":return a({},e,{editor:a({},s(t.payload," "))});case"AddListItem":var n="\n- ";if("ol"===t.payload.type){var r=u(t.payload.el.value,t.payload.el.selectionStart),o=parseFloat(r.substr(0,3));n=1===o?"\n1. ":"\n"+(o+1)+". "}return a({},e,{editor:a({},s(t.payload.el,n))});case"ToggleBold":return a({},e,{editor:a({},s(l(t.payload,"**")))});case"ToggleItalic":return a({},e,{editor:a({},s(l(t.payload,"_")))});default:return e}}var d={editor:{currentWord:"",value:"",valueUpToStart:""},width:0,height:0},f=t.createContext(void 0);exports.Editor=function(e){var i,l,s=e.defaultValue,v=e.onChange,p=e.onSubmit,g=e.onBlur,h=e.onCancel,y=e.disableFormatting,m=e.singleLine,b=e.children,E=function(e,t){if(null==e)return{};var n,r,a={},o=Object.keys(e);for(r=0;r<o.length;r++)t.indexOf(n=o[r])>=0||(a[n]=e[n]);return a}(e,["defaultValue","onChange","onSubmit","onBlur","onCancel","disableFormatting","singleLine","children"]),w=t.useRef(null),S=t.useRef(null),L=t.useReducer(c,d,(function(e){return a({},e,{editor:a({},e.editor,{value:s||""})})})),k=L[0],x=L[1],C=function(e,n){void 0===n&&(n=!1);var a=t.useState(o),i=a[0],u=a[1],l=t.useState((function(){return new r((function(e){return u(e[0].contentRect)}))}))[0];return t.useEffect((function(){if(e.current&&!n)return l.observe(e.current),function(){return l.disconnect()}}),[e,l,n]),i}(S),T=C.width,R=C.height;return t.useEffect((function(){var e=Array.isArray(i)?i:[i],t=function(t){e.reduce((function(e,n){return(!n.current||!n.current.contains(t.target))&&e}),!0)&&l(t)};return document.addEventListener("mousedown",t),document.addEventListener("touchstart",t),function(){document.removeEventListener("mousedown",t),document.removeEventListener("touchstart",t)}}),[i=w,l=function(){g&&g()}]),t.useEffect((function(){x({type:"EditorSize",payload:{width:T,height:R}})}),[T,R]),t.useEffect((function(){var e=S.current;if(e){var t=function(t){var n=function(e){var t=/(Mac|iPhone|iPod|iPad)/i.test(navigator.platform);return t&&e.metaKey||!t&&e.ctrlKey}(t);if(n&&"Enter"===t.key&&p&&p(),"Escape"===t.key&&h&&h(),"Enter"===t.key){if(!y){var r=u(e.value,e.selectionStart),a="- "===r.substr(0,2),o=new RegExp(/^[0-9]. /).test(r);(a||o)&&(x({type:"AddListItem",payload:{el:e,type:a?"ul":"ol"}}),t.preventDefault())}m&&t.preventDefault()}y||("Tab"===t.key&&(t.preventDefault(),x({type:"AddTab",payload:e})),n&&"b"===t.key&&x({type:"ToggleBold",payload:e}),n&&"i"===t.key&&x({type:"ToggleItalic",payload:e}))};return e.addEventListener("keydown",t,!1),function(){e.removeEventListener("keydown",t,!1)}}}),[p,h,g,y,m]),t.useEffect((function(){v&&v(k.editor.value)}),[v,k.editor.value]),t.createElement(f.Provider,{value:{state:k,dispatch:x,editorRef:S}},t.createElement("div",{"data-testid":"container",ref:w,className:"rmm-container"},t.createElement(n,Object.assign({},E,{"data-testid":"textarea",ref:S,className:"rmm-editor",wrap:"hard",onChange:function(e){x({type:"EditorData",payload:e.target})},value:k.editor.value})),b))},exports.EditorContet=f,exports.Mention=function(e){var n=e.prefix,r=e.data,a=e.onSearch,o=function(){var e=t.useContext(f);if(!e)throw new Error("Component has to be wrapped inside <Editor />");return e}(),i=o.state,u=o.dispatch,l=o.editorRef,s=i.editor,c=s.currentWord,d=s.valueUpToStart,v=s.value,p=i.width,g=c[0]===n,h=t.useState([0,0]),y=h[0],m=y[0],b=y[1],E=h[1],w=t.useState(0),S=w[0],L=w[1],k=c.replace(n,""),x=r.filter((function(e){return(""+e.value+e.label).toLowerCase().includes(k.toLowerCase())}));t.useEffect((function(){g&&a&&a(k)}),[g,k,a]),t.useEffect((function(){if(g&&l.current){var e=document.createElement("div"),t=d.split(/\n\r?/g),n=document.createElement("span");return e.classList.add("rmm-editor"),e.style.cssText="max-width: "+p+"px; position: absolute; top: 0; left: -9999px; opacity: 0; color: transparent; pointer-events: none; white-space: pre-wrap;",n.innerHTML=" ",n.style.position="absolute",e.innerHTML=t.join("<br />"),e.append(n),document.body.appendChild(e),E([n.offsetLeft,n.offsetTop+n.clientHeight+2]),function(){return document.body.removeChild(e)}}}),[g,p,l,d]);var C=t.useCallback((function(e,t){var r=v.slice(0,e.selectionStart-c.length)+""+n+t.value+" ",a=r+v.slice(e.selectionStart,v.length),o=r.length,i=r.length;e.value=a,e.setSelectionRange(o,i),u({type:"EditorData",payload:e}),L(0)}),[c.length,u,n,v]);if(t.useEffect((function(){var e=l.current;if(g){S>x.length&&L(0);var t=function(e){if("Enter"===e.key){var t=x[S]||x[0];if(!t)return;C(e.target,t),e.stopPropagation(),e.preventDefault()}"ArrowUp"===e.key&&(L((function(e){return Math.abs((e-1)%x.length)})),e.preventDefault()),"ArrowDown"===e.key&&(L((function(e){return Math.abs((e+1)%x.length)})),e.preventDefault())};if(e)return e.addEventListener("keydown",t,!1),function(){e.removeEventListener("keydown",t,!1)}}}),[g,c,v,n,x,S,u,l,C]),!g||!l.current)return null;var T=function(e){return function(){l.current&&(C(l.current,e),l.current.focus())}};return t.createElement("div",{className:"rmm-list",style:{transform:"translate("+m+"px, "+(b-l.current.scrollTop)+"px)"}},x.map((function(e,n){return t.createElement("div",{key:e.value,role:"button",className:"rmm-list-item "+(n===S&&"rmm-list-item-selected"),onMouseOver:function(){return L(n)},onClick:T(e)},t.createElement("b",null,e.value)," ",e.label)})))}; | ||
"use strict";function e(e){return e&&"object"==typeof e&&"default"in e?e.default:e}var t=require("react"),n=e(require("react-autosize-textarea")),r=e(require("resize-observer-polyfill"));function a(){return(a=Object.assign||function(e){for(var t=1;t<arguments.length;t++){var n=arguments[t];for(var r in n)Object.prototype.hasOwnProperty.call(n,r)&&(e[r]=n[r])}return e}).apply(this,arguments)}var o={x:0,y:0,width:0,height:0,top:0,right:0,bottom:0,left:0};function l(e,t,n){var r=0,a=e.replace(/\n/g," ").split(" ");if("number"==typeof n&&t!==n)return[e.substring(t,n),t,n];for(var o=0;o<a.length;o++){var l=a[o],i=r;if((r+=l.length)>=t)return[l.replace(/\r?\n|\r/g,"").trim(),i,i+l.length];r+=1}return["",0,0]}function i(e,t){for(var n=0,r=e.split(/\n/g),a=0,o=0;o<r.length;o++)(n+=r[o].length)>=t&&(a=o),n+=1;return{rows:r,currentIndex:a}}function u(e,t){var n=i(e,t);return n.rows[n.currentIndex]}function s(e,t){var n=e.selectionStart,r=e.selectionEnd,a=n!==r,o=l(e.value,n,r),i=o[0],u=o[1],s=o[2],c=i.substr(0,t.length),d=i.substr(-t.length),f=e.value.substr(0,u),p=e.value.substr(s,e.value.length);if(c!==t||d!==t)e.value=""+f+t+i+t+p,a?e.setSelectionRange(n,r+2*t.length):e.setSelectionRange(n+t.length,r+t.length);else{var v=i.substr(t.length,i.length-2*t.length);e.value=""+f+v+p,a?e.setSelectionRange(n,r-2*t.length):e.setSelectionRange(n-t.length,r-t.length)}return e}function c(e,t,n){var r=e.selectionStart!==e.selectionEnd,a=e.value,o=e.selectionStart,i=e.selectionEnd,u=a.substr(0,o);if(t){var s=a.substr(o,a.length);o=(u+t).length,i+=t.length,e.value=a=u+t+s}if(n){var c=a.substr(i,a.length);i=((u=a.substr(0,i))+n).length,e.value=a=u+n+c}return(t||n)&&(r?e.setSelectionRange(o-=(t||"").length,i+=(n||"").length):e.setSelectionRange(o,o)),{value:a,currentWord:l(a,o)[0],valueUpToStart:u,lastSelectionStart:o}}function d(e,t){switch(t.type){case"EditorData":return a({},e,{editor:a({},c(t.payload))});case"EditorSize":return a({},e,{},t.payload);case"AddTab":return a({},e,{editor:a({},c(t.payload," "))});case"AddListItem":var n="\n- ";if("ol"===t.payload.type){var r=u(t.payload.el.value,t.payload.el.selectionStart),o=parseFloat(r.substr(0,3));n=1===o?"\n1. ":"\n"+(o+1)+". "}return a({},e,{editor:a({},c(t.payload.el,n))});case"ClearList":var d=i(t.payload.value,t.payload.selectionStart),f=d.currentIndex,p=[].concat(d.rows);return p[f]="",t.payload.value=p.join("\n"),a({},e,{editor:a({},c(t.payload))});case"ToggleBold":return a({},e,{editor:a({},c(s(t.payload,"**")))});case"ToggleItalic":return a({},e,{editor:a({},c(s(t.payload,"_")))});case"Enclose":var v=l(t.payload.el.value,t.payload.el.selectionStart,t.payload.el.selectionEnd),y=v[2],g=t.payload.type,h="["===g?"]":"("===g?")":"}";return a({},e,t.payload.el.selectionStart!==t.payload.el.selectionEnd||v[0]===g?{editor:a({},c(t.payload.el,g,h))}:y===t.payload.el.selectionEnd?{editor:a({},c(t.payload.el,g,h))}:{editor:a({},c(t.payload.el,t.payload.type))});default:return e}}var f={editor:{currentWord:"",value:"",valueUpToStart:"",lastSelectionStart:0},width:0,height:0},p=t.createContext(void 0);exports.Editor=function(e){var l,i,s=e.defaultValue,c=e.onChange,v=e.onSubmit,y=e.onBlur,g=e.onCancel,h=e.disableFormatting,m=e.singleLine,E=e.children,b=function(e,t){if(null==e)return{};var n,r,a={},o=Object.keys(e);for(r=0;r<o.length;r++)t.indexOf(n=o[r])>=0||(a[n]=e[n]);return a}(e,["defaultValue","onChange","onSubmit","onBlur","onCancel","disableFormatting","singleLine","children"]),S=t.useRef(null),w=t.useRef(null),k=t.useReducer(d,f,(function(e){return a({},e,{editor:a({},e.editor,{value:s||""})})})),x=k[0],L=k[1],C=function(e,n){void 0===n&&(n=!1);var a=t.useState(o),l=a[0],i=a[1],u=t.useState((function(){return new r((function(e){return i(e[0].contentRect)}))}))[0];return t.useEffect((function(){if(e.current&&!n)return u.observe(e.current),function(){return u.disconnect()}}),[e,u,n]),l}(w),R=C.width,T=C.height;return t.useEffect((function(){var e=Array.isArray(l)?l:[l],t=function(t){e.reduce((function(e,n){return(!n.current||!n.current.contains(t.target))&&e}),!0)&&i(t)};return document.addEventListener("mousedown",t),document.addEventListener("touchstart",t),function(){document.removeEventListener("mousedown",t),document.removeEventListener("touchstart",t)}}),[l=S,i=function(){y&&y()}]),t.useEffect((function(){L({type:"EditorSize",payload:{width:R,height:T}})}),[R,T]),t.useEffect((function(){var e=w.current;if(e){var t=function(t){var n=function(e){var t=/(Mac|iPhone|iPod|iPad)/i.test(navigator.platform);return t&&e.metaKey||!t&&e.ctrlKey}(t);if(n&&"Enter"===t.key&&v&&v(),"Escape"===t.key&&g&&g(),"Enter"===t.key){if(!h){var r=u(e.value,e.selectionStart),a=r.substr(0,2),o="- "===a,l=new RegExp(/^[0-9]. /).test(r);if(o||l){if(o&&r===a||l&&r.substr(0,3)===r)return void L({type:"ClearList",payload:e});L({type:"AddListItem",payload:{el:e,type:o?"ul":"ol"}}),t.preventDefault()}}m&&t.preventDefault()}if(!h&&("Tab"===t.key&&(t.preventDefault(),L({type:"AddTab",payload:e})),n&&"b"===t.key&&L({type:"ToggleBold",payload:e}),n&&"i"===t.key&&L({type:"ToggleItalic",payload:e}),["[","(","{"].includes(t.key)&&(L({type:"Enclose",payload:{el:e,type:t.key}}),t.preventDefault()),["]",")","}"].includes(t.key))){var i=e.selectionStart===e.selectionEnd,s=e.selectionStart,c=x.editor.lastSelectionStart,d=e.value.substring(s-1,s);i&&s===c&&d==={")":"(","]":"[","}":"{"}[t.key]&&(e.setSelectionRange(s+1,s+1),t.preventDefault())}};return e.addEventListener("keydown",t,!1),function(){e.removeEventListener("keydown",t,!1)}}}),[v,g,y,h,m]),t.useEffect((function(){c&&c(x.editor.value)}),[c,x.editor.value]),t.createElement(p.Provider,{value:{state:x,dispatch:L,editorRef:w}},t.createElement("div",{"data-testid":"container",ref:S,className:"rmm-container"},t.createElement(n,Object.assign({},b,{"data-testid":"textarea",ref:w,className:"rmm-editor",wrap:"hard",onChange:function(e){L({type:"EditorData",payload:e.target})},value:x.editor.value})),E))},exports.EditorContet=p,exports.Mention=function(e){var n=e.prefix,r=e.data,a=e.onSearch,o=function(){var e=t.useContext(p);if(!e)throw new Error("Component has to be wrapped inside <Editor />");return e}(),l=o.state,i=o.dispatch,u=o.editorRef,s=l.editor,c=s.currentWord,d=s.valueUpToStart,f=s.value,v=l.width,y=c[0]===n,g=t.useState([0,0]),h=g[0],m=h[0],E=h[1],b=g[1],S=t.useState(0),w=S[0],k=S[1],x=c.replace(n,""),L=r.filter((function(e){return(""+e.value+e.label).toLowerCase().includes(x.toLowerCase())}));t.useEffect((function(){y&&a&&a(x)}),[y,x,a]),t.useEffect((function(){if(y&&u.current){var e=document.createElement("div"),t=d.split(/\n\r?/g),n=document.createElement("span");return e.classList.add("rmm-editor"),e.style.cssText="max-width: "+v+"px; position: absolute; top: 0; left: -9999px; opacity: 0; color: transparent; pointer-events: none; white-space: pre-wrap;",n.innerHTML=" ",n.style.position="absolute",e.innerHTML=t.join("<br />"),e.append(n),document.body.appendChild(e),b([n.offsetLeft,n.offsetTop+n.clientHeight+2]),function(){return document.body.removeChild(e)}}}),[y,v,u,d]);var C=t.useCallback((function(e,t){var r=f.slice(0,e.selectionStart-c.length)+""+n+t.value+" ",a=r+f.slice(e.selectionStart,f.length),o=r.length,l=r.length;e.value=a,e.setSelectionRange(o,l),i({type:"EditorData",payload:e}),k(0)}),[c.length,i,n,f]);if(t.useEffect((function(){var e=u.current;if(y){w>L.length&&k(0);var t=function(e){if("Enter"===e.key){var t=L[w]||L[0];if(!t)return;C(e.target,t),e.stopPropagation(),e.preventDefault()}"ArrowUp"===e.key&&(k((function(e){return Math.abs((e-1)%L.length)})),e.preventDefault()),"ArrowDown"===e.key&&(k((function(e){return Math.abs((e+1)%L.length)})),e.preventDefault())};if(e)return e.addEventListener("keydown",t,!1),function(){e.removeEventListener("keydown",t,!1)}}}),[y,c,f,n,L,w,i,u,C]),!y||!u.current)return null;var R=function(e){return function(){u.current&&(C(u.current,e),u.current.focus())}};return t.createElement("div",{className:"rmm-list",style:{transform:"translate("+m+"px, "+(E-u.current.scrollTop)+"px)"}},L.map((function(e,n){return t.createElement("div",{key:e.value,role:"button",className:"rmm-list-item "+(n===w&&"rmm-list-item-selected"),onMouseOver:function(){return k(n)},onClick:R(e)},t.createElement("b",null,e.value)," ",e.label)})))}; | ||
//# sourceMappingURL=react-marky-markdown.cjs.production.min.js.map |
@@ -133,5 +133,6 @@ import { useState, useEffect, createContext, useRef, useReducer, createElement, useContext, useCallback } from 'react'; | ||
} | ||
function getRowAtPosition(value, position) { | ||
function getRows(value, position) { | ||
var letterIndex = 0; | ||
var rows = value.split(/\n/g); | ||
var currentIndex = 0; | ||
@@ -143,3 +144,3 @@ for (var i = 0; i < rows.length; i++) { | ||
if (letterIndex >= position) { | ||
return row; | ||
currentIndex = i; | ||
} | ||
@@ -150,4 +151,14 @@ | ||
return ''; | ||
return { | ||
rows: rows, | ||
currentIndex: currentIndex | ||
}; | ||
} | ||
function getRowAtPosition(value, position) { | ||
var _getRows = getRows(value, position), | ||
rows = _getRows.rows, | ||
currentIndex = _getRows.currentIndex; | ||
return rows[currentIndex]; | ||
} | ||
function isCtrlCmd(event) { | ||
@@ -222,3 +233,5 @@ var isMacLike = /(Mac|iPhone|iPod|iPad)/i.test(navigator.platform); | ||
if (hasSelected) { | ||
el.setSelectionRange(caretStart - (insertStart || '').length, caretEnd + (insertEnd || '').length); | ||
caretStart = caretStart - (insertStart || '').length; | ||
caretEnd = caretEnd + (insertEnd || '').length; | ||
el.setSelectionRange(caretStart, caretEnd); | ||
} else { | ||
@@ -235,3 +248,4 @@ el.setSelectionRange(caretStart, caretStart); | ||
currentWord: currentWord, | ||
valueUpToStart: valueUpToStart | ||
valueUpToStart: valueUpToStart, | ||
lastSelectionStart: caretStart | ||
}; | ||
@@ -274,2 +288,14 @@ } | ||
case 'ClearList': | ||
var _getRows = getRows(action.payload.value, action.payload.selectionStart), | ||
rows = _getRows.rows, | ||
currentIndex = _getRows.currentIndex; | ||
var updatedRows = [].concat(rows); | ||
updatedRows[currentIndex] = ''; | ||
action.payload.value = updatedRows.join('\n'); | ||
return _extends({}, state, { | ||
editor: _extends({}, getEditorData(action.payload)) | ||
}); | ||
case 'ToggleBold': | ||
@@ -285,2 +311,24 @@ return _extends({}, state, { | ||
case 'Enclose': | ||
var _getWordAtPosition2 = getWordAtPosition(action.payload.el.value, action.payload.el.selectionStart, action.payload.el.selectionEnd), | ||
currentWord = _getWordAtPosition2[0], | ||
currentWordEnd = _getWordAtPosition2[2]; | ||
var startChar = action.payload.type; | ||
var endChar = startChar === '[' ? ']' : startChar === '(' ? ')' : '}'; | ||
if (action.payload.el.selectionStart !== action.payload.el.selectionEnd || currentWord === startChar) { | ||
return _extends({}, state, { | ||
editor: _extends({}, getEditorData(action.payload.el, startChar, endChar)) | ||
}); | ||
} else if (currentWordEnd === action.payload.el.selectionEnd) { | ||
return _extends({}, state, { | ||
editor: _extends({}, getEditorData(action.payload.el, startChar, endChar)) | ||
}); | ||
} | ||
return _extends({}, state, { | ||
editor: _extends({}, getEditorData(action.payload.el, action.payload.type)) | ||
}); | ||
default: | ||
@@ -295,3 +343,4 @@ return state; | ||
value: '', | ||
valueUpToStart: '' | ||
valueUpToStart: '', | ||
lastSelectionStart: 0 | ||
}, | ||
@@ -371,2 +420,10 @@ width: 0, | ||
if (isUL || isOL) { | ||
if (isUL && row === rowStartingChar || isOL && row.substr(0, 3) === row) { | ||
dispatch({ | ||
type: 'ClearList', | ||
payload: editor | ||
}); | ||
return; | ||
} | ||
dispatch({ | ||
@@ -410,2 +467,32 @@ type: 'AddListItem', | ||
} | ||
if (['[', '(', '{'].includes(event.key)) { | ||
dispatch({ | ||
type: 'Enclose', | ||
payload: { | ||
el: editor, | ||
type: event.key | ||
} | ||
}); | ||
event.preventDefault(); | ||
} | ||
if ([']', ')', '}'].includes(event.key)) { | ||
var noSelection = editor.selectionStart === editor.selectionEnd; | ||
var currentPosition = editor.selectionStart; | ||
var lastPosition = state.editor.lastSelectionStart; | ||
var lastCharacter = editor.value.substring(currentPosition - 1, currentPosition); | ||
var matching = { | ||
')': '(', | ||
']': '[', | ||
'}': '{' | ||
}; | ||
var eventKey = event.key; | ||
var matchingCharacter = matching[eventKey]; | ||
if (noSelection && currentPosition === lastPosition && lastCharacter === matchingCharacter) { | ||
editor.setSelectionRange(currentPosition + 1, currentPosition + 1); | ||
event.preventDefault(); | ||
} | ||
} | ||
} | ||
@@ -412,0 +499,0 @@ }; |
{ | ||
"name": "react-marky-markdown", | ||
"description": "A clean markdown editor for react", | ||
"version": "0.4.0", | ||
"version": "0.5.0", | ||
"license": "MIT", | ||
@@ -6,0 +6,0 @@ "author": "Daniel Brodin", |
# React Marky Markdown | ||
[![npm version](https://badge.fury.io/js/react-marky-markdown.svg)](https://badge.fury.io/js/react-marky-markdown) | ||
[![npm version](https://badge.fury.io/js/react-marky-markdown.svg)](https://badge.fury.io/js/react-marky-markdown) | ||
![](https://github.com/danielbrodin/react-marky-markdown/workflows/Test/badge.svg) | ||
@@ -52,2 +53,2 @@ A clean markdown editor for react. | ||
} | ||
``` | ||
``` |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
170660
1324
54