prosemirror-slash-menu 0.1.0 → 0.1.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/LICENSE +21 -0
- package/dist/index.es.js +1 -1
- package/dist/index.js +1 -1
- package/package.json +1 -1
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],
|
|
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","F1","F2","F3","F4","F5","F6","F7","F8","F9","F10","F11","F12","F13","F14","F15","F16","F17","F18","F19","F20","F21","F22","F23","F24","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(),
|
|
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","F1","F2","F3","F4","F5","F6","F7","F8","F9","F10","F11","F12","F13","F14","F15","F16","F17","F18","F19","F20","F21","F22","F23","F24","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;
|