@wordpress/commands 0.3.0 → 0.5.0
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/CHANGELOG.md +4 -0
- package/README.md +16 -0
- package/build/components/command-menu.js +83 -57
- package/build/components/command-menu.js.map +1 -1
- package/build/hooks/use-command-context.js +45 -0
- package/build/hooks/use-command-context.js.map +1 -0
- package/build/hooks/use-command-loader.js +6 -11
- package/build/hooks/use-command-loader.js.map +1 -1
- package/build/hooks/use-command.js +5 -3
- package/build/hooks/use-command.js.map +1 -1
- package/build/index.js +18 -0
- package/build/index.js.map +1 -1
- package/build/private-apis.js +4 -4
- package/build/private-apis.js.map +1 -1
- package/build/store/actions.js +61 -40
- package/build/store/actions.js.map +1 -1
- package/build/store/reducer.js +63 -22
- package/build/store/reducer.js.map +1 -1
- package/build/store/selectors.js +25 -17
- package/build/store/selectors.js.map +1 -1
- package/build-module/components/command-menu.js +83 -57
- package/build-module/components/command-menu.js.map +1 -1
- package/build-module/hooks/use-command-context.js +35 -0
- package/build-module/hooks/use-command-context.js.map +1 -0
- package/build-module/hooks/use-command-loader.js +6 -11
- package/build-module/hooks/use-command-loader.js.map +1 -1
- package/build-module/hooks/use-command.js +5 -3
- package/build-module/hooks/use-command.js.map +1 -1
- package/build-module/index.js +2 -0
- package/build-module/index.js.map +1 -1
- package/build-module/private-apis.js +4 -4
- package/build-module/private-apis.js.map +1 -1
- package/build-module/store/actions.js +55 -40
- package/build-module/store/actions.js.map +1 -1
- package/build-module/store/reducer.js +63 -22
- package/build-module/store/reducer.js.map +1 -1
- package/build-module/store/selectors.js +19 -15
- package/build-module/store/selectors.js.map +1 -1
- package/build-style/style-rtl.css +9 -1
- package/build-style/style.css +9 -1
- package/package.json +9 -9
- package/src/components/command-menu.js +57 -43
- package/src/components/style.scss +11 -1
- package/src/hooks/use-command-context.js +32 -0
- package/src/hooks/use-command-loader.js +12 -6
- package/src/hooks/use-command.js +7 -3
- package/src/index.js +2 -0
- package/src/private-apis.js +4 -4
- package/src/store/actions.js +53 -26
- package/src/store/reducer.js +53 -27
- package/src/store/selectors.js +22 -19
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["@wordpress/commands/src/components/command-menu.js"],"names":["Command","useSelect","useDispatch","useState","useEffect","useRef","useCallback","__","Modal","TextHighlight","__experimentalHStack","HStack","store","keyboardShortcutsStore","useShortcut","Icon","commandsStore","CommandMenuLoader","name","search","hook","setLoader","close","isLoading","commands","map","command","callback","icon","label","CommandMenuLoaderWrapper","currentLoader","key","setKey","current","prevKey","CommandMenuGroup","group","loaders","select","getCommands","getCommandLoaders","loader","CommandMenu","registerShortcut","setSearch","open","setOpen","groups","getGroups","setLoaders","category","description","keyCombination","modifier","character","event","preventDefault","prevOpen","bindGlobal","value","Object","values","some","Boolean"],"mappings":";;AAAA;AACA;AACA;AACA,SAASA,OAAT,QAAwB,MAAxB;AAEA;AACA;AACA;;AACA,SAASC,SAAT,EAAoBC,WAApB,QAAuC,iBAAvC;AACA,SAASC,QAAT,EAAmBC,SAAnB,EAA8BC,MAA9B,EAAsCC,WAAtC,QAAyD,oBAAzD;AACA,SAASC,EAAT,QAAmB,iBAAnB;AACA,SACCC,KADD,EAECC,aAFD,EAGCC,oBAAoB,IAAIC,MAHzB,QAIO,uBAJP;AAKA,SACCC,KAAK,IAAIC,sBADV,EAECC,WAFD,QAGO,+BAHP;AAIA,SAASC,IAAT,QAAqB,kBAArB;AAEA;AACA;AACA;;AACA,SAASH,KAAK,IAAII,aAAlB,QAAuC,UAAvC;;AAEA,SAASC,iBAAT,OAAuE;AAAA;;AAAA,MAA3C;AAAEC,IAAAA,IAAF;AAAQC,IAAAA,MAAR;AAAgBC,IAAAA,IAAhB;AAAsBC,IAAAA,SAAtB;AAAiCC,IAAAA;AAAjC,GAA2C;AACtE,QAAM;AAAEC,IAAAA,SAAF;AAAaC,IAAAA,QAAQ,GAAG;AAAxB,eAA+BJ,IAAI,CAAE;AAAED,IAA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,CADD,EAWGY,MAAM,IACP,cAAC,OAAD,CAAS,IAAT,QACG,CAAEI,SAAF,IACD,cAAC,OAAD,CAAS,KAAT,QACGhB,EAAE,CAAE,mBAAF,CADL,CAFF,EAMGyC,MAAM,CAACvB,GAAP,CAAcY,KAAF,IACb,cAAC,gBAAD;AACC,IAAA,GAAG,EAAGA,KADP;AAEC,IAAA,KAAK,EAAGA,KAFT;AAGC,IAAA,MAAM,EAAGlB,MAHV;AAIC,IAAA,SAAS,EAAGE,SAJb;AAKC,IAAA,KAAK,EAAGC;AALT,IADC,CANH,CAZF,CADD,CAND,CADD;AAyCA","sourcesContent":["/**\n * External dependencies\n */\nimport { Command } from 'cmdk';\n\n/**\n * WordPress dependencies\n */\nimport { useSelect, useDispatch } from '@wordpress/data';\nimport { useState, useEffect, useRef, useCallback } from '@wordpress/element';\nimport { __ } from '@wordpress/i18n';\nimport {\n\tModal,\n\tTextHighlight,\n\t__experimentalHStack as HStack,\n} from '@wordpress/components';\nimport {\n\tstore as keyboardShortcutsStore,\n\tuseShortcut,\n} from '@wordpress/keyboard-shortcuts';\nimport { Icon } from '@wordpress/icons';\n\n/**\n * Internal dependencies\n */\nimport { store as commandsStore } from '../store';\n\nfunction CommandMenuLoader( { name, search, hook, setLoader, close } ) {\n\tconst { isLoading, commands = [] } = hook( { search } ) ?? {};\n\tuseEffect( () => {\n\t\tsetLoader( name, isLoading );\n\t}, [ setLoader, name, isLoading ] );\n\n\treturn (\n\t\t<>\n\t\t\t<Command.List>\n\t\t\t\t{ isLoading && (\n\t\t\t\t\t<Command.Loading>{ __( 'Searching…' ) }</Command.Loading>\n\t\t\t\t) }\n\n\t\t\t\t{ commands.map( ( command ) => (\n\t\t\t\t\t<Command.Item\n\t\t\t\t\t\tkey={ command.name }\n\t\t\t\t\t\tvalue={ command.name }\n\t\t\t\t\t\tonSelect={ () => command.callback( { close } ) }\n\t\t\t\t\t>\n\t\t\t\t\t\t<HStack\n\t\t\t\t\t\t\talignment=\"left\"\n\t\t\t\t\t\t\tclassName=\"commands-command-menu__item\"\n\t\t\t\t\t\t>\n\t\t\t\t\t\t\t<Icon icon={ command.icon } />\n\t\t\t\t\t\t\t<span>\n\t\t\t\t\t\t\t\t<TextHighlight\n\t\t\t\t\t\t\t\t\ttext={ command.label }\n\t\t\t\t\t\t\t\t\thighlight={ search }\n\t\t\t\t\t\t\t\t/>\n\t\t\t\t\t\t\t</span>\n\t\t\t\t\t\t</HStack>\n\t\t\t\t\t</Command.Item>\n\t\t\t\t) ) }\n\t\t\t</Command.List>\n\t\t</>\n\t);\n}\n\nexport function CommandMenuLoaderWrapper( { hook, search, setLoader, close } ) {\n\t// The \"hook\" prop is actually a custom React hook\n\t// so to avoid breaking the rules of hooks\n\t// the CommandMenuLoaderWrapper component need to be\n\t// remounted on each hook prop change\n\t// We use the key state to make sure we do that properly.\n\tconst currentLoader = useRef( hook );\n\tconst [ key, setKey ] = useState( 0 );\n\tuseEffect( () => {\n\t\tif ( currentLoader.current !== hook ) {\n\t\t\tcurrentLoader.current = hook;\n\t\t\tsetKey( ( prevKey ) => prevKey + 1 );\n\t\t}\n\t}, [ hook ] );\n\n\treturn (\n\t\t<CommandMenuLoader\n\t\t\tkey={ key }\n\t\t\thook={ currentLoader.current }\n\t\t\tsearch={ search }\n\t\t\tsetLoader={ setLoader }\n\t\t\tclose={ close }\n\t\t/>\n\t);\n}\n\nexport function CommandMenuGroup( { group, search, setLoader, close } ) {\n\tconst { commands, loaders } = useSelect(\n\t\t( select ) => {\n\t\t\tconst { getCommands, getCommandLoaders } = select( commandsStore );\n\t\t\treturn {\n\t\t\t\tcommands: getCommands( group ),\n\t\t\t\tloaders: getCommandLoaders( group ),\n\t\t\t};\n\t\t},\n\t\t[ group ]\n\t);\n\n\treturn (\n\t\t<Command.Group>\n\t\t\t{ commands.map( ( command ) => (\n\t\t\t\t<Command.Item\n\t\t\t\t\tkey={ command.name }\n\t\t\t\t\tvalue={ command.name }\n\t\t\t\t\tonSelect={ () => command.callback( { close } ) }\n\t\t\t\t>\n\t\t\t\t\t<HStack\n\t\t\t\t\t\talignment=\"left\"\n\t\t\t\t\t\tclassName=\"commands-command-menu__item\"\n\t\t\t\t\t>\n\t\t\t\t\t\t<Icon icon={ command.icon } />\n\t\t\t\t\t\t<span>\n\t\t\t\t\t\t\t<TextHighlight\n\t\t\t\t\t\t\t\ttext={ command.label }\n\t\t\t\t\t\t\t\thighlight={ search }\n\t\t\t\t\t\t\t/>\n\t\t\t\t\t\t</span>\n\t\t\t\t\t</HStack>\n\t\t\t\t</Command.Item>\n\t\t\t) ) }\n\t\t\t{ loaders.map( ( loader ) => (\n\t\t\t\t<CommandMenuLoaderWrapper\n\t\t\t\t\tkey={ loader.name }\n\t\t\t\t\thook={ loader.hook }\n\t\t\t\t\tsearch={ search }\n\t\t\t\t\tsetLoader={ setLoader }\n\t\t\t\t\tclose={ close }\n\t\t\t\t/>\n\t\t\t) ) }\n\t\t</Command.Group>\n\t);\n}\n\nexport function CommandMenu() {\n\tconst { registerShortcut } = useDispatch( keyboardShortcutsStore );\n\tconst [ search, setSearch ] = useState( '' );\n\tconst [ open, setOpen ] = useState( false );\n\tconst { groups } = useSelect( ( select ) => {\n\t\tconst { getGroups } = select( commandsStore );\n\t\treturn {\n\t\t\tgroups: getGroups(),\n\t\t};\n\t}, [] );\n\tconst [ loaders, setLoaders ] = useState( {} );\n\n\tuseEffect( () => {\n\t\tregisterShortcut( {\n\t\t\tname: 'core/commands',\n\t\t\tcategory: 'global',\n\t\t\tdescription: __( 'Open the global command menu' ),\n\t\t\tkeyCombination: {\n\t\t\t\tmodifier: 'primary',\n\t\t\t\tcharacter: 'k',\n\t\t\t},\n\t\t} );\n\t}, [ registerShortcut ] );\n\n\tuseShortcut(\n\t\t'core/commands',\n\t\t( event ) => {\n\t\t\tevent.preventDefault();\n\t\t\tsetOpen( ( prevOpen ) => ! prevOpen );\n\t\t},\n\t\t{\n\t\t\tbindGlobal: true,\n\t\t}\n\t);\n\n\tconst setLoader = useCallback(\n\t\t( name, value ) =>\n\t\t\tsetLoaders( ( current ) => ( {\n\t\t\t\t...current,\n\t\t\t\t[ name ]: value,\n\t\t\t} ) ),\n\t\t[]\n\t);\n\tconst close = () => {\n\t\tsetSearch( '' );\n\t\tsetOpen( false );\n\t};\n\n\tif ( ! open ) {\n\t\treturn false;\n\t}\n\tconst isLoading = Object.values( loaders ).some( Boolean );\n\n\treturn (\n\t\t<Modal\n\t\t\tclassName=\"commands-command-menu\"\n\t\t\toverlayClassName=\"commands-command-menu__overlay\"\n\t\t\tonRequestClose={ close }\n\t\t\t__experimentalHideHeader\n\t\t>\n\t\t\t<div className=\"commands-command-menu__container\">\n\t\t\t\t<Command label={ __( 'Global Command Menu' ) }>\n\t\t\t\t\t<div className=\"commands-command-menu__header\">\n\t\t\t\t\t\t<Command.Input\n\t\t\t\t\t\t\t// The input should be focused when the modal is opened.\n\t\t\t\t\t\t\t// eslint-disable-next-line jsx-a11y/no-autofocus\n\t\t\t\t\t\t\tautoFocus\n\t\t\t\t\t\t\tvalue={ search }\n\t\t\t\t\t\t\tonValueChange={ setSearch }\n\t\t\t\t\t\t\tplaceholder={ __( 'Type a command or search' ) }\n\t\t\t\t\t\t/>\n\t\t\t\t\t</div>\n\t\t\t\t\t{ search && (\n\t\t\t\t\t\t<Command.List>\n\t\t\t\t\t\t\t{ ! isLoading && (\n\t\t\t\t\t\t\t\t<Command.Empty>\n\t\t\t\t\t\t\t\t\t{ __( 'No results found.' ) }\n\t\t\t\t\t\t\t\t</Command.Empty>\n\t\t\t\t\t\t\t) }\n\t\t\t\t\t\t\t{ groups.map( ( group ) => (\n\t\t\t\t\t\t\t\t<CommandMenuGroup\n\t\t\t\t\t\t\t\t\tkey={ group }\n\t\t\t\t\t\t\t\t\tgroup={ group }\n\t\t\t\t\t\t\t\t\tsearch={ search }\n\t\t\t\t\t\t\t\t\tsetLoader={ setLoader }\n\t\t\t\t\t\t\t\t\tclose={ close }\n\t\t\t\t\t\t\t\t/>\n\t\t\t\t\t\t\t) ) }\n\t\t\t\t\t\t</Command.List>\n\t\t\t\t\t) }\n\t\t\t\t</Command>\n\t\t\t</div>\n\t\t</Modal>\n\t);\n}\n"]}
|
|
1
|
+
{"version":3,"sources":["@wordpress/commands/src/components/command-menu.js"],"names":["Command","useSelect","useDispatch","useState","useEffect","useRef","useCallback","__","Modal","TextHighlight","__experimentalHStack","HStack","store","keyboardShortcutsStore","useShortcut","Icon","commandsStore","CommandMenuLoader","name","search","hook","setLoader","close","isLoading","commands","length","map","command","searchLabel","label","callback","icon","CommandMenuLoaderWrapper","currentLoader","key","setKey","current","prevKey","CommandMenuGroup","isContextual","loaders","select","getCommands","getCommandLoaders","loader","CommandMenu","registerShortcut","setSearch","isOpen","open","setLoaders","commandMenuInput","category","description","keyCombination","modifier","character","event","preventDefault","bindGlobal","value","closeAndReset","focus","Object","values","some","Boolean"],"mappings":";;AAAA;AACA;AACA;AACA,SAASA,OAAT,QAAwB,MAAxB;AAEA;AACA;AACA;;AACA,SAASC,SAAT,EAAoBC,WAApB,QAAuC,iBAAvC;AACA,SAASC,QAAT,EAAmBC,SAAnB,EAA8BC,MAA9B,EAAsCC,WAAtC,QAAyD,oBAAzD;AACA,SAASC,EAAT,QAAmB,iBAAnB;AACA,SACCC,KADD,EAECC,aAFD,EAGCC,oBAAoB,IAAIC,MAHzB,QAIO,uBAJP;AAKA,SACCC,KAAK,IAAIC,sBADV,EAECC,WAFD,QAGO,+BAHP;AAIA,SAASC,IAAT,QAAqB,kBAArB;AAEA;AACA;AACA;;AACA,SAASH,KAAK,IAAII,aAAlB,QAAuC,UAAvC;;AAEA,SAASC,iBAAT,OAAuE;AAAA;;AAAA,MAA3C;AAAEC,IAAAA,IAAF;AAAQC,IAAAA,MAAR;AAAgBC,IAAAA,IAAhB;AAAsBC,IAAAA,SAAtB;AAAiCC,IAAAA;AAAjC,GAA2C;AACtE,QAAM;AAAEC,IAAAA,SAAF;AAAaC,IAAAA,QAAQ,GAAG;AAAxB,eAA+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cAAc,EAAGL,aAHlB;AAIC,IAAA,wBAAwB;AAJzB,KAMC;AAAK,IAAA,SAAS,EAAC;AAAf,KACC,cAAC,OAAD;AAAS,IAAA,KAAK,EAAGtD,EAAE,CAAE,qBAAF;AAAnB,KACC;AAAK,IAAA,SAAS,EAAC;AAAf,KACC,cAAC,OAAD,CAAS,KAAT;AACC,IAAA,GAAG,EAAG4C,gBADP;AAEC,IAAA,KAAK,EAAGhC,MAFT;AAGC,IAAA,aAAa,EAAG4B,SAHjB;AAIC,IAAA,WAAW,EAAGxC,EAAE,CAAE,0BAAF;AAJjB,IADD,CADD,EASC,cAAC,OAAD,CAAS,IAAT,QACGY,MAAM,IAAI,CAAEI,SAAZ,IACD,cAAC,OAAD,CAAS,KAAT,QACGhB,EAAE,CAAE,mBAAF,CADL,CAFF,EAMC,cAAC,gBAAD;AACC,IAAA,MAAM,EAAGY,MADV;AAEC,IAAA,SAAS,EAAGE,SAFb;AAGC,IAAA,KAAK,EAAGwC,aAHT;AAIC,IAAA,YAAY;AAJb,IAND,EAYG1C,MAAM,IACP,cAAC,gBAAD;AACC,IAAA,MAAM,EAAGA,MADV;AAEC,IAAA,SAAS,EAAGE,SAFb;AAGC,IAAA,KAAK,EAAGwC;AAHT,IAbF,CATD,CADD,CAND,CADD;AAyCA","sourcesContent":["/**\n * External dependencies\n */\nimport { Command } from 'cmdk';\n\n/**\n * WordPress dependencies\n */\nimport { useSelect, useDispatch } from '@wordpress/data';\nimport { useState, useEffect, useRef, useCallback } from '@wordpress/element';\nimport { __ } from '@wordpress/i18n';\nimport {\n\tModal,\n\tTextHighlight,\n\t__experimentalHStack as HStack,\n} from '@wordpress/components';\nimport {\n\tstore as keyboardShortcutsStore,\n\tuseShortcut,\n} from '@wordpress/keyboard-shortcuts';\nimport { Icon } from '@wordpress/icons';\n\n/**\n * Internal dependencies\n */\nimport { store as commandsStore } from '../store';\n\nfunction CommandMenuLoader( { name, search, hook, setLoader, close } ) {\n\tconst { isLoading, commands = [] } = hook( { search } ) ?? {};\n\tuseEffect( () => {\n\t\tsetLoader( name, isLoading );\n\t}, [ setLoader, name, isLoading ] );\n\n\tif ( ! commands.length ) {\n\t\treturn null;\n\t}\n\n\treturn (\n\t\t<>\n\t\t\t<Command.List>\n\t\t\t\t{ commands.map( ( command ) => (\n\t\t\t\t\t<Command.Item\n\t\t\t\t\t\tkey={ command.name }\n\t\t\t\t\t\tvalue={ command.searchLabel ?? command.label }\n\t\t\t\t\t\tonSelect={ () => command.callback( { close } ) }\n\t\t\t\t\t>\n\t\t\t\t\t\t<HStack\n\t\t\t\t\t\t\talignment=\"left\"\n\t\t\t\t\t\t\tclassName=\"commands-command-menu__item\"\n\t\t\t\t\t\t>\n\t\t\t\t\t\t\t<Icon icon={ command.icon } />\n\t\t\t\t\t\t\t<span>\n\t\t\t\t\t\t\t\t<TextHighlight\n\t\t\t\t\t\t\t\t\ttext={ command.label }\n\t\t\t\t\t\t\t\t\thighlight={ search }\n\t\t\t\t\t\t\t\t/>\n\t\t\t\t\t\t\t</span>\n\t\t\t\t\t\t</HStack>\n\t\t\t\t\t</Command.Item>\n\t\t\t\t) ) }\n\t\t\t</Command.List>\n\t\t</>\n\t);\n}\n\nexport function CommandMenuLoaderWrapper( { hook, search, setLoader, close } ) {\n\t// The \"hook\" prop is actually a custom React hook\n\t// so to avoid breaking the rules of hooks\n\t// the CommandMenuLoaderWrapper component need to be\n\t// remounted on each hook prop change\n\t// We use the key state to make sure we do that properly.\n\tconst currentLoader = useRef( hook );\n\tconst [ key, setKey ] = useState( 0 );\n\tuseEffect( () => {\n\t\tif ( currentLoader.current !== hook ) {\n\t\t\tcurrentLoader.current = hook;\n\t\t\tsetKey( ( prevKey ) => prevKey + 1 );\n\t\t}\n\t}, [ hook ] );\n\n\treturn (\n\t\t<CommandMenuLoader\n\t\t\tkey={ key }\n\t\t\thook={ currentLoader.current }\n\t\t\tsearch={ search }\n\t\t\tsetLoader={ setLoader }\n\t\t\tclose={ close }\n\t\t/>\n\t);\n}\n\nexport function CommandMenuGroup( { isContextual, search, setLoader, close } ) {\n\tconst { commands, loaders } = useSelect(\n\t\t( select ) => {\n\t\t\tconst { getCommands, getCommandLoaders } = select( commandsStore );\n\t\t\treturn {\n\t\t\t\tcommands: getCommands( isContextual ),\n\t\t\t\tloaders: getCommandLoaders( isContextual ),\n\t\t\t};\n\t\t},\n\t\t[ isContextual ]\n\t);\n\n\tif ( ! commands.length && ! loaders.length ) {\n\t\treturn null;\n\t}\n\n\treturn (\n\t\t<Command.Group>\n\t\t\t{ commands.map( ( command ) => (\n\t\t\t\t<Command.Item\n\t\t\t\t\tkey={ command.name }\n\t\t\t\t\tvalue={ command.searchLabel ?? command.label }\n\t\t\t\t\tonSelect={ () => command.callback( { close } ) }\n\t\t\t\t>\n\t\t\t\t\t<HStack\n\t\t\t\t\t\talignment=\"left\"\n\t\t\t\t\t\tclassName=\"commands-command-menu__item\"\n\t\t\t\t\t>\n\t\t\t\t\t\t<Icon icon={ command.icon } />\n\t\t\t\t\t\t<span>\n\t\t\t\t\t\t\t<TextHighlight\n\t\t\t\t\t\t\t\ttext={ command.label }\n\t\t\t\t\t\t\t\thighlight={ search }\n\t\t\t\t\t\t\t/>\n\t\t\t\t\t\t</span>\n\t\t\t\t\t</HStack>\n\t\t\t\t</Command.Item>\n\t\t\t) ) }\n\t\t\t{ loaders.map( ( loader ) => (\n\t\t\t\t<CommandMenuLoaderWrapper\n\t\t\t\t\tkey={ loader.name }\n\t\t\t\t\thook={ loader.hook }\n\t\t\t\t\tsearch={ search }\n\t\t\t\t\tsetLoader={ setLoader }\n\t\t\t\t\tclose={ close }\n\t\t\t\t/>\n\t\t\t) ) }\n\t\t</Command.Group>\n\t);\n}\n\nexport function CommandMenu() {\n\tconst { registerShortcut } = useDispatch( keyboardShortcutsStore );\n\tconst [ search, setSearch ] = useState( '' );\n\tconst isOpen = useSelect(\n\t\t( select ) => select( commandsStore ).isOpen(),\n\t\t[]\n\t);\n\tconst { open, close } = useDispatch( commandsStore );\n\tconst [ loaders, setLoaders ] = useState( {} );\n\tconst commandMenuInput = useRef();\n\n\tuseEffect( () => {\n\t\tregisterShortcut( {\n\t\t\tname: 'core/commands',\n\t\t\tcategory: 'global',\n\t\t\tdescription: __( 'Open the global command menu' ),\n\t\t\tkeyCombination: {\n\t\t\t\tmodifier: 'primary',\n\t\t\t\tcharacter: 'k',\n\t\t\t},\n\t\t} );\n\t}, [ registerShortcut ] );\n\n\tuseShortcut(\n\t\t'core/commands',\n\t\t( event ) => {\n\t\t\tevent.preventDefault();\n\t\t\tif ( isOpen ) {\n\t\t\t\tclose();\n\t\t\t} else {\n\t\t\t\topen();\n\t\t\t}\n\t\t},\n\t\t{\n\t\t\tbindGlobal: true,\n\t\t}\n\t);\n\n\tconst setLoader = useCallback(\n\t\t( name, value ) =>\n\t\t\tsetLoaders( ( current ) => ( {\n\t\t\t\t...current,\n\t\t\t\t[ name ]: value,\n\t\t\t} ) ),\n\t\t[]\n\t);\n\tconst closeAndReset = () => {\n\t\tsetSearch( '' );\n\t\tclose();\n\t};\n\n\tuseEffect( () => {\n\t\t// Focus the command menu input when mounting the modal.\n\t\tif ( isOpen ) {\n\t\t\tcommandMenuInput.current.focus();\n\t\t}\n\t}, [ isOpen ] );\n\n\tif ( ! isOpen ) {\n\t\treturn false;\n\t}\n\tconst isLoading = Object.values( loaders ).some( Boolean );\n\n\treturn (\n\t\t<Modal\n\t\t\tclassName=\"commands-command-menu\"\n\t\t\toverlayClassName=\"commands-command-menu__overlay\"\n\t\t\tonRequestClose={ closeAndReset }\n\t\t\t__experimentalHideHeader\n\t\t>\n\t\t\t<div className=\"commands-command-menu__container\">\n\t\t\t\t<Command label={ __( 'Global Command Menu' ) }>\n\t\t\t\t\t<div className=\"commands-command-menu__header\">\n\t\t\t\t\t\t<Command.Input\n\t\t\t\t\t\t\tref={ commandMenuInput }\n\t\t\t\t\t\t\tvalue={ search }\n\t\t\t\t\t\t\tonValueChange={ setSearch }\n\t\t\t\t\t\t\tplaceholder={ __( 'Type a command or search' ) }\n\t\t\t\t\t\t/>\n\t\t\t\t\t</div>\n\t\t\t\t\t<Command.List>\n\t\t\t\t\t\t{ search && ! isLoading && (\n\t\t\t\t\t\t\t<Command.Empty>\n\t\t\t\t\t\t\t\t{ __( 'No results found.' ) }\n\t\t\t\t\t\t\t</Command.Empty>\n\t\t\t\t\t\t) }\n\t\t\t\t\t\t<CommandMenuGroup\n\t\t\t\t\t\t\tsearch={ search }\n\t\t\t\t\t\t\tsetLoader={ setLoader }\n\t\t\t\t\t\t\tclose={ closeAndReset }\n\t\t\t\t\t\t\tisContextual\n\t\t\t\t\t\t/>\n\t\t\t\t\t\t{ search && (\n\t\t\t\t\t\t\t<CommandMenuGroup\n\t\t\t\t\t\t\t\tsearch={ search }\n\t\t\t\t\t\t\t\tsetLoader={ setLoader }\n\t\t\t\t\t\t\t\tclose={ closeAndReset }\n\t\t\t\t\t\t\t/>\n\t\t\t\t\t\t) }\n\t\t\t\t\t</Command.List>\n\t\t\t\t</Command>\n\t\t\t</div>\n\t\t</Modal>\n\t);\n}\n"]}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* WordPress dependencies
|
|
3
|
+
*/
|
|
4
|
+
import { useEffect, useRef } from '@wordpress/element';
|
|
5
|
+
import { useDispatch, useSelect } from '@wordpress/data';
|
|
6
|
+
/**
|
|
7
|
+
* Internal dependencies
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import { store as commandsStore } from '../store';
|
|
11
|
+
/**
|
|
12
|
+
* Sets the active context of the command center
|
|
13
|
+
*
|
|
14
|
+
* @param {string} context Context to set.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
export default function useCommandContext(context) {
|
|
18
|
+
const {
|
|
19
|
+
getContext
|
|
20
|
+
} = useSelect(commandsStore);
|
|
21
|
+
const initialContext = useRef(getContext());
|
|
22
|
+
const {
|
|
23
|
+
setContext
|
|
24
|
+
} = useDispatch(commandsStore);
|
|
25
|
+
useEffect(() => {
|
|
26
|
+
setContext(context);
|
|
27
|
+
}, [context, setContext]); // This effects ensures that on unmount, we restore the context
|
|
28
|
+
// that was set before the component actually mounts.
|
|
29
|
+
|
|
30
|
+
useEffect(() => {
|
|
31
|
+
const initialContextRef = initialContext.current;
|
|
32
|
+
return () => setContext(initialContextRef);
|
|
33
|
+
}, [setContext]);
|
|
34
|
+
}
|
|
35
|
+
//# sourceMappingURL=use-command-context.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["@wordpress/commands/src/hooks/use-command-context.js"],"names":["useEffect","useRef","useDispatch","useSelect","store","commandsStore","useCommandContext","context","getContext","initialContext","setContext","initialContextRef","current"],"mappings":"AAAA;AACA;AACA;AACA,SAASA,SAAT,EAAoBC,MAApB,QAAkC,oBAAlC;AACA,SAASC,WAAT,EAAsBC,SAAtB,QAAuC,iBAAvC;AAEA;AACA;AACA;;AACA,SAASC,KAAK,IAAIC,aAAlB,QAAuC,UAAvC;AAEA;AACA;AACA;AACA;AACA;;AACA,eAAe,SAASC,iBAAT,CAA4BC,OAA5B,EAAsC;AACpD,QAAM;AAAEC,IAAAA;AAAF,MAAiBL,SAAS,CAAEE,aAAF,CAAhC;AACA,QAAMI,cAAc,GAAGR,MAAM,CAAEO,UAAU,EAAZ,CAA7B;AACA,QAAM;AAAEE,IAAAA;AAAF,MAAiBR,WAAW,CAAEG,aAAF,CAAlC;AAEAL,EAAAA,SAAS,CAAE,MAAM;AAChBU,IAAAA,UAAU,CAAEH,OAAF,CAAV;AACA,GAFQ,EAEN,CAAEA,OAAF,EAAWG,UAAX,CAFM,CAAT,CALoD,CASpD;AACA;;AACAV,EAAAA,SAAS,CAAE,MAAM;AAChB,UAAMW,iBAAiB,GAAGF,cAAc,CAACG,OAAzC;AACA,WAAO,MAAMF,UAAU,CAAEC,iBAAF,CAAvB;AACA,GAHQ,EAGN,CAAED,UAAF,CAHM,CAAT;AAIA","sourcesContent":["/**\n * WordPress dependencies\n */\nimport { useEffect, useRef } from '@wordpress/element';\nimport { useDispatch, useSelect } from '@wordpress/data';\n\n/**\n * Internal dependencies\n */\nimport { store as commandsStore } from '../store';\n\n/**\n * Sets the active context of the command center\n *\n * @param {string} context Context to set.\n */\nexport default function useCommandContext( context ) {\n\tconst { getContext } = useSelect( commandsStore );\n\tconst initialContext = useRef( getContext() );\n\tconst { setContext } = useDispatch( commandsStore );\n\n\tuseEffect( () => {\n\t\tsetContext( context );\n\t}, [ context, setContext ] );\n\n\t// This effects ensures that on unmount, we restore the context\n\t// that was set before the component actually mounts.\n\tuseEffect( () => {\n\t\tconst initialContextRef = initialContext.current;\n\t\treturn () => setContext( initialContextRef );\n\t}, [ setContext ] );\n}\n"]}
|
|
@@ -14,25 +14,20 @@ import { store as commandsStore } from '../store';
|
|
|
14
14
|
* @param {import('../store/actions').WPCommandLoaderConfig} loader command loader config.
|
|
15
15
|
*/
|
|
16
16
|
|
|
17
|
-
export default function useCommandLoader(
|
|
18
|
-
let {
|
|
19
|
-
name,
|
|
20
|
-
group,
|
|
21
|
-
hook
|
|
22
|
-
} = _ref;
|
|
17
|
+
export default function useCommandLoader(loader) {
|
|
23
18
|
const {
|
|
24
19
|
registerCommandLoader,
|
|
25
20
|
unregisterCommandLoader
|
|
26
21
|
} = useDispatch(commandsStore);
|
|
27
22
|
useEffect(() => {
|
|
28
23
|
registerCommandLoader({
|
|
29
|
-
name,
|
|
30
|
-
|
|
31
|
-
|
|
24
|
+
name: loader.name,
|
|
25
|
+
hook: loader.hook,
|
|
26
|
+
context: loader.context
|
|
32
27
|
});
|
|
33
28
|
return () => {
|
|
34
|
-
unregisterCommandLoader(name
|
|
29
|
+
unregisterCommandLoader(loader.name);
|
|
35
30
|
};
|
|
36
|
-
}, [name,
|
|
31
|
+
}, [loader.name, loader.hook, loader.context, registerCommandLoader, unregisterCommandLoader]);
|
|
37
32
|
}
|
|
38
33
|
//# sourceMappingURL=use-command-loader.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["@wordpress/commands/src/hooks/use-command-loader.js"],"names":["useEffect","useDispatch","store","commandsStore","useCommandLoader","
|
|
1
|
+
{"version":3,"sources":["@wordpress/commands/src/hooks/use-command-loader.js"],"names":["useEffect","useDispatch","store","commandsStore","useCommandLoader","loader","registerCommandLoader","unregisterCommandLoader","name","hook","context"],"mappings":"AAAA;AACA;AACA;AACA,SAASA,SAAT,QAA0B,oBAA1B;AACA,SAASC,WAAT,QAA4B,iBAA5B;AAEA;AACA;AACA;;AACA,SAASC,KAAK,IAAIC,aAAlB,QAAuC,UAAvC;AAEA;AACA;AACA;AACA;AACA;;AACA,eAAe,SAASC,gBAAT,CAA2BC,MAA3B,EAAoC;AAClD,QAAM;AAAEC,IAAAA,qBAAF;AAAyBC,IAAAA;AAAzB,MACLN,WAAW,CAAEE,aAAF,CADZ;AAEAH,EAAAA,SAAS,CAAE,MAAM;AAChBM,IAAAA,qBAAqB,CAAE;AACtBE,MAAAA,IAAI,EAAEH,MAAM,CAACG,IADS;AAEtBC,MAAAA,IAAI,EAAEJ,MAAM,CAACI,IAFS;AAGtBC,MAAAA,OAAO,EAAEL,MAAM,CAACK;AAHM,KAAF,CAArB;AAKA,WAAO,MAAM;AACZH,MAAAA,uBAAuB,CAAEF,MAAM,CAACG,IAAT,CAAvB;AACA,KAFD;AAGA,GATQ,EASN,CACFH,MAAM,CAACG,IADL,EAEFH,MAAM,CAACI,IAFL,EAGFJ,MAAM,CAACK,OAHL,EAIFJ,qBAJE,EAKFC,uBALE,CATM,CAAT;AAgBA","sourcesContent":["/**\n * WordPress dependencies\n */\nimport { useEffect } from '@wordpress/element';\nimport { useDispatch } from '@wordpress/data';\n\n/**\n * Internal dependencies\n */\nimport { store as commandsStore } from '../store';\n\n/**\n * Attach a command loader to the Global command menu.\n *\n * @param {import('../store/actions').WPCommandLoaderConfig} loader command loader config.\n */\nexport default function useCommandLoader( loader ) {\n\tconst { registerCommandLoader, unregisterCommandLoader } =\n\t\tuseDispatch( commandsStore );\n\tuseEffect( () => {\n\t\tregisterCommandLoader( {\n\t\t\tname: loader.name,\n\t\t\thook: loader.hook,\n\t\t\tcontext: loader.context,\n\t\t} );\n\t\treturn () => {\n\t\t\tunregisterCommandLoader( loader.name );\n\t\t};\n\t}, [\n\t\tloader.name,\n\t\tloader.hook,\n\t\tloader.context,\n\t\tregisterCommandLoader,\n\t\tunregisterCommandLoader,\n\t] );\n}\n"]}
|
|
@@ -26,13 +26,15 @@ export default function useCommand(command) {
|
|
|
26
26
|
useEffect(() => {
|
|
27
27
|
registerCommand({
|
|
28
28
|
name: command.name,
|
|
29
|
-
|
|
29
|
+
context: command.context,
|
|
30
30
|
label: command.label,
|
|
31
|
+
searchLabel: command.searchLabel,
|
|
32
|
+
icon: command.icon,
|
|
31
33
|
callback: currentCallback.current
|
|
32
34
|
});
|
|
33
35
|
return () => {
|
|
34
|
-
unregisterCommand(command.name
|
|
36
|
+
unregisterCommand(command.name);
|
|
35
37
|
};
|
|
36
|
-
}, [command.name, command.label, command.
|
|
38
|
+
}, [command.name, command.label, command.searchLabel, command.icon, command.context, registerCommand, unregisterCommand]);
|
|
37
39
|
}
|
|
38
40
|
//# sourceMappingURL=use-command.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["@wordpress/commands/src/hooks/use-command.js"],"names":["useEffect","useRef","useDispatch","store","commandsStore","useCommand","command","registerCommand","unregisterCommand","currentCallback","callback","current","name","
|
|
1
|
+
{"version":3,"sources":["@wordpress/commands/src/hooks/use-command.js"],"names":["useEffect","useRef","useDispatch","store","commandsStore","useCommand","command","registerCommand","unregisterCommand","currentCallback","callback","current","name","context","label","searchLabel","icon"],"mappings":"AAAA;AACA;AACA;AACA,SAASA,SAAT,EAAoBC,MAApB,QAAkC,oBAAlC;AACA,SAASC,WAAT,QAA4B,iBAA5B;AAEA;AACA;AACA;;AACA,SAASC,KAAK,IAAIC,aAAlB,QAAuC,UAAvC;AAEA;AACA;AACA;AACA;AACA;;AACA,eAAe,SAASC,UAAT,CAAqBC,OAArB,EAA+B;AAC7C,QAAM;AAAEC,IAAAA,eAAF;AAAmBC,IAAAA;AAAnB,MAAyCN,WAAW,CAAEE,aAAF,CAA1D;AACA,QAAMK,eAAe,GAAGR,MAAM,CAAEK,OAAO,CAACI,QAAV,CAA9B;AACAV,EAAAA,SAAS,CAAE,MAAM;AAChBS,IAAAA,eAAe,CAACE,OAAhB,GAA0BL,OAAO,CAACI,QAAlC;AACA,GAFQ,EAEN,CAAEJ,OAAO,CAACI,QAAV,CAFM,CAAT;AAIAV,EAAAA,SAAS,CAAE,MAAM;AAChBO,IAAAA,eAAe,CAAE;AAChBK,MAAAA,IAAI,EAAEN,OAAO,CAACM,IADE;AAEhBC,MAAAA,OAAO,EAAEP,OAAO,CAACO,OAFD;AAGhBC,MAAAA,KAAK,EAAER,OAAO,CAACQ,KAHC;AAIhBC,MAAAA,WAAW,EAAET,OAAO,CAACS,WAJL;AAKhBC,MAAAA,IAAI,EAAEV,OAAO,CAACU,IALE;AAMhBN,MAAAA,QAAQ,EAAED,eAAe,CAACE;AANV,KAAF,CAAf;AAQA,WAAO,MAAM;AACZH,MAAAA,iBAAiB,CAAEF,OAAO,CAACM,IAAV,CAAjB;AACA,KAFD;AAGA,GAZQ,EAYN,CACFN,OAAO,CAACM,IADN,EAEFN,OAAO,CAACQ,KAFN,EAGFR,OAAO,CAACS,WAHN,EAIFT,OAAO,CAACU,IAJN,EAKFV,OAAO,CAACO,OALN,EAMFN,eANE,EAOFC,iBAPE,CAZM,CAAT;AAqBA","sourcesContent":["/**\n * WordPress dependencies\n */\nimport { useEffect, useRef } from '@wordpress/element';\nimport { useDispatch } from '@wordpress/data';\n\n/**\n * Internal dependencies\n */\nimport { store as commandsStore } from '../store';\n\n/**\n * Attach a command to the Global command menu.\n *\n * @param {import('../store/actions').WPCommandConfig} command command config.\n */\nexport default function useCommand( command ) {\n\tconst { registerCommand, unregisterCommand } = useDispatch( commandsStore );\n\tconst currentCallback = useRef( command.callback );\n\tuseEffect( () => {\n\t\tcurrentCallback.current = command.callback;\n\t}, [ command.callback ] );\n\n\tuseEffect( () => {\n\t\tregisterCommand( {\n\t\t\tname: command.name,\n\t\t\tcontext: command.context,\n\t\t\tlabel: command.label,\n\t\t\tsearchLabel: command.searchLabel,\n\t\t\ticon: command.icon,\n\t\t\tcallback: currentCallback.current,\n\t\t} );\n\t\treturn () => {\n\t\t\tunregisterCommand( command.name );\n\t\t};\n\t}, [\n\t\tcommand.name,\n\t\tcommand.label,\n\t\tcommand.searchLabel,\n\t\tcommand.icon,\n\t\tcommand.context,\n\t\tregisterCommand,\n\t\tunregisterCommand,\n\t] );\n}\n"]}
|
package/build-module/index.js
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
1
|
export { CommandMenu } from './components/command-menu';
|
|
2
2
|
export { privateApis } from './private-apis';
|
|
3
|
+
export { default as useCommand } from './hooks/use-command';
|
|
4
|
+
export { default as useCommandLoader } from './hooks/use-command-loader';
|
|
3
5
|
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["@wordpress/commands/src/index.js"],"names":["CommandMenu","privateApis"],"mappings":"AAAA,SAASA,WAAT,QAA4B,2BAA5B;AACA,SAASC,WAAT,QAA4B,gBAA5B","sourcesContent":["export { CommandMenu } from './components/command-menu';\nexport { privateApis } from './private-apis';\n"]}
|
|
1
|
+
{"version":3,"sources":["@wordpress/commands/src/index.js"],"names":["CommandMenu","privateApis","default","useCommand","useCommandLoader"],"mappings":"AAAA,SAASA,WAAT,QAA4B,2BAA5B;AACA,SAASC,WAAT,QAA4B,gBAA5B;AACA,SAASC,OAAO,IAAIC,UAApB,QAAsC,qBAAtC;AACA,SAASD,OAAO,IAAIE,gBAApB,QAA4C,4BAA5C","sourcesContent":["export { CommandMenu } from './components/command-menu';\nexport { privateApis } from './private-apis';\nexport { default as useCommand } from './hooks/use-command';\nexport { default as useCommandLoader } from './hooks/use-command-loader';\n"]}
|
|
@@ -6,15 +6,15 @@ import { __dangerousOptInToUnstableAPIsOnlyForCoreModules } from '@wordpress/pri
|
|
|
6
6
|
* Internal dependencies
|
|
7
7
|
*/
|
|
8
8
|
|
|
9
|
-
import { default as
|
|
10
|
-
import {
|
|
9
|
+
import { default as useCommandContext } from './hooks/use-command-context';
|
|
10
|
+
import { store } from './store';
|
|
11
11
|
export const {
|
|
12
12
|
lock,
|
|
13
13
|
unlock
|
|
14
14
|
} = __dangerousOptInToUnstableAPIsOnlyForCoreModules('I know using unstable features means my plugin or theme will inevitably break on the next WordPress release.', '@wordpress/commands');
|
|
15
15
|
export const privateApis = {};
|
|
16
16
|
lock(privateApis, {
|
|
17
|
-
|
|
18
|
-
|
|
17
|
+
useCommandContext,
|
|
18
|
+
store
|
|
19
19
|
});
|
|
20
20
|
//# sourceMappingURL=private-apis.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["@wordpress/commands/src/private-apis.js"],"names":["__dangerousOptInToUnstableAPIsOnlyForCoreModules","default","
|
|
1
|
+
{"version":3,"sources":["@wordpress/commands/src/private-apis.js"],"names":["__dangerousOptInToUnstableAPIsOnlyForCoreModules","default","useCommandContext","store","lock","unlock","privateApis"],"mappings":"AAAA;AACA;AACA;AACA,SAASA,gDAAT,QAAiE,yBAAjE;AAEA;AACA;AACA;;AACA,SAASC,OAAO,IAAIC,iBAApB,QAA6C,6BAA7C;AACA,SAASC,KAAT,QAAsB,SAAtB;AAEA,OAAO,MAAM;AAAEC,EAAAA,IAAF;AAAQC,EAAAA;AAAR,IACZL,gDAAgD,CAC/C,8GAD+C,EAE/C,qBAF+C,CAD1C;AAMP,OAAO,MAAMM,WAAW,GAAG,EAApB;AACPF,IAAI,CAAEE,WAAF,EAAe;AAClBJ,EAAAA,iBADkB;AAElBC,EAAAA;AAFkB,CAAf,CAAJ","sourcesContent":["/**\n * WordPress dependencies\n */\nimport { __dangerousOptInToUnstableAPIsOnlyForCoreModules } from '@wordpress/private-apis';\n\n/**\n * Internal dependencies\n */\nimport { default as useCommandContext } from './hooks/use-command-context';\nimport { store } from './store';\n\nexport const { lock, unlock } =\n\t__dangerousOptInToUnstableAPIsOnlyForCoreModules(\n\t\t'I know using unstable features means my plugin or theme will inevitably break on the next WordPress release.',\n\t\t'@wordpress/commands'\n\t);\n\nexport const privateApis = {};\nlock( privateApis, {\n\tuseCommandContext,\n\tstore,\n} );\n"]}
|
|
@@ -5,11 +5,12 @@
|
|
|
5
5
|
*
|
|
6
6
|
* @typedef {Object} WPCommandConfig
|
|
7
7
|
*
|
|
8
|
-
* @property {string} name
|
|
9
|
-
* @property {string} label
|
|
10
|
-
* @property {string=}
|
|
11
|
-
* @property {
|
|
12
|
-
* @property {
|
|
8
|
+
* @property {string} name Command name.
|
|
9
|
+
* @property {string} label Command label.
|
|
10
|
+
* @property {string=} searchLabel Command search label.
|
|
11
|
+
* @property {string=} context Command context.
|
|
12
|
+
* @property {JSX.Element} icon Command icon.
|
|
13
|
+
* @property {Function} callback Command callback.
|
|
13
14
|
*/
|
|
14
15
|
|
|
15
16
|
/**
|
|
@@ -21,9 +22,9 @@
|
|
|
21
22
|
*
|
|
22
23
|
* @typedef {Object} WPCommandLoaderConfig
|
|
23
24
|
*
|
|
24
|
-
* @property {string} name
|
|
25
|
-
* @property {string=}
|
|
26
|
-
* @property {WPCommandLoaderHook} hook
|
|
25
|
+
* @property {string} name Command loader name.
|
|
26
|
+
* @property {string=} context Command loader context.
|
|
27
|
+
* @property {WPCommandLoaderHook} hook Command loader hook.
|
|
27
28
|
*/
|
|
28
29
|
|
|
29
30
|
/**
|
|
@@ -33,37 +34,24 @@
|
|
|
33
34
|
*
|
|
34
35
|
* @return {Object} action.
|
|
35
36
|
*/
|
|
36
|
-
export function registerCommand(
|
|
37
|
-
let {
|
|
38
|
-
name,
|
|
39
|
-
label,
|
|
40
|
-
icon,
|
|
41
|
-
callback,
|
|
42
|
-
group = ''
|
|
43
|
-
} = _ref;
|
|
37
|
+
export function registerCommand(config) {
|
|
44
38
|
return {
|
|
45
39
|
type: 'REGISTER_COMMAND',
|
|
46
|
-
|
|
47
|
-
label,
|
|
48
|
-
icon,
|
|
49
|
-
callback,
|
|
50
|
-
group
|
|
40
|
+
...config
|
|
51
41
|
};
|
|
52
42
|
}
|
|
53
43
|
/**
|
|
54
44
|
* Returns an action object used to unregister a command.
|
|
55
45
|
*
|
|
56
|
-
* @param {string} name
|
|
57
|
-
* @param {string} group Command group.
|
|
46
|
+
* @param {string} name Command name.
|
|
58
47
|
*
|
|
59
48
|
* @return {Object} action.
|
|
60
49
|
*/
|
|
61
50
|
|
|
62
|
-
export function unregisterCommand(name
|
|
51
|
+
export function unregisterCommand(name) {
|
|
63
52
|
return {
|
|
64
53
|
type: 'UNREGISTER_COMMAND',
|
|
65
|
-
name
|
|
66
|
-
group
|
|
54
|
+
name
|
|
67
55
|
};
|
|
68
56
|
}
|
|
69
57
|
/**
|
|
@@ -74,33 +62,60 @@ export function unregisterCommand(name, group) {
|
|
|
74
62
|
* @return {Object} action.
|
|
75
63
|
*/
|
|
76
64
|
|
|
77
|
-
export function registerCommandLoader(
|
|
78
|
-
let {
|
|
79
|
-
name,
|
|
80
|
-
group = '',
|
|
81
|
-
hook
|
|
82
|
-
} = _ref2;
|
|
65
|
+
export function registerCommandLoader(config) {
|
|
83
66
|
return {
|
|
84
67
|
type: 'REGISTER_COMMAND_LOADER',
|
|
85
|
-
|
|
86
|
-
group,
|
|
87
|
-
hook
|
|
68
|
+
...config
|
|
88
69
|
};
|
|
89
70
|
}
|
|
90
71
|
/**
|
|
91
72
|
* Unregister command loader hook.
|
|
92
73
|
*
|
|
93
|
-
* @param {string} name
|
|
94
|
-
* @param {string} group Command loader group.
|
|
74
|
+
* @param {string} name Command loader name.
|
|
95
75
|
*
|
|
96
76
|
* @return {Object} action.
|
|
97
77
|
*/
|
|
98
78
|
|
|
99
|
-
export function unregisterCommandLoader(name
|
|
79
|
+
export function unregisterCommandLoader(name) {
|
|
100
80
|
return {
|
|
101
81
|
type: 'UNREGISTER_COMMAND_LOADER',
|
|
102
|
-
name
|
|
103
|
-
|
|
82
|
+
name
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Opens the command center.
|
|
87
|
+
*
|
|
88
|
+
* @return {Object} action.
|
|
89
|
+
*/
|
|
90
|
+
|
|
91
|
+
export function open() {
|
|
92
|
+
return {
|
|
93
|
+
type: 'OPEN'
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Closes the command center.
|
|
98
|
+
*
|
|
99
|
+
* @return {Object} action.
|
|
100
|
+
*/
|
|
101
|
+
|
|
102
|
+
export function close() {
|
|
103
|
+
return {
|
|
104
|
+
type: 'CLOSE'
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Sets the active context.
|
|
109
|
+
*
|
|
110
|
+
* @param {string} context Context.
|
|
111
|
+
*
|
|
112
|
+
* @return {Object} action.
|
|
113
|
+
*/
|
|
114
|
+
|
|
115
|
+
export function setContext(context) {
|
|
116
|
+
return {
|
|
117
|
+
type: 'SET_CONTEXT',
|
|
118
|
+
context
|
|
104
119
|
};
|
|
105
120
|
}
|
|
106
121
|
//# sourceMappingURL=actions.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["@wordpress/commands/src/store/actions.js"],"names":["registerCommand","
|
|
1
|
+
{"version":3,"sources":["@wordpress/commands/src/store/actions.js"],"names":["registerCommand","config","type","unregisterCommand","name","registerCommandLoader","unregisterCommandLoader","open","close","setContext","context"],"mappings":"AAAA;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAEA;AACA;AACA;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,SAASA,eAAT,CAA0BC,MAA1B,EAAmC;AACzC,SAAO;AACNC,IAAAA,IAAI,EAAE,kBADA;AAEN,OAAGD;AAFG,GAAP;AAIA;AAED;AACA;AACA;AACA;AACA;AACA;AACA;;AACA,OAAO,SAASE,iBAAT,CAA4BC,IAA5B,EAAmC;AACzC,SAAO;AACNF,IAAAA,IAAI,EAAE,oBADA;AAENE,IAAAA;AAFM,GAAP;AAIA;AAED;AACA;AACA;AACA;AACA;AACA;AACA;;AACA,OAAO,SAASC,qBAAT,CAAgCJ,MAAhC,EAAyC;AAC/C,SAAO;AACNC,IAAAA,IAAI,EAAE,yBADA;AAEN,OAAGD;AAFG,GAAP;AAIA;AAED;AACA;AACA;AACA;AACA;AACA;AACA;;AACA,OAAO,SAASK,uBAAT,CAAkCF,IAAlC,EAAyC;AAC/C,SAAO;AACNF,IAAAA,IAAI,EAAE,2BADA;AAENE,IAAAA;AAFM,GAAP;AAIA;AAED;AACA;AACA;AACA;AACA;;AACA,OAAO,SAASG,IAAT,GAAgB;AACtB,SAAO;AACNL,IAAAA,IAAI,EAAE;AADA,GAAP;AAGA;AAED;AACA;AACA;AACA;AACA;;AACA,OAAO,SAASM,KAAT,GAAiB;AACvB,SAAO;AACNN,IAAAA,IAAI,EAAE;AADA,GAAP;AAGA;AAED;AACA;AACA;AACA;AACA;AACA;AACA;;AACA,OAAO,SAASO,UAAT,CAAqBC,OAArB,EAA+B;AACrC,SAAO;AACNR,IAAAA,IAAI,EAAE,aADA;AAENQ,IAAAA;AAFM,GAAP;AAIA","sourcesContent":["/** @typedef {import('@wordpress/keycodes').WPKeycodeModifier} WPKeycodeModifier */\n\n/**\n * Configuration of a registered keyboard shortcut.\n *\n * @typedef {Object} WPCommandConfig\n *\n * @property {string} name Command name.\n * @property {string} label Command label.\n * @property {string=} searchLabel Command search label.\n * @property {string=} context Command context.\n * @property {JSX.Element} icon Command icon.\n * @property {Function} callback Command callback.\n */\n\n/**\n * @typedef {(search: string) => WPCommandConfig[]} WPCommandLoaderHook hoo\n */\n\n/**\n * Command loader config.\n *\n * @typedef {Object} WPCommandLoaderConfig\n *\n * @property {string} name Command loader name.\n * @property {string=} context Command loader context.\n * @property {WPCommandLoaderHook} hook Command loader hook.\n */\n\n/**\n * Returns an action object used to register a new command.\n *\n * @param {WPCommandConfig} config Command config.\n *\n * @return {Object} action.\n */\nexport function registerCommand( config ) {\n\treturn {\n\t\ttype: 'REGISTER_COMMAND',\n\t\t...config,\n\t};\n}\n\n/**\n * Returns an action object used to unregister a command.\n *\n * @param {string} name Command name.\n *\n * @return {Object} action.\n */\nexport function unregisterCommand( name ) {\n\treturn {\n\t\ttype: 'UNREGISTER_COMMAND',\n\t\tname,\n\t};\n}\n\n/**\n * Register command loader.\n *\n * @param {WPCommandLoaderConfig} config Command loader config.\n *\n * @return {Object} action.\n */\nexport function registerCommandLoader( config ) {\n\treturn {\n\t\ttype: 'REGISTER_COMMAND_LOADER',\n\t\t...config,\n\t};\n}\n\n/**\n * Unregister command loader hook.\n *\n * @param {string} name Command loader name.\n *\n * @return {Object} action.\n */\nexport function unregisterCommandLoader( name ) {\n\treturn {\n\t\ttype: 'UNREGISTER_COMMAND_LOADER',\n\t\tname,\n\t};\n}\n\n/**\n * Opens the command center.\n *\n * @return {Object} action.\n */\nexport function open() {\n\treturn {\n\t\ttype: 'OPEN',\n\t};\n}\n\n/**\n * Closes the command center.\n *\n * @return {Object} action.\n */\nexport function close() {\n\treturn {\n\t\ttype: 'CLOSE',\n\t};\n}\n\n/**\n * Sets the active context.\n *\n * @param {string} context Context.\n *\n * @return {Object} action.\n */\nexport function setContext( context ) {\n\treturn {\n\t\ttype: 'SET_CONTEXT',\n\t\tcontext,\n\t};\n}\n"]}
|
|
@@ -18,14 +18,13 @@ function commands() {
|
|
|
18
18
|
switch (action.type) {
|
|
19
19
|
case 'REGISTER_COMMAND':
|
|
20
20
|
return { ...state,
|
|
21
|
-
[action.
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
}
|
|
21
|
+
[action.name]: {
|
|
22
|
+
name: action.name,
|
|
23
|
+
label: action.label,
|
|
24
|
+
searchLabel: action.searchLabel,
|
|
25
|
+
context: action.context,
|
|
26
|
+
callback: action.callback,
|
|
27
|
+
icon: action.icon
|
|
29
28
|
}
|
|
30
29
|
};
|
|
31
30
|
|
|
@@ -34,10 +33,8 @@ function commands() {
|
|
|
34
33
|
const {
|
|
35
34
|
[action.name]: _,
|
|
36
35
|
...remainingState
|
|
37
|
-
} = state
|
|
38
|
-
return
|
|
39
|
-
[action.group]: remainingState
|
|
40
|
-
};
|
|
36
|
+
} = state;
|
|
37
|
+
return remainingState;
|
|
41
38
|
}
|
|
42
39
|
}
|
|
43
40
|
|
|
@@ -60,11 +57,10 @@ function commandLoaders() {
|
|
|
60
57
|
switch (action.type) {
|
|
61
58
|
case 'REGISTER_COMMAND_LOADER':
|
|
62
59
|
return { ...state,
|
|
63
|
-
[action.
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
}
|
|
60
|
+
[action.name]: {
|
|
61
|
+
name: action.name,
|
|
62
|
+
context: action.context,
|
|
63
|
+
hook: action.hook
|
|
68
64
|
}
|
|
69
65
|
};
|
|
70
66
|
|
|
@@ -73,19 +69,64 @@ function commandLoaders() {
|
|
|
73
69
|
const {
|
|
74
70
|
[action.name]: _,
|
|
75
71
|
...remainingState
|
|
76
|
-
} = state
|
|
77
|
-
return
|
|
78
|
-
[action.group]: remainingState
|
|
79
|
-
};
|
|
72
|
+
} = state;
|
|
73
|
+
return remainingState;
|
|
80
74
|
}
|
|
81
75
|
}
|
|
82
76
|
|
|
83
77
|
return state;
|
|
84
78
|
}
|
|
79
|
+
/**
|
|
80
|
+
* Reducer returning the command center open state.
|
|
81
|
+
*
|
|
82
|
+
* @param {Object} state Current state.
|
|
83
|
+
* @param {Object} action Dispatched action.
|
|
84
|
+
*
|
|
85
|
+
* @return {boolean} Updated state.
|
|
86
|
+
*/
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
function isOpen() {
|
|
90
|
+
let state = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
|
|
91
|
+
let action = arguments.length > 1 ? arguments[1] : undefined;
|
|
92
|
+
|
|
93
|
+
switch (action.type) {
|
|
94
|
+
case 'OPEN':
|
|
95
|
+
return true;
|
|
96
|
+
|
|
97
|
+
case 'CLOSE':
|
|
98
|
+
return false;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
return state;
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* Reducer returning the command center's active context.
|
|
105
|
+
*
|
|
106
|
+
* @param {Object} state Current state.
|
|
107
|
+
* @param {Object} action Dispatched action.
|
|
108
|
+
*
|
|
109
|
+
* @return {boolean} Updated state.
|
|
110
|
+
*/
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
function context() {
|
|
114
|
+
let state = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 'root';
|
|
115
|
+
let action = arguments.length > 1 ? arguments[1] : undefined;
|
|
116
|
+
|
|
117
|
+
switch (action.type) {
|
|
118
|
+
case 'SET_CONTEXT':
|
|
119
|
+
return action.context;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
return state;
|
|
123
|
+
}
|
|
85
124
|
|
|
86
125
|
const reducer = combineReducers({
|
|
87
126
|
commands,
|
|
88
|
-
commandLoaders
|
|
127
|
+
commandLoaders,
|
|
128
|
+
isOpen,
|
|
129
|
+
context
|
|
89
130
|
});
|
|
90
131
|
export default reducer;
|
|
91
132
|
//# sourceMappingURL=reducer.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["@wordpress/commands/src/store/reducer.js"],"names":["combineReducers","commands","state","action","type","
|
|
1
|
+
{"version":3,"sources":["@wordpress/commands/src/store/reducer.js"],"names":["combineReducers","commands","state","action","type","name","label","searchLabel","context","callback","icon","_","remainingState","commandLoaders","hook","isOpen","reducer"],"mappings":"AAAA;AACA;AACA;AACA,SAASA,eAAT,QAAgC,iBAAh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AAP;AACA;AAbF;;AAgBA,SAAOV,KAAP;AACA;AAED;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;;AACA,SAASa,MAAT,GAAyC;AAAA,MAAxBb,KAAwB,uEAAhB,KAAgB;AAAA,MAATC,MAAS;;AACxC,UAASA,MAAM,CAACC,IAAhB;AACC,SAAK,MAAL;AACC,aAAO,IAAP;;AACD,SAAK,OAAL;AACC,aAAO,KAAP;AAJF;;AAOA,SAAOF,KAAP;AACA;AAED;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;;AACA,SAASM,OAAT,GAA2C;AAAA,MAAzBN,KAAyB,uEAAjB,MAAiB;AAAA,MAATC,MAAS;;AAC1C,UAASA,MAAM,CAACC,IAAhB;AACC,SAAK,aAAL;AACC,aAAOD,MAAM,CAACK,OAAd;AAFF;;AAKA,SAAON,KAAP;AACA;;AAED,MAAMc,OAAO,GAAGhB,eAAe,CAAE;AAChCC,EAAAA,QADgC;AAEhCY,EAAAA,cAFgC;AAGhCE,EAAAA,MAHgC;AAIhCP,EAAAA;AAJgC,CAAF,CAA/B;AAOA,eAAeQ,OAAf","sourcesContent":["/**\n * WordPress dependencies\n */\nimport { combineReducers } from '@wordpress/data';\n\n/**\n * Reducer returning the registered commands\n *\n * @param {Object} state Current state.\n * @param {Object} action Dispatched action.\n *\n * @return {Object} Updated state.\n */\nfunction commands( state = {}, action ) {\n\tswitch ( action.type ) {\n\t\tcase 'REGISTER_COMMAND':\n\t\t\treturn {\n\t\t\t\t...state,\n\t\t\t\t[ action.name ]: {\n\t\t\t\t\tname: action.name,\n\t\t\t\t\tlabel: action.label,\n\t\t\t\t\tsearchLabel: action.searchLabel,\n\t\t\t\t\tcontext: action.context,\n\t\t\t\t\tcallback: action.callback,\n\t\t\t\t\ticon: action.icon,\n\t\t\t\t},\n\t\t\t};\n\t\tcase 'UNREGISTER_COMMAND': {\n\t\t\tconst { [ action.name ]: _, ...remainingState } = state;\n\t\t\treturn remainingState;\n\t\t}\n\t}\n\n\treturn state;\n}\n\n/**\n * Reducer returning the command loaders\n *\n * @param {Object} state Current state.\n * @param {Object} action Dispatched action.\n *\n * @return {Object} Updated state.\n */\nfunction commandLoaders( state = {}, action ) {\n\tswitch ( action.type ) {\n\t\tcase 'REGISTER_COMMAND_LOADER':\n\t\t\treturn {\n\t\t\t\t...state,\n\t\t\t\t[ action.name ]: {\n\t\t\t\t\tname: action.name,\n\t\t\t\t\tcontext: action.context,\n\t\t\t\t\thook: action.hook,\n\t\t\t\t},\n\t\t\t};\n\t\tcase 'UNREGISTER_COMMAND_LOADER': {\n\t\t\tconst { [ action.name ]: _, ...remainingState } = state;\n\t\t\treturn remainingState;\n\t\t}\n\t}\n\n\treturn state;\n}\n\n/**\n * Reducer returning the command center open state.\n *\n * @param {Object} state Current state.\n * @param {Object} action Dispatched action.\n *\n * @return {boolean} Updated state.\n */\nfunction isOpen( state = false, action ) {\n\tswitch ( action.type ) {\n\t\tcase 'OPEN':\n\t\t\treturn true;\n\t\tcase 'CLOSE':\n\t\t\treturn false;\n\t}\n\n\treturn state;\n}\n\n/**\n * Reducer returning the command center's active context.\n *\n * @param {Object} state Current state.\n * @param {Object} action Dispatched action.\n *\n * @return {boolean} Updated state.\n */\nfunction context( state = 'root', action ) {\n\tswitch ( action.type ) {\n\t\tcase 'SET_CONTEXT':\n\t\t\treturn action.context;\n\t}\n\n\treturn state;\n}\n\nconst reducer = combineReducers( {\n\tcommands,\n\tcommandLoaders,\n\tisOpen,\n\tcontext,\n} );\n\nexport default reducer;\n"]}
|
|
@@ -2,20 +2,24 @@
|
|
|
2
2
|
* External dependencies
|
|
3
3
|
*/
|
|
4
4
|
import createSelector from 'rememo';
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
return
|
|
5
|
+
export const getCommands = createSelector(function (state) {
|
|
6
|
+
let contextual = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
|
|
7
|
+
return Object.values(state.commands).filter(command => {
|
|
8
|
+
const isContextual = command.context && command.context === state.context;
|
|
9
|
+
return contextual ? isContextual : !isContextual;
|
|
10
|
+
});
|
|
11
|
+
}, state => [state.commands, state.context]);
|
|
12
|
+
export const getCommandLoaders = createSelector(function (state) {
|
|
13
|
+
let contextual = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
|
|
14
|
+
return Object.values(state.commandLoaders).filter(loader => {
|
|
15
|
+
const isContextual = loader.context && loader.context === state.context;
|
|
16
|
+
return contextual ? isContextual : !isContextual;
|
|
17
|
+
});
|
|
18
|
+
}, state => [state.commandLoaders, state.context]);
|
|
19
|
+
export function isOpen(state) {
|
|
20
|
+
return state.isOpen;
|
|
21
|
+
}
|
|
22
|
+
export function getContext(state) {
|
|
23
|
+
return state.context;
|
|
8
24
|
}
|
|
9
|
-
|
|
10
|
-
export const getGroups = createSelector(state => unique(Object.keys(state.commands).concat(Object.keys(state.commandLoaders))), state => [state.commands, state.commandLoaders]);
|
|
11
|
-
export const getCommands = createSelector((state, group) => {
|
|
12
|
-
var _state$commands$group;
|
|
13
|
-
|
|
14
|
-
return Object.values((_state$commands$group = state.commands[group]) !== null && _state$commands$group !== void 0 ? _state$commands$group : {});
|
|
15
|
-
}, (state, group) => [state.commands[group]]);
|
|
16
|
-
export const getCommandLoaders = createSelector((state, group) => {
|
|
17
|
-
var _state$commandLoaders;
|
|
18
|
-
|
|
19
|
-
return Object.values((_state$commandLoaders = state.commandLoaders[group]) !== null && _state$commandLoaders !== void 0 ? _state$commandLoaders : {});
|
|
20
|
-
}, (state, group) => [state.commandLoaders[group]]);
|
|
21
25
|
//# sourceMappingURL=selectors.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["@wordpress/commands/src/store/selectors.js"],"names":["createSelector","
|
|
1
|
+
{"version":3,"sources":["@wordpress/commands/src/store/selectors.js"],"names":["createSelector","getCommands","state","contextual","Object","values","commands","filter","command","isContextual","context","getCommandLoaders","commandLoaders","loader","isOpen","getContext"],"mappings":"AAAA;AACA;AACA;AACA,OAAOA,cAAP,MAA2B,QAA3B;AAEA,OAAO,MAAMC,WAAW,GAAGD,cAAc,CACxC,UAAEE,KAAF;AAAA,MAASC,UAAT,uEAAsB,KAAtB;AAAA,SACCC,MAAM,CAACC,MAAP,CAAeH,KAAK,CAACI,QAArB,EAAgCC,MAAhC,CAA0CC,OAAF,IAAe;AACtD,UAAMC,YAAY,GACjBD,OAAO,CAACE,OAAR,IAAmBF,OAAO,CAACE,OAAR,KAAoBR,KAAK,CAACQ,OAD9C;AAEA,WAAOP,UAAU,GAAGM,YAAH,GAAkB,CAAEA,YAArC;AACA,GAJD,CADD;AAAA,CADwC,EAOtCP,KAAF,IAAa,CAAEA,KAAK,CAACI,QAAR,EAAkBJ,KAAK,CAACQ,OAAxB,CAP2B,CAAlC;AAUP,OAAO,MAAMC,iBAAiB,GAAGX,cAAc,CAC9C,UAAEE,KAAF;AAAA,MAASC,UAAT,uEAAsB,KAAtB;AAAA,SACCC,MAAM,CAACC,MAAP,CAAeH,KAAK,CAACU,cAArB,EAAsCL,MAAtC,CAAgDM,MAAF,IAAc;AAC3D,UAAMJ,YAAY,GACjBI,MAAM,CAACH,OAAP,IAAkBG,MAAM,CAACH,OAAP,KAAmBR,KAAK,CAACQ,OAD5C;AAEA,WAAOP,UAAU,GAAGM,YAAH,GAAkB,CAAEA,YAArC;AACA,GAJD,CADD;AAAA,CAD8C,EAO5CP,KAAF,IAAa,CAAEA,KAAK,CAACU,cAAR,EAAwBV,KAAK,CAACQ,OAA9B,CAPiC,CAAxC;AAUP,OAAO,SAASI,MAAT,CAAiBZ,KAAjB,EAAyB;AAC/B,SAAOA,KAAK,CAACY,MAAb;AACA;AAED,OAAO,SAASC,UAAT,CAAqBb,KAArB,EAA6B;AACnC,SAAOA,KAAK,CAACQ,OAAb;AACA","sourcesContent":["/**\n * External dependencies\n */\nimport createSelector from 'rememo';\n\nexport const getCommands = createSelector(\n\t( state, contextual = false ) =>\n\t\tObject.values( state.commands ).filter( ( command ) => {\n\t\t\tconst isContextual =\n\t\t\t\tcommand.context && command.context === state.context;\n\t\t\treturn contextual ? isContextual : ! isContextual;\n\t\t} ),\n\t( state ) => [ state.commands, state.context ]\n);\n\nexport const getCommandLoaders = createSelector(\n\t( state, contextual = false ) =>\n\t\tObject.values( state.commandLoaders ).filter( ( loader ) => {\n\t\t\tconst isContextual =\n\t\t\t\tloader.context && loader.context === state.context;\n\t\t\treturn contextual ? isContextual : ! isContextual;\n\t\t} ),\n\t( state ) => [ state.commandLoaders, state.context ]\n);\n\nexport function isOpen( state ) {\n\treturn state.isOpen;\n}\n\nexport function getContext( state ) {\n\treturn state.context;\n}\n"]}
|