Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Added support for middleware in edit menu #31

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
andersevenrud merged 1 commit into os-js:master from ThisIsOstad:master
Jan 17, 2021
Merged

Added support for middleware in edit menu #31

andersevenrud merged 1 commit into os-js:master from ThisIsOstad:master
Jan 17, 2021

Conversation

@ThisIsOstad
Copy link
Contributor

@ThisIsOstad ThisIsOstad commented Jan 17, 2021
edited
Loading

It adds support for the dynamic edit menu in the application. Any other application can now use the osjs/filemanager:contextmenu:edit middleware group to change context/edit menu.

Here is an example of how an application can use this feature:

  1. Create a middleware.js file at the root of your application.
  2. Change webpack config and add the middleware.js as a separate entry.
  3. Add { "filename": "middleware.js", "type": "background" } to the files property in the metadata config.
  4. Use a code like this in the created middleware.js file:
import osjs from 'osjs';
osjs.middleware('osjs/filemanager:menu:edit', (({file, isContextMenu}) => {
 if (!isContextMenu) {
 return [];
 }
 return [{
 label: 'Scan',
 disabled: !file.isFile,
 onclick: () => osjs.run('scanner', {file})
 }];
}));

Closes os-js/OS.js#752.

andersevenrud reacted with heart emoji
Copy link
Member

andersevenrud commented Jan 17, 2021
edited
Loading

I think it would be useful to have async function support here!

Ex:

const promises = editMenuMiddlewares
 .map(fn => fn({file: item, menuItems: items, isContextMenu}))
const appendItems = await Promise.all(promises)
const menuItems = [...editMenuItems, ...appendItems]

Copy link
Contributor Author

There are two problems:

First, the createEditMenu itself is not a async function. Can we make it async? How?

The second problem is that it doesn't support adding an item in a place other than the end of menu items (which is not very important for me).

Copy link
Member

To make it async, just place an async keyword here:

return ({name, ev}, args, isContextMenu = false) => {

Then await on this:

menu: menuItems[name](args, isContextMenu),

And finally async keyword here

const createEditMenu = (item, isContextMenu) => {
ThisIsOstad reacted with thumbs up emoji ThisIsOstad reacted with laugh emoji

Copy link
Contributor Author

Thanks for your help 😁

To make it possible for any application to add multiple items we need to assume that they will return an array, so appendedItems will be an array of arrays and we need a .reduce to make it a flat array. Right?

Copy link
Member

No problem!

To make it possible for any application to add multiple items we need to assume that they will return an array

Ah, yeah. These callbacks should probably only be able to return arrays.

To flatten it out simply do the following (as I'm not sure if the current babel setup does polyfills of .flatten and .flatMap)::

const promises = editMenuMiddlewares
 .map(fn => fn({file: item, menuItems: items, isContextMenu}))
const appendItems = await Promise.all(promises)
const menuItems = [...editMenuItems, ...([].concat(...appendItems))]
ThisIsOstad reacted with thumbs up emoji

Copy link
Member

Maybe also sanitize it :)

const appendItems = (await Promise.all(promises))
 .map(item => item instanceof Array)

Copy link
Contributor Author

Do you mean filter?

andersevenrud reacted with thumbs up emoji

Copy link
Member

Ops. Yeah, filter 😊

Copy link
Member

You have confirmed that the latest changes and everything works with this ? 🤓

Copy link
Contributor Author

Now it's working 😁

I'd missed an await for menuItemsFromMiddleware.

andersevenrud reacted with rocket emoji

Copy link
Member

Sweet! Then I'll merge and release this one as well :)

ThisIsOstad reacted with hooray emoji ThisIsOstad reacted with heart emoji

@andersevenrud andersevenrud merged commit 79ad5bf into os-js:master Jan 17, 2021
Copy link
Member

@osjs/filemanager-application@1.5.0 is now published with these changes.

Again, thank you very much for this!

ThisIsOstad reacted with hooray emoji

Copy link
Contributor Author

I really enjoyed it, thank you too 😊

andersevenrud reacted with heart emoji

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@andersevenrud andersevenrud andersevenrud approved these changes

Labels

enhancement New feature or request

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Adding context menu to file manager according to packages installed on OSjs.

AltStyle によって変換されたページ (->オリジナル) /