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

Get DOM element type clicked on #608

Closed
JBotma opened this issue Oct 16, 2017 · 5 comments
Closed

Get DOM element type clicked on #608

JBotma opened this issue Oct 16, 2017 · 5 comments
Labels
resolution:expired This issue was closed due to lack of feedback. status:stale type:docs This issue reports a task related to documentation (e.g. an idea for a guide). type:question This issue asks a question (how to...).

Comments

@JBotma
Copy link

JBotma commented Oct 16, 2017

Hi! Firstly I would like to thank you for creating such amazing software. I am very excited to start using CKE5!

I need to see on what type of DOM element I clicked on. This will allow my custom interface (great feature btw) to show "delete" button if the user clicked on an image/table or show "bold/italic" buttons if the user clicked on text.

I started looking into the Observer classes, but cannot quite figure out how I can achieve this.
Any advice would be greatly appreciated?

@Reinmar Reinmar added type:docs This issue reports a task related to documentation (e.g. an idea for a guide). type:question This issue asks a question (how to...). labels Oct 16, 2017
@Reinmar
Copy link
Member

Reinmar commented Oct 16, 2017

Hey! Great question. We didn't yet finish the view docs in the architecture introduction so this bit isn't covered yet.

The way to retrieve the clicked element would be to listen to editor.editing.view#click event:

editor.editing.view.on( 'click', ( evt, data ) => {
   data.target; // -> engine.view.Element
} );

However, the ClickObserver is not automatically loaded so the features which need it need to load it:

import ClickObserver from '@ckeditor/ckeditor5-engine/src/view/observer/clickobserver';

// Somewhere in your plugin's init() callback:
editor.editing.view.addObserver( ClickObserver );

Now, the event will be fired as expected.

However, you will soon notice that this event is fired with a data containing the target property which doesn't point to a native DOM element, but to the view's element. There's no easy way to retrieve the underlying DOM element on purpose, cause that's a good practice to use the view elements as often as possible – that makes your code easier to test. However, in certain cases you may need to access the underlying DOM and for that you can use the DomConverter#mapViewToDom() method (the instance of DomConverter is available in editor.editing.view.domConverter).

PS. I'm not sure what you want to implement exactly, but checking what was clicked may not be the most adequate option. Sometimes, it's worth checking where the selection landed and, for instance, use the model. I actually don't remember how the link's feature implementation ended up, but I remember that we had to try a couple of options to find a good UX. It's, unfortunately, not something that the engine could solve for the developer.

@Reinmar Reinmar added this to the backlog milestone Feb 14, 2019
@lasfin
Copy link

lasfin commented Jun 28, 2019

@Reinmar Is it possible to listen to clicks on specific elements to get cursor position with ClickObserver? I need to track clicks near the image to change its alignment and add a paragraph near it and don't know the right way to do that.
Thanks in advance.

@pomek pomek removed this from the backlog milestone Feb 21, 2022
@PororoChan
Copy link

PororoChan commented Jun 27, 2022

a

@CKEditorBot
Copy link
Collaborator

There has been no activity on this issue for the past year. We've marked it as stale and will close it in 30 days. We understand it may be relevant, so if you're interested in the solution, leave a comment or reaction under this issue.

@CKEditorBot
Copy link
Collaborator

We've closed your issue due to inactivity over the last year. We understand that the issue may still be relevant. If so, feel free to open a new one (and link this issue to it).

@CKEditorBot CKEditorBot added the resolution:expired This issue was closed due to lack of feedback. label Nov 2, 2023
@CKEditorBot CKEditorBot closed this as not planned Won't fix, can't repro, duplicate, stale Nov 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
resolution:expired This issue was closed due to lack of feedback. status:stale type:docs This issue reports a task related to documentation (e.g. an idea for a guide). type:question This issue asks a question (how to...).
Projects
None yet
Development

No branches or pull requests

6 participants