diff --git a/.nojekyll b/.nojekyll index 99f3895..7c0fc4f 100644 --- a/.nojekyll +++ b/.nojekyll @@ -1 +1 @@ -9207d802 \ No newline at end of file +da7f2af9 \ No newline at end of file diff --git a/documents/pdf/index.pdf b/documents/pdf/index.pdf index 8f1aac4..5756909 100644 Binary files a/documents/pdf/index.pdf and b/documents/pdf/index.pdf differ diff --git a/documents/webr/index.html b/documents/webr/index.html index 11cdf62..370948a 100644 --- a/documents/webr/index.html +++ b/documents/webr/index.html @@ -93,7 +93,8 @@ "search-label": "Search" } } - + + - - + @@ -293,20 +340,6 @@

{ + // 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()); + // Check if no code is selected + if (isEmptyCodeText(selectedText)) { + // Obtain the current cursor position + let currentPosition = editor.getPosition(); + // Retrieve the current line content + let currentLine = editor.getModel().getLineContent(currentPosition.lineNumber); + + // Propose a new position to move the cursor to + let newPosition = new monaco.Position(currentPosition.lineNumber + 1, 1); + + // Check if the new position is beyond the last line of the editor + if (newPosition.lineNumber > editor.getModel().getLineCount()) { + // Add a new line at the end of the editor + editor.executeEdits("addNewLine", [{ + range: new monaco.Range(newPosition.lineNumber, 1, newPosition.lineNumber, 1), + text: "\n", + forceMoveMarkers: true, + }]); + } + + // Run the entire line of code. + executeCode(currentLine); + + // Move cursor to new position + editor.setPosition(newPosition); + } else { + // Code to run when Ctrl+Enter is pressed with 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); @@ -342,15 +434,15 @@

{ + // 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()); + // Check if no code is selected + if (isEmptyCodeText(selectedText)) { + // Obtain the current cursor position + let currentPosition = editor.getPosition(); + // Retrieve the current line content + let currentLine = editor.getModel().getLineContent(currentPosition.lineNumber); + + // Propose a new position to move the cursor to + let newPosition = new monaco.Position(currentPosition.lineNumber + 1, 1); + + // Check if the new position is beyond the last line of the editor + if (newPosition.lineNumber > editor.getModel().getLineCount()) { + // Add a new line at the end of the editor + editor.executeEdits("addNewLine", [{ + range: new monaco.Range(newPosition.lineNumber, 1, newPosition.lineNumber, 1), + text: "\n", + forceMoveMarkers: true, + }]); + } + + // Run the entire line of code. + executeCode(currentLine); + + // Move cursor to new position + editor.setPosition(newPosition); + } else { + // Code to run when Ctrl+Enter is pressed with 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); @@ -503,15 +644,15 @@

{ + // 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()); + // Check if no code is selected + if (isEmptyCodeText(selectedText)) { + // Obtain the current cursor position + let currentPosition = editor.getPosition(); + // Retrieve the current line content + let currentLine = editor.getModel().getLineContent(currentPosition.lineNumber); + + // Propose a new position to move the cursor to + let newPosition = new monaco.Position(currentPosition.lineNumber + 1, 1); + + // Check if the new position is beyond the last line of the editor + if (newPosition.lineNumber > editor.getModel().getLineCount()) { + // Add a new line at the end of the editor + editor.executeEdits("addNewLine", [{ + range: new monaco.Range(newPosition.lineNumber, 1, newPosition.lineNumber, 1), + text: "\n", + forceMoveMarkers: true, + }]); + } + + // Run the entire line of code. + executeCode(currentLine); + + // Move cursor to new position + editor.setPosition(newPosition); + } else { + // Code to run when Ctrl+Enter is pressed with 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); @@ -661,15 +851,15 @@

{ @@ -807,6 +987,65 @@

{ + // 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()); + // Check if no code is selected + if (isEmptyCodeText(selectedText)) { + // Obtain the current cursor position + let currentPosition = editor.getPosition(); + // Retrieve the current line content + let currentLine = editor.getModel().getLineContent(currentPosition.lineNumber); + + // Propose a new position to move the cursor to + let newPosition = new monaco.Position(currentPosition.lineNumber + 1, 1); + + // Check if the new position is beyond the last line of the editor + if (newPosition.lineNumber > editor.getModel().getLineCount()) { + // Add a new line at the end of the editor + editor.executeEdits("addNewLine", [{ + range: new monaco.Range(newPosition.lineNumber, 1, newPosition.lineNumber, 1), + text: "\n", + forceMoveMarkers: true, + }]); + } + + // Run the entire line of code. + executeCode(currentLine); + + // Move cursor to new position + editor.setPosition(newPosition); + } else { + // Code to run when Ctrl+Enter is pressed with 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); @@ -825,15 +1064,15 @@

{ - if (msg.type === "canvasExec") { - if (!canvas) { + // Determine if old canvas can be used or a new canvas is required. + if (msg.type === 'canvas'){ + // Add image to the current canvas + if (msg.data.event === 'canvasImage') { + canvas.getContext('2d').drawImage(msg.data.image, 0, 0); + } else if (msg.data.event === 'canvasNewPage') { + // Generate a new canvas element canvas = document.createElement("canvas"); canvas.setAttribute("width", 2 * 504); canvas.setAttribute("height", 2 * 360); @@ -861,7 +1105,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 = () => { @@ -968,6 +1197,65 @@

< } }; + // Helper function to check if selected text is empty + function isEmptyCodeText(selectedCodeText) { + return (selectedCodeText === null || selectedCodeText === undefined || selectedCodeText === ""); + } + + // 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()); + // Check if no code is selected + if (isEmptyCodeText(selectedText)) { + // Obtain the current cursor position + let currentPosition = editor.getPosition(); + // Retrieve the current line content + let currentLine = editor.getModel().getLineContent(currentPosition.lineNumber); + + // Propose a new position to move the cursor to + let newPosition = new monaco.Position(currentPosition.lineNumber + 1, 1); + + // Check if the new position is beyond the last line of the editor + if (newPosition.lineNumber > editor.getModel().getLineCount()) { + // Add a new line at the end of the editor + editor.executeEdits("addNewLine", [{ + range: new monaco.Range(newPosition.lineNumber, 1, newPosition.lineNumber, 1), + text: "\n", + forceMoveMarkers: true, + }]); + } + + // Run the entire line of code. + executeCode(currentLine); + + // Move cursor to new position + editor.setPosition(newPosition); + } else { + // Code to run when Ctrl+Enter is pressed with 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); @@ -986,15 +1274,15 @@

< // Initialize webR await globalThis.webR.init(); - // Setup a webR canvas - await webR.evalRVoid("canvas(width=504, height=360)"); + // Setup a webR canvas by making a namespace call into the {webr} package + await webR.evalRVoid("webr::canvas(width=504, height=360)"); // Capture output data from evaluating the code const result = await webRCodeShelter.captureR(codeToRun, { withAutoprint: true, captureStreams: true, - captureConditions: false, - env: webR.objs.emptyEnv, + captureConditions: false//, + // env: webR.objs.emptyEnv, // maintain a global environment for webR v0.2.0 }); // Start attempting to parse the result data @@ -1013,8 +1301,13 @@

< // Output each image stored msgs.forEach(msg => { - if (msg.type === "canvasExec") { - if (!canvas) { + // Determine if old canvas can be used or a new canvas is required. + if (msg.type === 'canvas'){ + // Add image to the current canvas + if (msg.data.event === 'canvasImage') { + canvas.getContext('2d').drawImage(msg.data.image, 0, 0); + } else if (msg.data.event === 'canvasNewPage') { + // Generate a new canvas element canvas = document.createElement("canvas"); canvas.setAttribute("width", 2 * 504); canvas.setAttribute("height", 2 * 360); @@ -1022,7 +1315,6 @@

< canvas.style.display = "block"; canvas.style.margin = "auto"; } - Function(`this.getContext("2d").${msg.data}`).bind(canvas)(); } }); @@ -1099,20 +1391,6 @@

{ @@ -1130,6 +1408,65 @@

{ + // 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()); + // Check if no code is selected + if (isEmptyCodeText(selectedText)) { + // Obtain the current cursor position + let currentPosition = editor.getPosition(); + // Retrieve the current line content + let currentLine = editor.getModel().getLineContent(currentPosition.lineNumber); + + // Propose a new position to move the cursor to + let newPosition = new monaco.Position(currentPosition.lineNumber + 1, 1); + + // Check if the new position is beyond the last line of the editor + if (newPosition.lineNumber > editor.getModel().getLineCount()) { + // Add a new line at the end of the editor + editor.executeEdits("addNewLine", [{ + range: new monaco.Range(newPosition.lineNumber, 1, newPosition.lineNumber, 1), + text: "\n", + forceMoveMarkers: true, + }]); + } + + // Run the entire line of code. + executeCode(currentLine); + + // Move cursor to new position + editor.setPosition(newPosition); + } else { + // Code to run when Ctrl+Enter is pressed with 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); @@ -1148,15 +1485,15 @@

{ - if (msg.type === "canvasExec") { - if (!canvas) { + // Determine if old canvas can be used or a new canvas is required. + if (msg.type === 'canvas'){ + // Add image to the current canvas + if (msg.data.event === 'canvasImage') { + canvas.getContext('2d').drawImage(msg.data.image, 0, 0); + } else if (msg.data.event === 'canvasNewPage') { + // Generate a new canvas element canvas = document.createElement("canvas"); canvas.setAttribute("width", 2 * 504); canvas.setAttribute("height", 2 * 360); @@ -1184,7 +1526,6 @@

{ + // 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()); + // Check if no code is selected + if (isEmptyCodeText(selectedText)) { + // Obtain the current cursor position + let currentPosition = editor.getPosition(); + // Retrieve the current line content + let currentLine = editor.getModel().getLineContent(currentPosition.lineNumber); + + // Propose a new position to move the cursor to + let newPosition = new monaco.Position(currentPosition.lineNumber + 1, 1); + + // Check if the new position is beyond the last line of the editor + if (newPosition.lineNumber > editor.getModel().getLineCount()) { + // Add a new line at the end of the editor + editor.executeEdits("addNewLine", [{ + range: new monaco.Range(newPosition.lineNumber, 1, newPosition.lineNumber, 1), + text: "\n", + forceMoveMarkers: true, + }]); + } + + // Run the entire line of code. + executeCode(currentLine); + + // Move cursor to new position + editor.setPosition(newPosition); + } else { + // Code to run when Ctrl+Enter is pressed with 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); @@ -1308,15 +1694,15 @@

{ + // 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()); + // Check if no code is selected + if (isEmptyCodeText(selectedText)) { + // Obtain the current cursor position + let currentPosition = editor.getPosition(); + // Retrieve the current line content + let currentLine = editor.getModel().getLineContent(currentPosition.lineNumber); + + // Propose a new position to move the cursor to + let newPosition = new monaco.Position(currentPosition.lineNumber + 1, 1); + + // Check if the new position is beyond the last line of the editor + if (newPosition.lineNumber > editor.getModel().getLineCount()) { + // Add a new line at the end of the editor + editor.executeEdits("addNewLine", [{ + range: new monaco.Range(newPosition.lineNumber, 1, newPosition.lineNumber, 1), + text: "\n", + forceMoveMarkers: true, + }]); + } + + // Run the entire line of code. + executeCode(currentLine); + + // Move cursor to new position + editor.setPosition(newPosition); + } else { + // Code to run when Ctrl+Enter is pressed with 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); @@ -1463,15 +1898,15 @@

{ + // 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()); + // Check if no code is selected + if (isEmptyCodeText(selectedText)) { + // Obtain the current cursor position + let currentPosition = editor.getPosition(); + // Retrieve the current line content + let currentLine = editor.getModel().getLineContent(currentPosition.lineNumber); + + // Propose a new position to move the cursor to + let newPosition = new monaco.Position(currentPosition.lineNumber + 1, 1); + + // Check if the new position is beyond the last line of the editor + if (newPosition.lineNumber > editor.getModel().getLineCount()) { + // Add a new line at the end of the editor + editor.executeEdits("addNewLine", [{ + range: new monaco.Range(newPosition.lineNumber, 1, newPosition.lineNumber, 1), + text: "\n", + forceMoveMarkers: true, + }]); + } + + // Run the entire line of code. + executeCode(currentLine); + + // Move cursor to new position + editor.setPosition(newPosition); + } else { + // Code to run when Ctrl+Enter is pressed with 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); @@ -1622,15 +2106,15 @@

{ + // 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()); + // Check if no code is selected + if (isEmptyCodeText(selectedText)) { + // Obtain the current cursor position + let currentPosition = editor.getPosition(); + // Retrieve the current line content + let currentLine = editor.getModel().getLineContent(currentPosition.lineNumber); + + // Propose a new position to move the cursor to + let newPosition = new monaco.Position(currentPosition.lineNumber + 1, 1); + + // Check if the new position is beyond the last line of the editor + if (newPosition.lineNumber > editor.getModel().getLineCount()) { + // Add a new line at the end of the editor + editor.executeEdits("addNewLine", [{ + range: new monaco.Range(newPosition.lineNumber, 1, newPosition.lineNumber, 1), + text: "\n", + forceMoveMarkers: true, + }]); + } + + // Run the entire line of code. + executeCode(currentLine); + + // Move cursor to new position + editor.setPosition(newPosition); + } else { + // Code to run when Ctrl+Enter is pressed with 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); @@ -1781,15 +2314,15 @@

{ @@ -1921,6 +2444,65 @@

{ + // 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()); + // Check if no code is selected + if (isEmptyCodeText(selectedText)) { + // Obtain the current cursor position + let currentPosition = editor.getPosition(); + // Retrieve the current line content + let currentLine = editor.getModel().getLineContent(currentPosition.lineNumber); + + // Propose a new position to move the cursor to + let newPosition = new monaco.Position(currentPosition.lineNumber + 1, 1); + + // Check if the new position is beyond the last line of the editor + if (newPosition.lineNumber > editor.getModel().getLineCount()) { + // Add a new line at the end of the editor + editor.executeEdits("addNewLine", [{ + range: new monaco.Range(newPosition.lineNumber, 1, newPosition.lineNumber, 1), + text: "\n", + forceMoveMarkers: true, + }]); + } + + // Run the entire line of code. + executeCode(currentLine); + + // Move cursor to new position + editor.setPosition(newPosition); + } else { + // Code to run when Ctrl+Enter is pressed with 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); @@ -1939,15 +2521,15 @@

{ - if (msg.type === "canvasExec") { - if (!canvas) { + // Determine if old canvas can be used or a new canvas is required. + if (msg.type === 'canvas'){ + // Add image to the current canvas + if (msg.data.event === 'canvasImage') { + canvas.getContext('2d').drawImage(msg.data.image, 0, 0); + } else if (msg.data.event === 'canvasNewPage') { + // Generate a new canvas element canvas = document.createElement("canvas"); canvas.setAttribute("width", 2 * 504); canvas.setAttribute("height", 2 * 360); @@ -1975,7 +2562,6 @@

{ @@ -2080,6 +2652,65 @@

{ + // 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()); + // Check if no code is selected + if (isEmptyCodeText(selectedText)) { + // Obtain the current cursor position + let currentPosition = editor.getPosition(); + // Retrieve the current line content + let currentLine = editor.getModel().getLineContent(currentPosition.lineNumber); + + // Propose a new position to move the cursor to + let newPosition = new monaco.Position(currentPosition.lineNumber + 1, 1); + + // Check if the new position is beyond the last line of the editor + if (newPosition.lineNumber > editor.getModel().getLineCount()) { + // Add a new line at the end of the editor + editor.executeEdits("addNewLine", [{ + range: new monaco.Range(newPosition.lineNumber, 1, newPosition.lineNumber, 1), + text: "\n", + forceMoveMarkers: true, + }]); + } + + // Run the entire line of code. + executeCode(currentLine); + + // Move cursor to new position + editor.setPosition(newPosition); + } else { + // Code to run when Ctrl+Enter is pressed with 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); @@ -2098,15 +2729,15 @@

{ - if (msg.type === "canvasExec") { - if (!canvas) { + // Determine if old canvas can be used or a new canvas is required. + if (msg.type === 'canvas'){ + // Add image to the current canvas + if (msg.data.event === 'canvasImage') { + canvas.getContext('2d').drawImage(msg.data.image, 0, 0); + } else if (msg.data.event === 'canvasNewPage') { + // Generate a new canvas element canvas = document.createElement("canvas"); canvas.setAttribute("width", 2 * 504); canvas.setAttribute("height", 2 * 360); @@ -2134,7 +2770,6 @@

<
Hello!
+

webr extension version: 0.3.6

diff --git a/search.json b/search.json index 4d39e34..79667c0 100644 --- a/search.json +++ b/search.json @@ -53,7 +53,7 @@ "href": "documents/webr/index.html#sample-cases", "title": "webR-enabled code cells", "section": "2 Sample cases", - "text": "2 Sample cases\n\n2.1 Fit a linear regression model\nLoading\n webR...\n\n\n \n\n\n\n\n2.2 Create a graph with base R\nLoading\n webR...\n\n\n \n\n\n\n\n2.3 Packages\n\n2.3.1 Available Packages\nYou can view what packages are available for webR by either executing the following R code (either with WebR or just R):\nLoading\n webR...\n\n\n \n\n\nOr, by navigating to the WebR repository:\nhttps://github.com/r-wasm/webr-repo/blob/main/repo-packages\n\n\n2.3.2 Installing a Package\nInstalling ggplot2 may take at least 2 minutes to run.\nLoading\n webR...\n\n\n \n\n\n\n\n2.3.3 Using a Package\nOnce ggplot2 is loaded, then use the package as normal.\nLoading\n webR...\n\n\n \n\n\n\n\n\n2.4 Define variables and re-use them in later cells\nLoading\n webR...\n\n\n \n\n\nLoading\n webR...\n\n\n \n\n\n\n\n2.5 Escape characters in a string\nLoading\n webR...\n\n\n \n\n\n\n\n2.6 Anonymous function definition\nLoading\n webR...\n\n\n \n\n\n\n\n2.7 Empty code cell\nLoading\n webR...\n\n\n \n\n\n\n\n2.8 Prior code cell\n\nLoading\n webR...\n\n\n \n\n\n\n\n\n2.9 Pre-rendered code cell\n\nmessage(\"Hello!\")\n\nHello!" + "text": "2 Sample cases\n\n2.1 Fit a linear regression model\nLoading\n webR...\n\n\n \n\n\n\n\n2.2 Create a graph with base R\nLoading\n webR...\n\n\n \n\n\n\n\n2.3 Packages\n\n2.3.1 Available Packages\nYou can view what packages are available for webR by either executing the following R code (either with WebR or just R):\nLoading\n webR...\n\n\n \n\n\nOr, by navigating to the WebR repository:\nhttps://github.com/r-wasm/webr-repo/blob/main/repo-packages\n\n\n2.3.2 Installing a Package\nInstalling ggplot2 may take at least 2 minutes to run.\nLoading\n webR...\n\n\n \n\n\n\n\n2.3.3 Using a Package\nOnce ggplot2 is loaded, then use the package as normal.\nLoading\n webR...\n\n\n \n\n\n\n\n\n2.4 Define variables and re-use them in later cells\nLoading\n webR...\n\n\n \n\n\nLoading\n webR...\n\n\n \n\n\n\n\n2.5 Escape characters in a string\nLoading\n webR...\n\n\n \n\n\n\n\n2.6 Anonymous function definition\nLoading\n webR...\n\n\n \n\n\n\n\n2.7 Empty code cell\nLoading\n webR...\n\n\n \n\n\n\n\n2.8 Prior code cell\n\nLoading\n webR...\n\n\n \n\n\n\n\n\n2.9 Pre-rendered code cell\n\nmessage(\"Hello!\")\n\nHello!\n\n\nwebr extension version: 0.3.6" }, { "objectID": "documents/ojs/index.html", diff --git a/sitemap.xml b/sitemap.xml index d1e4368..d2b210c 100644 --- a/sitemap.xml +++ b/sitemap.xml @@ -2,30 +2,30 @@ https://github.com/royfrancis/quarto-exp/index.html - 2023-09-30T23:08:31.436Z + 2023-09-30T23:18:32.773Z https://github.com/royfrancis/quarto-exp/documents/report/index.html - 2023-09-30T23:08:30.824Z + 2023-09-30T23:18:32.013Z https://github.com/royfrancis/quarto-exp/documents/webr/index.html - 2023-09-30T23:08:21.684Z + 2023-09-30T23:18:20.837Z https://github.com/royfrancis/quarto-exp/documents/ojs/index.html - 2023-09-30T23:08:16.172Z + 2023-09-30T23:18:14.221Z https://github.com/royfrancis/quarto-exp/documents/fontawesome/index.html - 2023-09-30T23:08:13.704Z + 2023-09-30T23:18:11.601Z https://github.com/royfrancis/quarto-exp/documents/revealjs/index.html - 2023-09-30T23:08:19.928Z + 2023-09-30T23:18:18.577Z https://github.com/royfrancis/quarto-exp/documents/theme-title/index.html - 2023-09-30T23:08:31.112Z + 2023-09-30T23:18:32.365Z