editor context menu, focus fix

This commit is contained in:
Jan Prochazka
2021-03-11 07:53:37 +01:00
parent 5f97f7d922
commit c193955fbe
6 changed files with 91 additions and 12 deletions

View File

@@ -15,6 +15,9 @@
import 'ace-builds/src-noconflict/theme-twilight';
import 'ace-builds/src-noconflict/ext-searchbox';
import 'ace-builds/src-noconflict/ext-language_tools';
import { currentDropDownMenu } from '../stores';
import _ from 'lodash';
import { handleCommandKeyDown } from '../commands/CommandListener.svelte';
const EDITOR_ID = `svelte-ace-editor-div:${Math.floor(Math.random() * 10000000000)}`;
const dispatch = createEventDispatcher<{
@@ -40,6 +43,7 @@
export let mode: string = 'text'; // String
export let theme: string = 'github'; // String
export let options: any = {}; // Object
export let menu;
let editor: ace.Editor;
let contentBackup: string = '';
@@ -54,13 +58,6 @@
const requireEditorPlugins = () => {};
requireEditorPlugins();
onDestroy(() => {
if (editor) {
editor.destroy();
editor.container.remove();
}
});
$: watchValue(value);
function watchValue(val: string) {
if (contentBackup !== val && editor && typeof val === 'string') {
@@ -101,6 +98,17 @@
resizeOnNextTick();
}
const handleContextMenu = e => {
e.preventDefault();
const left = e.pageX;
const top = e.pageY;
currentDropDownMenu.set({ left, top, items: _.isFunction(menu) ? menu() : menu });
};
const handleKeyDown = (data, hash, keyString, keyCode, event) => {
handleCommandKeyDown(event);
};
onMount(() => {
editor = ace.edit(EDITOR_ID);
@@ -115,6 +123,18 @@
if (options) {
editor.setOptions(options);
}
editor.container.addEventListener('contextmenu', handleContextMenu);
editor.keyBinding.addKeyboardHandler(handleKeyDown);
});
onDestroy(() => {
if (editor) {
editor.container.removeEventListener('contextmenu', handleContextMenu);
editor.keyBinding.removeKeyboardHandler(handleKeyDown);
editor.destroy();
editor.container.remove();
}
});
function setEventCallBacks() {