UNPKG

shahnevis-core

Version:

**Shahnevis Core** is a lightweight and flexible library for building custom code editors. It provides essential features like syntax highlighting, a minimap, multi-cursor support, line numbering, and plugin management. This library is framework-agnostic,

93 lines (81 loc) 3.31 kB
/* This file is part of Shahnevis Core. Copyright (C) 2024 shahrooz saneidarani (github.com/shahroozD) Shahnevis Core is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. Shahnevis Core is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>. */ var isUndoingOrRedoing = false; var debounceTimeout; export function debounceSaveState(stateManager, editor, foldingUtils) { clearTimeout(debounceTimeout); debounceTimeout = setTimeout(saveState(stateManager, editor, foldingUtils), 300); // Save state after 300ms of inactivity } // Save a snapshot of both the text and the folded‐blocks export function saveState(stateManager, editor, foldingUtils) { var maxHistorySize = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 100; if (isUndoingOrRedoing) return; var snapshot = { text: editor.value, foldedBlocks: JSON.parse(JSON.stringify(foldingUtils.getFoldedBlocksById())) }; stateManager.undoStack.push(snapshot); stateManager.redoStack = []; if (stateManager.undoStack.length > maxHistorySize) { stateManager.undoStack.shift(); } } // Undo: pop from undoStack, push current into redoStack export function undo(stateManager, editor, foldingUtils) { if (!stateManager.undoStack.length) return; // push current into redo stateManager.redoStack.push({ text: editor.value, foldedBlocks: JSON.parse(JSON.stringify(foldingUtils.getFoldedBlocksById())) }); var prev = stateManager.undoStack.pop(); isUndoingOrRedoing = true; // console.log("prev.foldedBlocks", prev.foldedBlocks); editor.value = prev.text; foldingUtils.updateFoldedBlocks(prev.foldedBlocks); isUndoingOrRedoing = false; // console.log(stateManager.undoStack); } // Redo: pop from redoStack, push current into undoStack export function redo(stateManager, editor, foldingUtils) { if (!stateManager.redoStack.length) return; stateManager.undoStack.push({ text: editor.value, foldedBlocks: JSON.parse(JSON.stringify(foldingUtils.getFoldedBlocksById())) }); var next = stateManager.redoStack.pop(); isUndoingOrRedoing = true; editor.value = next.text; foldingUtils.updateFoldedBlocks(next.foldedBlocks); isUndoingOrRedoing = false; } // // Event listener for tracking changes in the editor // // editor.addEventListener("input", saveState); // editor.addEventListener("input", debounceSaveState); // Keydown handler export function handleUndoRedo(e, stateManager, editor, foldingUtils) { var mod = e.ctrlKey || e.metaKey; if (!mod) return; // Ctrl+Z / Cmd+Z without Shift => undo if (e.key === "z" && !e.shiftKey) { e.preventDefault(); undo(stateManager, editor, foldingUtils); } // Ctrl+Y or Cmd+Shift+Z => redo else if (e.key === "y" || e.key === "z" && e.shiftKey) { e.preventDefault(); redo(stateManager, editor, foldingUtils); } }