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

Backup: copy-paste support with refactor of java side events handling #231

Closed
wants to merge 9 commits into from
Closed
2 changes: 1 addition & 1 deletion .run/Build java.run.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="Build java" type="ShConfigurationType">
<option name="SCRIPT_TEXT" value="npm run build-full-pretty" />
<option name="SCRIPT_TEXT" value="npm run build-full-pretty -- --verbose" />
<option name="INDEPENDENT_SCRIPT_PATH" value="true" />
<option name="SCRIPT_PATH" value="" />
<option name="SCRIPT_OPTIONS" value="" />
Expand Down
11 changes: 3 additions & 8 deletions lib/canvas_editor/clipboard_handler.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
'use strict';

class ClipboardHandler {
copyMolecule(molecule) {
const data = molecule.getIDCodeAndCoordinates();
navigator.clipboard.writeText(`${data.idCode} ${data.coordinates}`);
}

pasteMolecule() {
// TODO: find a way to implement this in a synchronous way.
return null;
putString(data) {
void navigator.clipboard.writeText(data);
return true;
}
}

Expand Down
15 changes: 14 additions & 1 deletion lib/canvas_editor/create_editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@

const EditorArea = require('./editor_area');
const getEditorStylesheet = require('./editor_stylesheet');
const { addPointerListeners, addKeyboardListeners } = require('./events');
const {
addPointerListeners,
addKeyboardListeners,
addClipboardListeners,
} = require('./events');
const Toolbar = require('./toolbar');
const UIHelper = require('./ui_helper');

Expand Down Expand Up @@ -72,6 +76,7 @@ function createEditor(
new EditorArea(editorCanvas, onChange),
uiHelper,
);

if (initialFragment) {
if (initialMode === 'molecule') {
const fragmentMolecule = new Molecule(0, 0);
Expand Down Expand Up @@ -125,12 +130,20 @@ function createEditor(
);
}

const removeClipboardListeners = addClipboardListeners(
rootElement,
editorArea,
readOnly,
JavaEditorArea,
);

function destroy() {
rootElement.remove();
resizeObserver.disconnect();
removePointerListeners?.();
removeKeyboardListeners?.();
removeToolbarPointerListeners?.();
removeClipboardListeners?.();
}

return { editorArea, toolbar, uiHelper, destroy };
Expand Down
58 changes: 55 additions & 3 deletions lib/canvas_editor/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

function fireMouseEvent(what, ev, clickCount = 0) {
if (ev.button > 0) {
// TODO: remove this to implement popup menu.

Check warning on line 14 in lib/canvas_editor/events.js

View workflow job for this annotation

GitHub Actions / lint

Unexpected 'todo' comment: 'TODO: remove this to implement popup...'
return;
}
drawArea.fireMouseEvent(
Expand Down Expand Up @@ -70,23 +70,27 @@
function addKeyboardListeners(canvasElement, editorArea, JavaEditorArea) {
const isMac =
typeof navigator !== 'undefined' && navigator.platform === 'MacIntel';
const isMenuKey = (ev) => (isMac && ev.metaKey) || (!isMac && ev.ctrlKey);

function fireKeyEvent(what, ev) {
const key = getKeyFromEvent(ev, JavaEditorArea);
if (key === null) return;
ev.stopPropagation();
ev.preventDefault();
Comment on lines -77 to -78
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed so browser can propagate copy-paste events.
To interact with clipboard, browser need proper user interaction


editorArea.fireKeyEvent(
what,
key,
ev.altKey,
ev.ctrlKey,
ev.shiftKey,
(isMac && ev.metaKey) || (!isMac && ev.ctrlKey),
isMenuKey(ev),
);
}

canvasElement.addEventListener('keydown', (ev) => {
// copy-paste is handled by the clipboard event
if (isMenuKey(ev) && ev.key === 'c') return;
if (isMenuKey(ev) && ev.key === 'v') return;
Comment on lines +90 to +92
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not triggering java events withfireKeyEvent in this case, avoid double copy-paste events.
And events triggered from keyboard can't work. Need clipboard events


fireKeyEvent(JavaEditorArea.KEY_EVENT_PRESSED, ev);
});
canvasElement.addEventListener('keyup', (ev) => {
Expand Down Expand Up @@ -129,7 +133,55 @@
}
}

/**
*
* @param {HTMLDivElement} rootElement
* @param editorArea
* @param {boolean} readOnly
* @param JavaEditorArea
*/
function addClipboardListeners(
rootElement,
editorArea,
readOnly,
JavaEditorArea,
) {
/**
* @param {ClipboardEvent} event
*/
function onCopy(event) {
editorArea.fireClipboardEvent(JavaEditorArea.CLIPBOARD_EVENT_COPY, null);

event.preventDefault();
event.stopPropagation();
}

/**
* @param {ClipboardEvent} event
*/
function onPaste(event) {
const idcode = event.clipboardData.getData('text/plain');
if (!idcode) return;

editorArea.fireClipboardEvent(JavaEditorArea.CLIPBOARD_EVENT_PASTE, idcode);

event.preventDefault();
event.stopPropagation();
}

rootElement.addEventListener('copy', onCopy);
if (!readOnly) {
rootElement.addEventListener('paste', onPaste);
}

return () => {
rootElement.removeEventListener('copy', onCopy);
rootElement.removeEventListener('paste', onPaste);
};
}

module.exports = {
addPointerListeners,
addKeyboardListeners,
addClipboardListeners,
};
Loading
Loading