react-marky-markdown
Advanced tools
Comparing version 0.5.0 to 0.5.1
@@ -7,3 +7,8 @@ # Changelog | ||
## 0.5.1 2019-11-08 | ||
### Fixes | ||
- Bugfix | ||
## 0.5.0 2019-11-07 | ||
### Added | ||
- Add autocomplete for enclosing types like `(` and `[` | ||
@@ -10,0 +15,0 @@ - Double enter when in a list will now exit the list. |
@@ -477,13 +477,5 @@ 'use strict'; | ||
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]; | ||
var nextCharacater = editor.value[editor.selectionStart]; | ||
if (noSelection && currentPosition === lastPosition && lastCharacter === matchingCharacter) { | ||
if (noSelection && nextCharacater === event.key) { | ||
editor.setSelectionRange(currentPosition + 1, currentPosition + 1); | ||
@@ -490,0 +482,0 @@ event.preventDefault(); |
@@ -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 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)})))}; | ||
"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.selectionStart===e.selectionEnd&&e.value[e.selectionStart]===t.key&&(e.setSelectionRange(i+1,i+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 |
@@ -473,13 +473,5 @@ import { useState, useEffect, createContext, useRef, useReducer, createElement, useContext, useCallback } from 'react'; | ||
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]; | ||
var nextCharacater = editor.value[editor.selectionStart]; | ||
if (noSelection && currentPosition === lastPosition && lastCharacter === matchingCharacter) { | ||
if (noSelection && nextCharacater === event.key) { | ||
editor.setSelectionRange(currentPosition + 1, currentPosition + 1); | ||
@@ -486,0 +478,0 @@ event.preventDefault(); |
{ | ||
"name": "react-marky-markdown", | ||
"description": "A clean markdown editor for react", | ||
"version": "0.5.0", | ||
"version": "0.5.1", | ||
"license": "MIT", | ||
@@ -6,0 +6,0 @@ "author": "Daniel Brodin", |
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
168167
1308