os-js / osjs-client

OS.js Client Module
https://manual.os-js.org/
Other
31 stars 31 forks source link

Accessing options in save-file event #156

Closed mahsashadi closed 3 years ago

mahsashadi commented 3 years ago

Hi, How can I access fileType parameter in save-file event in below code?

function handleCreateFile(fileType) {
    basic.createSaveDialog();
}

useEffect(() => {
  basic.on('save-file', selectedData => {
    // I need my fileType here
  });
}, []);
andersevenrud commented 3 years ago

A VFS file object contains isDirectory. Can't you use that ?

mahsashadi commented 3 years ago

Indeed I just pass an extension to handleCreateFile function (odt, odp,...), and then I want It to open save as dialog to create the file with my extension, with path and name set in SaveDialog

mahsashadi commented 3 years ago

To clarify more, the project is about making new file for office. I have different buttons for making new word/ powerpoint/ excel files, which all calls handleCreateFile function in their onclick event.

andersevenrud commented 3 years ago

You could probably create a proxy type event handler for this:

function handleCreateFile(fileType) {
  basic.off('save-file') // Just to ensure no duplicate events
  basic.once('save-file', result => basic.emit('my-save-file', result, fileType)
  basic.createSaveDialog()
}

useEffect(() => {
  basic.on('my-save-file', (file, fileType) => {
  })
}, [])
mahsashadi commented 3 years ago

Thanks.

And how can I filter the files on saveDialog to only show files with my fileType?

andersevenrud commented 3 years ago

What's the value of a fileType ?

mahsashadi commented 3 years ago

It can be an array of strings

For example [“docx”, “odt”] for openjng new file in Office Word, and [“ppt”, “odp”] for Office Powerpoint

andersevenrud commented 3 years ago

Currently it's only possible to filter with the mime option, so you'd have to map your fileType to MIME(s).

mahsashadi commented 3 years ago

By mime, you mean I should define basic like below to work as I need?

  const myBasic = core.make('osjs/basic-application', proc, win, {
    mimeTypes: fileType   //['docx', 'odt', 'txt']
  })
andersevenrud commented 3 years ago

No. MIMEs are formatted like this foo/bar.

Ex a docx file extension is usually the application/vnd.openxmlformats-officedocument.wordprocessingml.document MIME type.

mahsashadi commented 3 years ago

So I did not understand how and where I can use them in savedialog senario!

andersevenrud commented 3 years ago

The basic application usually takes the mime definitions from the metadata file, but you can define these yourself like you demonstrated:

const fileTypes = {
  docx: ['application/vnd.openxmlformats-officedocument.wordprocessingml.document']
}

const mimeTypes = Object.values(fileTypes).flat()
const myBasic = core.make('osjs/basic-application', proc, win, {
  mimeTypes
})

But since you mention that you want to have different behavior based on what button is clicked, then you probably wanna override the basic application mime types in your dialog. The mimeTypes parameter for the basic application is designed to make the dialog accept multiple different MIME types, i.e.text/plain+text/html+application/vnd.openxmlformats-officedocument.wordprocessingml.document files, not separately. I hope that makes sense...

const fileTypes = {
  docx: ['application/vnd.openxmlformats-officedocument.wordprocessingml.document']
}

function something(fileType) {
  const mimeTypes = fileTypes[fileType] || []
  basic.createSaveDialog({
    mime: mimeTypes
  })
}

So if you need to go for an approach like above here, then you can probably just drop using basic application, because all that it really does is just to handle the session and dialogs anyway.

mahsashadi commented 3 years ago

Thank a lot for your great explanation :+1 I think the code above works, I will examine more tomorrow.

Just it has problem with one of my drives (loaded by my own adapter) which only has directories (no file) at first level, none of directories are shown.

So if you need to go for an approach like above here, then you can probably just drop using basic application, because all that it really does is just to handle the session and dialogs anyway.

Since I need a dialog with save-as dialog options I use basic, so why it is better not to use basic in this case?!

andersevenrud commented 3 years ago

Since I need a dialog with save-as dialog options I use basic, so why it is better not to use basic in this case?!

Sure, you can just keep using this if it works for you. I just think that maybe once you start to override functionality, maybe it would be best to write it yourself -- if you look at the basic-application source you'll see that there isn't that much going on :) But don't fix what ain't broke, I suppose.

mahsashadi commented 3 years ago

maybe it would be best to write it yourself

you are right, I will rewrite this code part again later, thanks a lot.

Just another question , I use defaultFilename in below code:

const newBasic = core.make('osjs/basic-application', proc, win, {
        defaultFilename: ' '
    })

But from the second time later that I opened it in my app, I have previous used filename. How can I always have empty filename?

andersevenrud commented 3 years ago

Hm. Sounds like a bug. Can you show screenshots on how to reproduce ?

andersevenrud commented 3 years ago

Sounds like a bug

Oh, or maybe not. I'm not 100% sure what you're refering to.

The default behavior of the "basic application" is to store the last used file into the session. So maybe this is what you're getting ?

You can probably override that by doing proc.args.file = undefined before basic.init().

mahsashadi commented 3 years ago

BTW, there is a bug that when I close the save-as dialog by close button (not by cancel button), if the filename textbox has some value, the confirm dialog for overwriting is triggered! For example It happenes in your textpad application.

andersevenrud commented 3 years ago

Could you please file an issue about it here https://github.com/os-js/osjs-dialogs/issues ? :)

mahsashadi commented 3 years ago

Could you please file an issue about it here https://github.com/os-js/osjs-dialogs/issues ? :)

Sure. It issued here.

andersevenrud commented 3 years ago

Please close this issue if you have solved what was asked in OP