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

Remove event listeners #6

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions sample/index.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="utf-8">
<title>CKEditor 5 Mermaid widget – Development Sample</title>
Expand All @@ -10,13 +11,15 @@
}
</style>
</head>

<body>

<h1>CKEditor 5 Mermaid widget – Development Sample</h1>
<h1>CKEditor 5 Mermaid widget – Development Sample</h1>

<div id="editor">
</div>
<div id="editor">
</div>

<script src="./ckeditor.dist.js"></script>
<script src="./ckeditor.dist.js"></script>
</body>

</html>
24 changes: 16 additions & 8 deletions src/mermaidediting.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,19 +174,27 @@ export default class MermaidEditing extends Plugin {
domElement.addEventListener( 'input', debouncedListener );

/* Workaround for internal #1544 */
domElement.addEventListener( 'focus', () => {
const model = editor.model;
const selectedElement = model.document.selection.getSelectedElement();
domElement.addEventListener( 'focus', focusListener, true );

// Move the selection onto the mermaid widget if it's currently not selected.
if ( selectedElement !== data.item ) {
model.change( writer => writer.setSelection( data.item, 'on' ) );
}
}, true );
// Remove the focus and input listener on editor#destroy.
editor.on( 'destroy', () => {
domElement.removeEventListener( 'input', debouncedListener );
domElement.removeEventListener( 'focus', focusListener );
} );

return domElement;
}

function focusListener( ) {
const model = editor.model;
const selectedElement = model.document.selection.getSelectedElement();

// Move the selection onto the mermaid widget if it's currently not selected.
if ( selectedElement !== data.item ) {
model.change( writer => writer.setSelection( data.item, 'on' ) );
}
}

function createMermaidPreview( domDocument ) {
// Taking the text from the wrapper container element for now
const mermaidSource = data.item.getAttribute( 'source' );
Expand Down