Skip to content
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

Invalid handlers are still listed in OC.Viewer.availableHandlers #912

Closed
azul opened this issue May 25, 2021 · 15 comments · Fixed by #2115
Closed

Invalid handlers are still listed in OC.Viewer.availableHandlers #912

azul opened this issue May 25, 2021 · 15 comments · Fixed by #2115
Labels
1. to develop Accepted and waiting to be taken care of bug Something isn't working good first issue Good for newcomers

Comments

@azul
Copy link
Contributor

azul commented May 25, 2021

Describe the bug
OCA.Viewer.registerHandler({}) will add a handler without id or mimes to OCA.Viewer.availableHandlers.

To Reproduce
Steps to reproduce the behavior:

  1. Go to some page that loads the viewer js.
  2. Go to the console
OCA.Viewer.availableHandlers.length
// 3
OCA.Viewer.registerHandler({})
// err: Please do NOT wait for the DOMContentLoaded before registering your viewer handler
// err: The following handler doesn't have a valid id
OCA.Viewer.availableHandlers.length
// 4

Expected behavior
Invalid handlers should not be added to the available handlers.

Desktop (please complete the following information):

  • OS: Linux
  • Browser Chromium
  • Version 20

Additional context
We're using the availableHandlers in the Collectives app to open markdown files in a viewer component. This is hacky and probably not the intended way of using this API. However adding an invalid handler to the list of availableHandlers still seems like an error.

@azul azul added bug Something isn't working 0. Needs triage Pending approval or rejection. This issue is pending approval. labels May 25, 2021
@skjnldsv
Copy link
Member

Oh right, it should not!

@skjnldsv skjnldsv added 1. to develop Accepted and waiting to be taken care of good first issue Good for newcomers and removed 0. Needs triage Pending approval or rejection. This issue is pending approval. labels May 25, 2021
@skjnldsv
Copy link
Member

skjnldsv commented May 25, 2021

Weird, because we actually return

viewer/src/views/Viewer.vue

Lines 525 to 529 in 267797a

// checking valid handler id
if (!handler.id || handler.id.trim() === '' || typeof handler.id !== 'string') {
console.error('The following handler doesn\'t have a valid id', handler)
return
}

EDIT:
Aaah, we should indeed make sure the check is in the Viewer service instead.

registerHandler(handler) {
this._state.handlers.push(handler)
this._mimetypes.push.apply(this._mimetypes, handler.mimes)
}

@skjnldsv
Copy link
Member

We're using the availableHandlers in the Collectives app to open markdown files in a viewer component. This is hacky and probably not the intended way of using this API. However adding an invalid handler to the list of availableHandlers still seems like an error.

Please link me some code?

@azul
Copy link
Contributor Author

azul commented May 28, 2021

Thanks for the quick response @skjnldsv 😃

We fixed the issue on our end by coming up with a more robust way of selecting the text app handler:

handler() {
	return OCA.Viewer.availableHandlers.find(h => h.id === 'text')
},

Before we were checking h.mimes.contains('text/markdown') - which caused an error when h.mimes was not set.

This issue is mainly meant to track the unexpected behavior we were seeing.

@skjnldsv
Copy link
Member

skjnldsv commented May 28, 2021

Please don't do that.
this is not why this API has been designed, if you want a proper implementation of the handlers, please focus on implementing #644 with a proper API to render viewer without the Modal.

I cannot guarantee the stability of your code nor can inform you when it will break on your app.
I'm really advocating against hacks like those. availableHandlers should really not have been made available publicly.
I will most likely remove it in the future when I'll move the registerHandler method inside the Viewer service, meaning we would not need the availableHandlers to be publicly exposed.

@azul
Copy link
Contributor Author

azul commented May 28, 2021

@skjnldsv What would that proper API look like?
The first thing that comes to my mind would be

OCA.Viewer.open(path, {modal: false})

But without the modal one would need to specify where to render the viewer. So maybe something like

OCA.Viewer.open(path, {el: containerElement})

But i still don't understand how that would work. Right now the ViewerComponent is rendered in the render function of the Vue instance that is attached to the #viewer element.
For the second API to work we'd need a separate Vue instance that would be attached to a different element and only render one handlers component, right?

I suspect this Vue instance would have to be created inside the open function and then attached to the specified element.
I guess it would make sense to do this in a separate API function such as display.

OCA.Viewer.display(path, {el: containerElement})

Does that sound like what you had in mind?

@skjnldsv
Copy link
Member

OCA.Viewer.open(path, {el: containerElement})

This looks rather nice :)
Btw, the direct call of open(path) is deprecated and removed for 22 (let me do a pr now) Use the restructuring object calling. OCA.Viewer.open({ path, el: containerElement })`

@skjnldsv
Copy link
Member

But i still don't understand how that would work. Right now the ViewerComponent is rendered in the render function of the Vue instance that is attached to the #viewer element.

That would require a slightly different logic.
But if we specify this, it should disable other features like pagination, loadMore, etc etc.
So it's easy to have a dedicated script that mounts the component directly in Viewer.vue

Just have a proper Vue.extend of the currentFile.modal for example 🤷
We do that in other locations already like: https://github.com/nextcloud/nextcloud-vue/blob/a553c341029aedf9fef2437e471e471a5da57e75/src/components/Modal/Modal.vue#L384-L390

@SKB-CGN
Copy link

SKB-CGN commented Nov 24, 2021

Hi,
i still have the error with NC 22.2.3

2021-11-24 10_52_41-Dashboard - Kreyenborg koeln - Cloud

@skjnldsv
Copy link
Member

@StefCGN should probably be a warning for the first two.
Can you expand to know which app is loading those?

@SKB-CGN
Copy link

SKB-CGN commented Nov 29, 2021

@skjnldsv Please have a look here.
2021-11-29 13_38_20-Dashboard - Kreyenborg koeln - Cloud

2021-11-29 13_35_58-Dateien - Kreyenborg koeln - Cloud

2021-11-29 13_36_12-Dateien - Kreyenborg koeln - Cloud

2021-11-29 13_36_20-Dateien - Kreyenborg koeln - Cloud

@skjnldsv
Copy link
Member

For the DOMContentLoaded one: #1079

@SKB-CGN
Copy link

SKB-CGN commented Nov 29, 2021

Means? You are working on?

@azul
Copy link
Contributor Author

azul commented Feb 17, 2022

@max-nextcloud Could you up on the new viewer API proposal to render the viewer without the modal in a given element? That would clean up the handling significantly. No more need to inspect the availableHandlers.

@max-nextcloud
Copy link
Contributor

Long time no see ;)

In the meantime:

  • collectives started using an API exposed by the text app rather than relying on viewer.
  • 69668aa removed the DOMContentLoaded warning in the end.
  • Most of the other script loading has been updated with the vue rework.

But the initial report in this issue still holds true:
grafik

So I'll prepare a PR that fixes it as proposed in #912 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
1. to develop Accepted and waiting to be taken care of bug Something isn't working good first issue Good for newcomers
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants