From 49e04f7a1d5f2b6ff80ab4b5ceef437e6d1e3849 Mon Sep 17 00:00:00 2001 From: Vojtech Kral Date: Fri, 6 Jun 2014 23:29:46 +0200 Subject: [PATCH] Fix: editor reinitialization upon navigation --- .../book/editor.js | 128 +++++++++++------- 1 file changed, 78 insertions(+), 50 deletions(-) diff --git a/node_modules/gitbook-plugin-rust-playpen/book/editor.js b/node_modules/gitbook-plugin-rust-playpen/book/editor.js index b4be8db8e7..0ef51c335c 100644 --- a/node_modules/gitbook-plugin-rust-playpen/book/editor.js +++ b/node_modules/gitbook-plugin-rust-playpen/book/editor.js @@ -11,12 +11,12 @@ if (typeof String.prototype.startsWith != 'function') { // Regex for finding new lines var newLineRegex = /(?:\r\n|\r|\n)/g; -// Fetching DOM items -var activeCode = document.getElementById("active-code"); -var editorDiv = document.getElementById("editor"); -var resetButton = document.getElementById("reset-code"); -var runButton = document.getElementById("run-code"); -var resultDiv = document.getElementById("result"); +// DOM items +var activeCode; +var editorDiv; +var resetButton; +var runButton; +var resultDiv; // Background colors for program result on success/error var successColor = "#E2EEF6"; @@ -34,20 +34,80 @@ var SUCCESS = 0; var ERROR = 1; var WARNING = 2; -// JS exists, display ACE editor -activeCode.style.display = "block"; +// Ace editor +var editor; +var Range; -// Setting up ace editor -var editor = ace.edit("editor"); -var Range = ace.require('ace/range').Range; -editor.setTheme("ace/theme/chrome"); -editor.getSession().setMode("ace/mode/rust"); -editor.setShowPrintMargin(false); -editor.renderer.setShowGutter(false); -editor.setHighlightActiveLine(false); +// Original source code +var originalCode; -// Store original source code -var originalCode = editor.getSession().getValue(); +function initEditor() +{ + // Fetching DOM items + activeCode = document.getElementById("active-code"); + editorDiv = document.getElementById("editor"); + resetButton = document.getElementById("reset-code"); + runButton = document.getElementById("run-code"); + resultDiv = document.getElementById("result"); + + if (editorDiv === null) + return; // No editor on this page + + // Setup ace editor + editor = ace.edit("editor"); + Range = ace.require('ace/range').Range; + + // JS exists, display ACE editor + activeCode.style.display = "block"; + + editor.setTheme("ace/theme/chrome"); + editor.getSession().setMode("ace/mode/rust"); + editor.setShowPrintMargin(false); + editor.renderer.setShowGutter(false); + editor.setHighlightActiveLine(false); + + originalCode = editor.getSession().getValue(); + + // Set initial size to match initial content + updateEditorHeight(); + + // Registering handler for run button click + runButton.addEventListener("click", function(ev) { + resultDiv.style.display = "block"; + resultDiv.innerHTML = "Running..."; + + // clear previous markers, if any + markers.map(function(id) { editor.getSession().removeMarker(id); }); + + // Get the code, run the program + var program = editor.getValue(); + runProgram(program, handleResult); + }); + + // Registering handler for reset button click + resetButton.addEventListener("click", function(ev) { + editor.getSession().setValue(originalCode); + resultDiv.style.display = "none"; + }); + + // Highlight active line when focused + editor.on('focus', function() { + editor.setHighlightActiveLine(true); + }); + + // Don't when not + editor.on('blur', function() { + editor.setHighlightActiveLine(false); + }); +} + +initEditor(); + +require(["gitbook"], function(gitbook) { + gitbook.events.bind("page.change", function() { + initEditor(); + }) +}); // Changes the height of the editor to match its contents function updateEditorHeight() { @@ -60,9 +120,6 @@ function updateEditorHeight() { editor.resize(); }; -// Set initial size to match initial content -updateEditorHeight(); - function escapeHTML(unsafe) { return unsafe .replace(/&/g, "&") @@ -201,32 +258,3 @@ function parseProblems(lines) { return ranges; } - -// Registering handler for run button click -runButton.addEventListener("click", function(ev) { - resultDiv.style.display = "block"; - resultDiv.innerHTML = "Running..."; - - // clear previous markers, if any - markers.map(function(id) { editor.getSession().removeMarker(id); }); - - // Get the code, run the program - var program = editor.getValue(); - runProgram(program, handleResult); -}); - -// Registering handler for reset button click -resetButton.addEventListener("click", function(ev) { - editor.getSession().setValue(originalCode); - resultDiv.style.display = "none"; -}); - -// Highlight active line when focused -editor.on('focus', function() { - editor.setHighlightActiveLine(true); -}); - -// Don't when not -editor.on('blur', function() { - editor.setHighlightActiveLine(false); -});