Skip to content

Conversation

@ThisIsOstad
Copy link
Contributor

@ThisIsOstad ThisIsOstad commented Jan 17, 2021

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 andersevenrud self-assigned this Jan 17, 2021
@andersevenrud andersevenrud added the enhancement New feature or request label Jan 17, 2021
@andersevenrud andersevenrud self-requested a review January 17, 2021 18:49
@andersevenrud
Copy link
Member

andersevenrud commented Jan 17, 2021

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]

@ThisIsOstad
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).

@andersevenrud
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
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?

@andersevenrud
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))]

@andersevenrud
Copy link
Member

Maybe also sanitize it :)

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

@ThisIsOstad
Copy link
Contributor Author

Do you mean filter?

@andersevenrud
Copy link
Member

Ops. Yeah, filter 😊

@andersevenrud
Copy link
Member

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

@ThisIsOstad
Copy link
Contributor Author

Now it's working 😁

I'd missed an await for menuItemsFromMiddleware.

@andersevenrud
Copy link
Member

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

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

@osjs/[email protected] is now published with these changes.

Again, thank you very much for this!

@ThisIsOstad
Copy link
Contributor Author

I really enjoyed it, thank you too 😊

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

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

2 participants