From deb3cf608e59a48db746abb0d5c7fde961253fb7 Mon Sep 17 00:00:00 2001 From: James J Balamuta Date: Wed, 13 Sep 2023 17:46:39 -0700 Subject: [PATCH] Monaco Editor Update (#29) * Ignore a few files that are generated from tests. * Update the MonacoEditor to v0.43.0 from v0.31.0 * Fix WebR chunks not working with Safari browser #21 by re-arranging the load steps for MonacoEditor The `loader.js` and `require.config()` initialization is set to happen at the top of the document's instead of the . * Fix #28 by re-registering keyboard shortcuts. * Bump extension version to 0.3.2 --- .gitignore | 4 +++ _extensions/webr/_extension.yml | 2 +- _extensions/webr/monaco-editor-init.html | 10 ++++++ _extensions/webr/webr-editor.html | 40 +++++++++++++++--------- _extensions/webr/webr-init.html | 13 ++------ _extensions/webr/webr.lua | 3 ++ 6 files changed, 47 insertions(+), 25 deletions(-) create mode 100644 _extensions/webr/monaco-editor-init.html diff --git a/.gitignore b/.gitignore index 8db40be2..9a66cbf2 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,5 @@ /.luarc.json +*_files +webr-worker.js +webr-serviceworker.js +webr-*.html diff --git a/_extensions/webr/_extension.yml b/_extensions/webr/_extension.yml index 168939c4..e4174f25 100644 --- a/_extensions/webr/_extension.yml +++ b/_extensions/webr/_extension.yml @@ -1,7 +1,7 @@ name: webr title: Embedded webr code cells author: James Joseph Balamuta -version: 0.3.1 +version: 0.3.2 quarto-required: ">=1.2.198" contributes: filters: diff --git a/_extensions/webr/monaco-editor-init.html b/_extensions/webr/monaco-editor-init.html new file mode 100644 index 00000000..84746ac6 --- /dev/null +++ b/_extensions/webr/monaco-editor-init.html @@ -0,0 +1,10 @@ + + \ No newline at end of file diff --git a/_extensions/webr/webr-editor.html b/_extensions/webr/webr-editor.html index 506240c8..3f741ab7 100644 --- a/_extensions/webr/webr-editor.html +++ b/_extensions/webr/webr-editor.html @@ -30,20 +30,6 @@ hideCursorInOverviewRuler: true // Remove cursor indictor in right hand side scroll bar }); - // Add a keydown event listener for Shift+Enter using the addCommand method - editor.addCommand(monaco.KeyMod.Shift | monaco.KeyCode.Enter, function () { - // Code to run when Shift+Enter is pressed - executeCode(editor.getValue()); - }); - - // Add a keydown event listener for Ctrl+Enter to run selected code - editor.addCommand(monaco.KeyMod.CtrlCmd | monaco.KeyCode.Enter, function () { - // Get the selected text from the editor - const selectedText = editor.getModel().getValueInRange(editor.getSelection()); - // Code to run when Ctrl+Enter is pressed (run selected code) - executeCode(selectedText); - }); - // Dynamically modify the height of the editor window if new lines are added. let ignoreEvent = false; const updateHeight = () => { @@ -61,6 +47,32 @@ } }; + // Registry of keyboard shortcuts that should be re-added to each editor window + // when focus changes. + const addWebRKeyboardShortCutCommands = () => { + // Add a keydown event listener for Shift+Enter to run all code in cell + editor.addCommand(monaco.KeyMod.Shift | monaco.KeyCode.Enter, () => { + + // Retrieve all text inside the editor + executeCode(editor.getValue()); + }); + + // Add a keydown event listener for CMD/Ctrl+Enter to run selected code + editor.addCommand(monaco.KeyMod.CtrlCmd | monaco.KeyCode.Enter, () => { + // Get the selected text from the editor + const selectedText = editor.getModel().getValueInRange(editor.getSelection()); + // Code to run when Ctrl+Enter is pressed (run selected code) + executeCode(selectedText); + }); + } + + // Register an on focus event handler for when a code cell is selected to update + // what keyboard shortcut commands should work. + // This is a workaround to fix a regression that happened with multiple + // editor windows since Monaco 0.32.0 + // https://github.com/microsoft/monaco-editor/issues/2947 + editor.onDidFocusEditorText(addWebRKeyboardShortCutCommands); + // Register an on change event for when new code is added to the editor window editor.onDidContentSizeChange(updateHeight); diff --git a/_extensions/webr/webr-init.html b/_extensions/webr/webr-init.html index 8bebb656..487f9327 100644 --- a/_extensions/webr/webr-init.html +++ b/_extensions/webr/webr-init.html @@ -1,4 +1,5 @@ - + + - -