prosemirror-slash-menu 0.0.2 → 0.1.1

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023 Emergence Engineering
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/dist/index.es.js CHANGED
@@ -1 +1 @@
1
- import{PluginKey as e,Plugin as t}from"prosemirror-state";const n=e=>"submenu"===e.type?[e.id,...e.elements.map((e=>n(e)))].flat():[e.id],r=e=>{const t=(e=>e.filteredElements.map((e=>n(e))).flat())(e);return t.length!==new Set(t).size},s=e=>"submenu"===e.type?[e,...e.elements.map((e=>s(e)))].flat():[e],l=e=>e.elements.map((e=>s(e))).flat(),o=(e,t)=>l(t).find((t=>t.id===e)),u=(e,t,n="root")=>{let r="root";return t.forEach((t=>{if("submenu"===t.type){t.id===e&&(r=n);const s=t.elements.map((e=>e.id));r=s.includes(e)?t.id:u(e,t.elements,t.id)}t.id===e&&(r=n)})),r},i=(e,t,n)=>e.dispatch(e.state.tr.setMeta(t,n)),c=["Shift","Alt","Control","Pause","CapsLock","Escape","PageUp","PageDown","End","Home","PrintScreen","Insert","Delete","Meta","ContextMenu","F1","F2","F3","F4","F5","F6","F7","F8","F9","F10","NumLock","ScrollLock","AudioVolumeMute","AudioVolumeDown","AudioVolumeUp","LaunchMediaPlayer","LaunchApplication1","LaunchApplication2","ArrowUp","ArrowDown","ArrowLeft","ArrowRight"];var a;!function(e){e.OpenMenu="openMenu",e.CloseMenu="closeMenu",e.Execute="Execute",e.NextItem="NextItem",e.PrevItem="PrevItem",e.inputChange="InputChange",e.addChar="addChar",e.removeChar="removeChar",e.Ignore="Ignore",e.Catch="Catch"}(a||(a={}));const d={shouldOpen:(e,t,n)=>{const r=n.state,s=r.selection.from<0||r.selection.from>r.doc.content.size?null:r.doc.resolve(r.selection.from),l=null==s?void 0:s.parent,o="paragraph"===(null==l?void 0:l.type.name),u=o&&2===(null==l?void 0:l.nodeSize),i=r.selection.$head.parentOffset,c=r.selection.$head.parent.textContent.slice(i-1,i),a=" "===c||""===c||" "===c;return!e.open&&"/"===t.key&&o&&(u||a)},shouldClose:(e,t)=>e.open&&("/"===t.key||"Escape"===t.key||"Backspace"===t.key)&&0===e.filter.length},p=e=>{const t=(e=>{const t=u(e.selected,e.filteredElements),n=o(t,e);if("root"===t){const t=e.filteredElements.findIndex((t=>t.id===e.selected))+1;if(t<e.filteredElements.length)return e.filteredElements[t].id}if(n&&"submenu"===n.type){const t=n.elements.findIndex((t=>t.id===e.selected))+1;if(t<n.elements.length)return n.elements[t].id}})(e);return t?Object.assign(Object.assign({},e),{selected:t}):e},m=e=>{const t=(e=>{const t=u(e.selected,e.filteredElements),n=o(t,e);if("root"===t){const t=e.filteredElements.findIndex((t=>t.id===e.selected))-1;if(t>=0)return e.filteredElements[t].id}if(n&&"submenu"===n.type){const t=n.elements.findIndex((t=>t.id===e.selected))-1;if(t>=0)return n.elements[t].id}})(e);return t?Object.assign(Object.assign({},e),{selected:t}):e};var f;!function(e){e.open="open",e.close="close",e.execute="execute",e.nextItem="nextItem",e.prevItem="prevItem",e.openSubMenu="openSubMenu",e.closeSubMenu="closeSubMenu",e.inputChange="inputChange"}(f||(f={}));const h=new e("slash-menu-plugin"),y=(e,n,s)=>{const y={selected:e[0].id,open:!1,filter:"",ignoredKeys:n?[...c,...n]:c,filteredElements:e,elements:e};if(r(y))throw new Error("Menu elements must have unique id's!");return new t({key:h,props:{handleKeyDown(e,t){const n=e.state,r=h.getState(n);if(!r)return!1;const l=((e,t,n,r,s)=>{const l=s||d,u=o(e.selected,e);if(l.shouldOpen(e,t,n))return a.OpenMenu;if(l.shouldClose(e,t,n))return a.CloseMenu;if(e.open){if("ArrowDown"===t.key)return a.NextItem;if("ArrowUp"===t.key)return a.PrevItem;if("Enter"===t.key||"Tab"===t.key||"ArrowRight"===t.key&&"submenu"===(null==u?void 0:u.type))return a.Execute;if("Escape"===t.key||"Backspace"===t.key&&0===e.filter.length||"ArrowLeft"===t.key&&e.subMenuId)return a.CloseMenu;if(e.filter.length>0&&"Backspace"===t.key)return a.removeChar;if(!r.includes(t.key))return a.addChar;if("ArrowLeft"===t.key||"ArrowRight"===t.key)return a.Catch;console.log("here")}return a.Ignore})(r,t,e,y.ignoredKeys,s);switch(l){case a.OpenMenu:return i(e,h,{type:f.open}),!0;case a.CloseMenu:{const{subMenuId:s}=r;return s?i(e,h,{type:f.closeSubMenu,element:o(s,y)}):"/"===t.key?e.dispatch(n.tr.insertText("/").setMeta(h,{type:f.close})):i(e,h,{type:f.close}),!0}case a.Execute:{const t=o(r.selected,r);return!!t&&("command"===t.type&&(t.command(e),i(e,h,{type:f.execute})),"submenu"===t.type&&i(e,h,{type:f.openSubMenu,element:t}),!0)}case a.NextItem:return i(e,h,{type:f.nextItem}),!0;case a.PrevItem:return i(e,h,{type:f.prevItem}),!0;case a.addChar:return i(e,h,{type:f.inputChange,filter:r.filter+t.key}),!0;case a.removeChar:{const t=1===r.filter.length?"":r.filter.slice(0,-1);return i(e,h,{type:f.inputChange,filter:t}),!0}case a.Catch:return!0;default:return!1}}},state:{init:()=>y,apply(e,t){var n;const r=e.getMeta(h);switch(null==r?void 0:r.type){case f.open:return Object.assign(Object.assign({},y),{open:!0});case f.close:case f.execute:return y;case f.openSubMenu:return((e,t)=>{const n=t.element;return"submenu"===(null==n?void 0:n.type)?Object.assign(Object.assign({},e),{filteredElements:n.elements,selected:n.elements[0].id,subMenuId:n.id}):e})(t,r);case f.closeSubMenu:return((e,t,n)=>{const r=t.element;if("submenu"===(null==r?void 0:r.type)){const t=u(r.id,n.filteredElements);if("root"===t)return Object.assign(Object.assign({},n),{open:!0});const s=o(t,n);return"submenu"!==(null==s?void 0:s.type)?e:Object.assign(Object.assign({},e),{filteredElements:s.elements,selected:s.elements[0].id,subMenuId:t})}return e})(t,r,y);case f.nextItem:return p(t);case f.prevItem:return m(t);case f.inputChange:{const e=r.filter?((e,t)=>{const n=new RegExp(`${t.toLowerCase().replace(/\s/g,"\\s")}`);return l(e).map((e=>e)).filter((e=>null!==e.label.toLowerCase().match(n)&&"submenu"!==e.type))})(y,r.filter):y.elements,s=null===(n=null==e?void 0:e[0])||void 0===n?void 0:n.id;return Object.assign(Object.assign({},t),{selected:s||t.selected,filteredElements:e,filter:r.filter||""})}default:return t}}},initialState:y})};export{h as SlashMenuKey,y as SlashMenuPlugin,f as SlashMetaTypes,c as defaultIgnoredKeys,i as dispatchWithMeta,o as getElementById};
1
+ import{PluginKey as e,Plugin as t}from"prosemirror-state";const n=e=>"submenu"===e.type?[e.id,...e.elements.map((e=>n(e)))].flat():[e.id],r=e=>{const t=(e=>e.filteredElements.map((e=>n(e))).flat())(e);return t.length!==new Set(t).size},s=e=>"submenu"===e.type?[e,...e.elements.map((e=>s(e)))].flat():[e],o=e=>e.elements.map((e=>s(e))).flat(),l=(e,t)=>o(t).find((t=>t.id===e)),a=(e,t,n="root")=>{let r="root";return t.forEach((t=>{if("submenu"===t.type){t.id===e&&(r=n);const s=t.elements.map((e=>e.id));r=s.includes(e)?t.id:a(e,t.elements,t.id)}t.id===e&&(r=n)})),r},i=(e,t,n)=>e.dispatch(e.state.tr.setMeta(t,n)),u=["Unidentified","Alt","AltGraph","CapsLock","Control","Fn","FnLock","Hyper","Meta","NumLock","PageDown","PageUp","Pause","PrintScreen","Redo","ScrollLock","Shift","Super","Symbol","SymbolLock","Enter","Tab","ArrowDown","ArrowLeft","ArrowRight","ArrowUp","End","Home","PageDown","PageUp","Backspace","Clear","Copy","CrSel","Cut","Delete","EraseEof","ExSel","Insert","Paste","Redo","Undo","Accept","Again","Attn","Cancel","ContextMenu","Escape","Execute","Find","Finish","Help","Pause","Play","Props","Select","ZoomIn","ZoomOut","BrightnessDown","BrightnessUp","Eject","LogOff","Power","PowerOff","PrintScreen","Hibernate","Standby","WakeUp","AllCandidates","Alphanumeric","CodeInput","Compose","Convert","Dead","FinalMode","GroupFirst","GroupLast","GroupNext","GroupPrevious","ModeChange","NextCandidate","NonConvert","PreviousCandidate","Process","SingleCandidate","HangulMode","HanjaMode","JunjaMode","Eisu","Hankaku","Hiragana","HiraganaKatakana","KanaMode","KanjiMode","Katakana","Romaji","Zenkaku","ZenkakuHanaku"];var c;!function(e){e.OpenMenu="openMenu",e.CloseMenu="closeMenu",e.Execute="Execute",e.NextItem="NextItem",e.PrevItem="PrevItem",e.inputChange="InputChange",e.addChar="addChar",e.removeChar="removeChar",e.Ignore="Ignore",e.Catch="Catch"}(c||(c={}));const d={shouldOpen:(e,t,n)=>{const r=n.state,s=r.selection.from<0||r.selection.from>r.doc.content.size?null:r.doc.resolve(r.selection.from),o=null==s?void 0:s.parent,l="paragraph"===(null==o?void 0:o.type.name),a=l&&2===(null==o?void 0:o.nodeSize),i=r.selection.$head.parentOffset,u=r.selection.$head.parent.textContent.slice(i-1,i),c=" "===u||""===u||" "===u;return!e.open&&"/"===t.key&&l&&(a||c)},shouldClose:(e,t)=>e.open&&("/"===t.key||"Escape"===t.key||"Backspace"===t.key)&&0===e.filter.length},p=e=>{const t=(e=>{const t=a(e.selected,e.filteredElements),n=l(t,e);if("root"===t){const t=e.filteredElements.findIndex((t=>t.id===e.selected))+1;if(t<e.filteredElements.length)return e.filteredElements[t].id}if(n&&"submenu"===n.type){const t=n.elements.findIndex((t=>t.id===e.selected))+1;if(t<n.elements.length)return n.elements[t].id}})(e);return t?Object.assign(Object.assign({},e),{selected:t}):e},m=e=>{const t=(e=>{const t=a(e.selected,e.filteredElements),n=l(t,e);if("root"===t){const t=e.filteredElements.findIndex((t=>t.id===e.selected))-1;if(t>=0)return e.filteredElements[t].id}if(n&&"submenu"===n.type){const t=n.elements.findIndex((t=>t.id===e.selected))-1;if(t>=0)return n.elements[t].id}})(e);return t?Object.assign(Object.assign({},e),{selected:t}):e};var f;!function(e){e.open="open",e.close="close",e.execute="execute",e.nextItem="nextItem",e.prevItem="prevItem",e.openSubMenu="openSubMenu",e.closeSubMenu="closeSubMenu",e.inputChange="inputChange"}(f||(f={}));const g=new e("slash-menu-plugin"),h=(e,n,s)=>{const h={selected:e[0].id,open:!1,filter:"",ignoredKeys:n?[...u,...n]:u,filteredElements:e,elements:e};if(r(h))throw new Error("Menu elements must have unique id's!");return new t({key:g,props:{handleKeyDown(e,t){const n=e.state,r=g.getState(n);if(!r)return!1;const o=((e,t,n,r,s)=>{const o=s||d,a=l(e.selected,e);if(o.shouldOpen(e,t,n))return c.OpenMenu;if(o.shouldClose(e,t,n))return c.CloseMenu;if(e.open){if("ArrowDown"===t.key)return c.NextItem;if("ArrowUp"===t.key)return c.PrevItem;if("Enter"===t.key||"Tab"===t.key||"ArrowRight"===t.key&&"submenu"===(null==a?void 0:a.type))return c.Execute;if("Escape"===t.key||"Backspace"===t.key&&0===e.filter.length||"ArrowLeft"===t.key&&e.subMenuId)return c.CloseMenu;if(e.filter.length>0&&"Backspace"===t.key)return c.removeChar;if(!r.includes(t.key))return c.addChar;if("ArrowLeft"===t.key||"ArrowRight"===t.key)return c.Catch;console.log("here")}return c.Ignore})(r,t,e,h.ignoredKeys,s);switch(o){case c.OpenMenu:return i(e,g,{type:f.open}),!0;case c.CloseMenu:{const{subMenuId:s}=r;return s?i(e,g,{type:f.closeSubMenu,element:l(s,h)}):"/"===t.key?e.dispatch(n.tr.insertText("/").setMeta(g,{type:f.close})):i(e,g,{type:f.close}),!0}case c.Execute:{const t=l(r.selected,r);return!!t&&("command"===t.type&&(t.command(e),i(e,g,{type:f.execute})),"submenu"===t.type&&i(e,g,{type:f.openSubMenu,element:t}),!0)}case c.NextItem:return i(e,g,{type:f.nextItem}),!0;case c.PrevItem:return i(e,g,{type:f.prevItem}),!0;case c.addChar:return i(e,g,{type:f.inputChange,filter:r.filter+t.key}),!0;case c.removeChar:{const t=1===r.filter.length?"":r.filter.slice(0,-1);return i(e,g,{type:f.inputChange,filter:t}),!0}case c.Catch:return!0;default:return!1}}},state:{init:()=>h,apply(e,t){var n;const r=e.getMeta(g);switch(null==r?void 0:r.type){case f.open:return Object.assign(Object.assign({},h),{open:!0});case f.close:case f.execute:return h;case f.openSubMenu:return((e,t)=>{const n=t.element;return"submenu"===(null==n?void 0:n.type)?Object.assign(Object.assign({},e),{filteredElements:n.elements,selected:n.elements[0].id,subMenuId:n.id}):e})(t,r);case f.closeSubMenu:return((e,t,n)=>{const r=t.element;if("submenu"===(null==r?void 0:r.type)){const t=a(r.id,n.filteredElements);if("root"===t)return Object.assign(Object.assign({},n),{open:!0});const s=l(t,n);return"submenu"!==(null==s?void 0:s.type)?e:Object.assign(Object.assign({},e),{filteredElements:s.elements,selected:s.elements[0].id,subMenuId:t})}return e})(t,r,h);case f.nextItem:return p(t);case f.prevItem:return m(t);case f.inputChange:{const e=r.filter?((e,t)=>{const n=new RegExp(`${t.toLowerCase().replace(/\s/g,"\\s")}`);return o(e).map((e=>e)).filter((e=>null!==e.label.toLowerCase().match(n)&&"submenu"!==e.type))})(h,r.filter):h.elements,s=null===(n=null==e?void 0:e[0])||void 0===n?void 0:n.id;return Object.assign(Object.assign({},t),{selected:s||t.selected,filteredElements:e,filter:r.filter||""})}default:return t}}},initialState:h})};export{g as SlashMenuKey,h as SlashMenuPlugin,f as SlashMetaTypes,u as defaultIgnoredKeys,i as dispatchWithMeta,l as getElementById};
package/dist/index.js CHANGED
@@ -1 +1 @@
1
- "use strict";var e=require("prosemirror-state");const t=e=>"submenu"===e.type?[e.id,...e.elements.map((e=>t(e)))].flat():[e.id],n=e=>{const n=(e=>e.filteredElements.map((e=>t(e))).flat())(e);return n.length!==new Set(n).size},s=e=>"submenu"===e.type?[e,...e.elements.map((e=>s(e)))].flat():[e],r=e=>e.elements.map((e=>s(e))).flat(),l=(e,t)=>r(t).find((t=>t.id===e)),o=(e,t,n="root")=>{let s="root";return t.forEach((t=>{if("submenu"===t.type){t.id===e&&(s=n);const r=t.elements.map((e=>e.id));s=r.includes(e)?t.id:o(e,t.elements,t.id)}t.id===e&&(s=n)})),s},a=(e,t,n)=>e.dispatch(e.state.tr.setMeta(t,n)),u=["Shift","Alt","Control","Pause","CapsLock","Escape","PageUp","PageDown","End","Home","PrintScreen","Insert","Delete","Meta","ContextMenu","F1","F2","F3","F4","F5","F6","F7","F8","F9","F10","NumLock","ScrollLock","AudioVolumeMute","AudioVolumeDown","AudioVolumeUp","LaunchMediaPlayer","LaunchApplication1","LaunchApplication2","ArrowUp","ArrowDown","ArrowLeft","ArrowRight"];var i;!function(e){e.OpenMenu="openMenu",e.CloseMenu="closeMenu",e.Execute="Execute",e.NextItem="NextItem",e.PrevItem="PrevItem",e.inputChange="InputChange",e.addChar="addChar",e.removeChar="removeChar",e.Ignore="Ignore",e.Catch="Catch"}(i||(i={}));const c={shouldOpen:(e,t,n)=>{const s=n.state,r=s.selection.from<0||s.selection.from>s.doc.content.size?null:s.doc.resolve(s.selection.from),l=null==r?void 0:r.parent,o="paragraph"===(null==l?void 0:l.type.name),a=o&&2===(null==l?void 0:l.nodeSize),u=s.selection.$head.parentOffset,i=s.selection.$head.parent.textContent.slice(u-1,u),c=" "===i||""===i||" "===i;return!e.open&&"/"===t.key&&o&&(a||c)},shouldClose:(e,t)=>e.open&&("/"===t.key||"Escape"===t.key||"Backspace"===t.key)&&0===e.filter.length},p=e=>{const t=(e=>{const t=o(e.selected,e.filteredElements),n=l(t,e);if("root"===t){const t=e.filteredElements.findIndex((t=>t.id===e.selected))+1;if(t<e.filteredElements.length)return e.filteredElements[t].id}if(n&&"submenu"===n.type){const t=n.elements.findIndex((t=>t.id===e.selected))+1;if(t<n.elements.length)return n.elements[t].id}})(e);return t?Object.assign(Object.assign({},e),{selected:t}):e},d=e=>{const t=(e=>{const t=o(e.selected,e.filteredElements),n=l(t,e);if("root"===t){const t=e.filteredElements.findIndex((t=>t.id===e.selected))-1;if(t>=0)return e.filteredElements[t].id}if(n&&"submenu"===n.type){const t=n.elements.findIndex((t=>t.id===e.selected))-1;if(t>=0)return n.elements[t].id}})(e);return t?Object.assign(Object.assign({},e),{selected:t}):e};var m;exports.SlashMetaTypes=void 0,(m=exports.SlashMetaTypes||(exports.SlashMetaTypes={})).open="open",m.close="close",m.execute="execute",m.nextItem="nextItem",m.prevItem="prevItem",m.openSubMenu="openSubMenu",m.closeSubMenu="closeSubMenu",m.inputChange="inputChange";const h=new e.PluginKey("slash-menu-plugin");exports.SlashMenuKey=h,exports.SlashMenuPlugin=(t,s,m)=>{const f={selected:t[0].id,open:!1,filter:"",ignoredKeys:s?[...u,...s]:u,filteredElements:t,elements:t};if(n(f))throw new Error("Menu elements must have unique id's!");return new e.Plugin({key:h,props:{handleKeyDown(e,t){const n=e.state,s=h.getState(n);if(!s)return!1;const r=((e,t,n,s,r)=>{const o=r||c,a=l(e.selected,e);if(o.shouldOpen(e,t,n))return i.OpenMenu;if(o.shouldClose(e,t,n))return i.CloseMenu;if(e.open){if("ArrowDown"===t.key)return i.NextItem;if("ArrowUp"===t.key)return i.PrevItem;if("Enter"===t.key||"Tab"===t.key||"ArrowRight"===t.key&&"submenu"===(null==a?void 0:a.type))return i.Execute;if("Escape"===t.key||"Backspace"===t.key&&0===e.filter.length||"ArrowLeft"===t.key&&e.subMenuId)return i.CloseMenu;if(e.filter.length>0&&"Backspace"===t.key)return i.removeChar;if(!s.includes(t.key))return i.addChar;if("ArrowLeft"===t.key||"ArrowRight"===t.key)return i.Catch;console.log("here")}return i.Ignore})(s,t,e,f.ignoredKeys,m);switch(r){case i.OpenMenu:return a(e,h,{type:exports.SlashMetaTypes.open}),!0;case i.CloseMenu:{const{subMenuId:r}=s;return r?a(e,h,{type:exports.SlashMetaTypes.closeSubMenu,element:l(r,f)}):"/"===t.key?e.dispatch(n.tr.insertText("/").setMeta(h,{type:exports.SlashMetaTypes.close})):a(e,h,{type:exports.SlashMetaTypes.close}),!0}case i.Execute:{const t=l(s.selected,s);return!!t&&("command"===t.type&&(t.command(e),a(e,h,{type:exports.SlashMetaTypes.execute})),"submenu"===t.type&&a(e,h,{type:exports.SlashMetaTypes.openSubMenu,element:t}),!0)}case i.NextItem:return a(e,h,{type:exports.SlashMetaTypes.nextItem}),!0;case i.PrevItem:return a(e,h,{type:exports.SlashMetaTypes.prevItem}),!0;case i.addChar:return a(e,h,{type:exports.SlashMetaTypes.inputChange,filter:s.filter+t.key}),!0;case i.removeChar:{const t=1===s.filter.length?"":s.filter.slice(0,-1);return a(e,h,{type:exports.SlashMetaTypes.inputChange,filter:t}),!0}case i.Catch:return!0;default:return!1}}},state:{init:()=>f,apply(e,t){var n;const s=e.getMeta(h);switch(null==s?void 0:s.type){case exports.SlashMetaTypes.open:return Object.assign(Object.assign({},f),{open:!0});case exports.SlashMetaTypes.close:case exports.SlashMetaTypes.execute:return f;case exports.SlashMetaTypes.openSubMenu:return((e,t)=>{const n=t.element;return"submenu"===(null==n?void 0:n.type)?Object.assign(Object.assign({},e),{filteredElements:n.elements,selected:n.elements[0].id,subMenuId:n.id}):e})(t,s);case exports.SlashMetaTypes.closeSubMenu:return((e,t,n)=>{const s=t.element;if("submenu"===(null==s?void 0:s.type)){const t=o(s.id,n.filteredElements);if("root"===t)return Object.assign(Object.assign({},n),{open:!0});const r=l(t,n);return"submenu"!==(null==r?void 0:r.type)?e:Object.assign(Object.assign({},e),{filteredElements:r.elements,selected:r.elements[0].id,subMenuId:t})}return e})(t,s,f);case exports.SlashMetaTypes.nextItem:return p(t);case exports.SlashMetaTypes.prevItem:return d(t);case exports.SlashMetaTypes.inputChange:{const e=s.filter?((e,t)=>{const n=new RegExp(`${t.toLowerCase().replace(/\s/g,"\\s")}`);return r(e).map((e=>e)).filter((e=>null!==e.label.toLowerCase().match(n)&&"submenu"!==e.type))})(f,s.filter):f.elements,l=null===(n=null==e?void 0:e[0])||void 0===n?void 0:n.id;return Object.assign(Object.assign({},t),{selected:l||t.selected,filteredElements:e,filter:s.filter||""})}default:return t}}},initialState:f})},exports.defaultIgnoredKeys=u,exports.dispatchWithMeta=a,exports.getElementById=l;
1
+ "use strict";var e=require("prosemirror-state");const t=e=>"submenu"===e.type?[e.id,...e.elements.map((e=>t(e)))].flat():[e.id],n=e=>{const n=(e=>e.filteredElements.map((e=>t(e))).flat())(e);return n.length!==new Set(n).size},s=e=>"submenu"===e.type?[e,...e.elements.map((e=>s(e)))].flat():[e],r=e=>e.elements.map((e=>s(e))).flat(),a=(e,t)=>r(t).find((t=>t.id===e)),o=(e,t,n="root")=>{let s="root";return t.forEach((t=>{if("submenu"===t.type){t.id===e&&(s=n);const r=t.elements.map((e=>e.id));s=r.includes(e)?t.id:o(e,t.elements,t.id)}t.id===e&&(s=n)})),s},l=(e,t,n)=>e.dispatch(e.state.tr.setMeta(t,n)),i=["Unidentified","Alt","AltGraph","CapsLock","Control","Fn","FnLock","Hyper","Meta","NumLock","PageDown","PageUp","Pause","PrintScreen","Redo","ScrollLock","Shift","Super","Symbol","SymbolLock","Enter","Tab","ArrowDown","ArrowLeft","ArrowRight","ArrowUp","End","Home","PageDown","PageUp","Backspace","Clear","Copy","CrSel","Cut","Delete","EraseEof","ExSel","Insert","Paste","Redo","Undo","Accept","Again","Attn","Cancel","ContextMenu","Escape","Execute","Find","Finish","Help","Pause","Play","Props","Select","ZoomIn","ZoomOut","BrightnessDown","BrightnessUp","Eject","LogOff","Power","PowerOff","PrintScreen","Hibernate","Standby","WakeUp","AllCandidates","Alphanumeric","CodeInput","Compose","Convert","Dead","FinalMode","GroupFirst","GroupLast","GroupNext","GroupPrevious","ModeChange","NextCandidate","NonConvert","PreviousCandidate","Process","SingleCandidate","HangulMode","HanjaMode","JunjaMode","Eisu","Hankaku","Hiragana","HiraganaKatakana","KanaMode","KanjiMode","Katakana","Romaji","Zenkaku","ZenkakuHanaku"];var u;!function(e){e.OpenMenu="openMenu",e.CloseMenu="closeMenu",e.Execute="Execute",e.NextItem="NextItem",e.PrevItem="PrevItem",e.inputChange="InputChange",e.addChar="addChar",e.removeChar="removeChar",e.Ignore="Ignore",e.Catch="Catch"}(u||(u={}));const p={shouldOpen:(e,t,n)=>{const s=n.state,r=s.selection.from<0||s.selection.from>s.doc.content.size?null:s.doc.resolve(s.selection.from),a=null==r?void 0:r.parent,o="paragraph"===(null==a?void 0:a.type.name),l=o&&2===(null==a?void 0:a.nodeSize),i=s.selection.$head.parentOffset,u=s.selection.$head.parent.textContent.slice(i-1,i),p=" "===u||""===u||" "===u;return!e.open&&"/"===t.key&&o&&(l||p)},shouldClose:(e,t)=>e.open&&("/"===t.key||"Escape"===t.key||"Backspace"===t.key)&&0===e.filter.length},c=e=>{const t=(e=>{const t=o(e.selected,e.filteredElements),n=a(t,e);if("root"===t){const t=e.filteredElements.findIndex((t=>t.id===e.selected))+1;if(t<e.filteredElements.length)return e.filteredElements[t].id}if(n&&"submenu"===n.type){const t=n.elements.findIndex((t=>t.id===e.selected))+1;if(t<n.elements.length)return n.elements[t].id}})(e);return t?Object.assign(Object.assign({},e),{selected:t}):e},d=e=>{const t=(e=>{const t=o(e.selected,e.filteredElements),n=a(t,e);if("root"===t){const t=e.filteredElements.findIndex((t=>t.id===e.selected))-1;if(t>=0)return e.filteredElements[t].id}if(n&&"submenu"===n.type){const t=n.elements.findIndex((t=>t.id===e.selected))-1;if(t>=0)return n.elements[t].id}})(e);return t?Object.assign(Object.assign({},e),{selected:t}):e};var m;exports.SlashMetaTypes=void 0,(m=exports.SlashMetaTypes||(exports.SlashMetaTypes={})).open="open",m.close="close",m.execute="execute",m.nextItem="nextItem",m.prevItem="prevItem",m.openSubMenu="openSubMenu",m.closeSubMenu="closeSubMenu",m.inputChange="inputChange";const f=new e.PluginKey("slash-menu-plugin");exports.SlashMenuKey=f,exports.SlashMenuPlugin=(t,s,m)=>{const h={selected:t[0].id,open:!1,filter:"",ignoredKeys:s?[...i,...s]:i,filteredElements:t,elements:t};if(n(h))throw new Error("Menu elements must have unique id's!");return new e.Plugin({key:f,props:{handleKeyDown(e,t){const n=e.state,s=f.getState(n);if(!s)return!1;const r=((e,t,n,s,r)=>{const o=r||p,l=a(e.selected,e);if(o.shouldOpen(e,t,n))return u.OpenMenu;if(o.shouldClose(e,t,n))return u.CloseMenu;if(e.open){if("ArrowDown"===t.key)return u.NextItem;if("ArrowUp"===t.key)return u.PrevItem;if("Enter"===t.key||"Tab"===t.key||"ArrowRight"===t.key&&"submenu"===(null==l?void 0:l.type))return u.Execute;if("Escape"===t.key||"Backspace"===t.key&&0===e.filter.length||"ArrowLeft"===t.key&&e.subMenuId)return u.CloseMenu;if(e.filter.length>0&&"Backspace"===t.key)return u.removeChar;if(!s.includes(t.key))return u.addChar;if("ArrowLeft"===t.key||"ArrowRight"===t.key)return u.Catch;console.log("here")}return u.Ignore})(s,t,e,h.ignoredKeys,m);switch(r){case u.OpenMenu:return l(e,f,{type:exports.SlashMetaTypes.open}),!0;case u.CloseMenu:{const{subMenuId:r}=s;return r?l(e,f,{type:exports.SlashMetaTypes.closeSubMenu,element:a(r,h)}):"/"===t.key?e.dispatch(n.tr.insertText("/").setMeta(f,{type:exports.SlashMetaTypes.close})):l(e,f,{type:exports.SlashMetaTypes.close}),!0}case u.Execute:{const t=a(s.selected,s);return!!t&&("command"===t.type&&(t.command(e),l(e,f,{type:exports.SlashMetaTypes.execute})),"submenu"===t.type&&l(e,f,{type:exports.SlashMetaTypes.openSubMenu,element:t}),!0)}case u.NextItem:return l(e,f,{type:exports.SlashMetaTypes.nextItem}),!0;case u.PrevItem:return l(e,f,{type:exports.SlashMetaTypes.prevItem}),!0;case u.addChar:return l(e,f,{type:exports.SlashMetaTypes.inputChange,filter:s.filter+t.key}),!0;case u.removeChar:{const t=1===s.filter.length?"":s.filter.slice(0,-1);return l(e,f,{type:exports.SlashMetaTypes.inputChange,filter:t}),!0}case u.Catch:return!0;default:return!1}}},state:{init:()=>h,apply(e,t){var n;const s=e.getMeta(f);switch(null==s?void 0:s.type){case exports.SlashMetaTypes.open:return Object.assign(Object.assign({},h),{open:!0});case exports.SlashMetaTypes.close:case exports.SlashMetaTypes.execute:return h;case exports.SlashMetaTypes.openSubMenu:return((e,t)=>{const n=t.element;return"submenu"===(null==n?void 0:n.type)?Object.assign(Object.assign({},e),{filteredElements:n.elements,selected:n.elements[0].id,subMenuId:n.id}):e})(t,s);case exports.SlashMetaTypes.closeSubMenu:return((e,t,n)=>{const s=t.element;if("submenu"===(null==s?void 0:s.type)){const t=o(s.id,n.filteredElements);if("root"===t)return Object.assign(Object.assign({},n),{open:!0});const r=a(t,n);return"submenu"!==(null==r?void 0:r.type)?e:Object.assign(Object.assign({},e),{filteredElements:r.elements,selected:r.elements[0].id,subMenuId:t})}return e})(t,s,h);case exports.SlashMetaTypes.nextItem:return c(t);case exports.SlashMetaTypes.prevItem:return d(t);case exports.SlashMetaTypes.inputChange:{const e=s.filter?((e,t)=>{const n=new RegExp(`${t.toLowerCase().replace(/\s/g,"\\s")}`);return r(e).map((e=>e)).filter((e=>null!==e.label.toLowerCase().match(n)&&"submenu"!==e.type))})(h,s.filter):h.elements,a=null===(n=null==e?void 0:e[0])||void 0===n?void 0:n.id;return Object.assign(Object.assign({},t),{selected:a||t.selected,filteredElements:e,filter:s.filter||""})}default:return t}}},initialState:h})},exports.defaultIgnoredKeys=i,exports.dispatchWithMeta=l,exports.getElementById=a;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "prosemirror-slash-menu",
3
- "version": "0.0.2",
3
+ "version": "0.1.1",
4
4
  "description": "Slash menu for ProseMirror",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.es.js",
@@ -65,7 +65,7 @@
65
65
  "typescript": "^5.1.3"
66
66
  },
67
67
  "engines": {
68
- "npm": ">=8.0.0",
69
- "node": ">=16.0.0"
68
+ "node": ">=12",
69
+ "npm": ">=7"
70
70
  }
71
71
  }