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,
156 lines (140 loc) • 7.38 kB
JavaScript
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function _toConsumableArray(r) { return _arrayWithoutHoles(r) || _iterableToArray(r) || _unsupportedIterableToArray(r) || _nonIterableSpread(); }
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
function _iterableToArray(r) { if ("undefined" != typeof Symbol && null != r[Symbol.iterator] || null != r["@@iterator"]) return Array.from(r); }
function _arrayWithoutHoles(r) { if (Array.isArray(r)) return _arrayLikeToArray(r); }
function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
/*
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/>.
*/
// utils/stackUtils.js
// 1) Save a snapshot immediately
export function saveSnapshot(manager, editor, foldingUtils) {
if (manager.isUndoRedo) return;
var snap = {
text: editor.value,
foldedBlocks: JSON.parse(JSON.stringify(foldingUtils.getFoldedBlocksById())),
cursorStart: editor.selectionStart,
cursorEnd: editor.selectionEnd
};
manager.undoStack.push(snap);
manager.redoStack = [];
if (manager.undoStack.length > manager.maxHistory) {
manager.undoStack.shift();
}
}
// 2) Debounced save
export function debounceSnapshot(manager, editor, foldingUtils, onChange) {
clearTimeout(manager.debounceId);
manager.debounceId = setTimeout(function () {
saveSnapshot(manager, editor, foldingUtils);
onChange(function (prevState) {
return _objectSpread(_objectSpread({}, prevState), {}, {
undoStack: _toConsumableArray(manager.undoStack),
redoStack: _toConsumableArray(manager.redoStack)
});
});
}, 300);
}
/**
* Undo the last snapshot: restore text + foldedBlocks, and push current state into redoStack.
*/
function undo(manager, editor, foldingUtils, onChange) {
if (!manager.undoStack.length) return;
// Decide whether to pop or just peek
var prev;
if (manager.undoStack.length < 2) {
// Only one snapshot: peek at it but don't remove
prev = manager.undoStack[0];
} else {
// More than one: pop the top snapshot
prev = manager.undoStack.pop();
// Push current state into redoStack
manager.redoStack.push({
text: editor.value,
foldedBlocks: JSON.parse(JSON.stringify(foldingUtils.getFoldedBlocksById())),
cursorStart: editor.selectionStart,
cursorEnd: editor.selectionEnd
});
}
manager.isUndoingOrRedoing = true;
editor.value = prev.text;
editor.selectionStart = prev.cursorStart;
editor.selectionEnd = prev.cursorEnd;
foldingUtils.updateFoldedBlocks(prev.foldedBlocks);
// Notify React state if provided
if (typeof onChange === 'function') {
onChange(function (prevState) {
return _objectSpread(_objectSpread({}, prevState), {}, {
undoStack: _toConsumableArray(manager.undoStack),
redoStack: _toConsumableArray(manager.redoStack)
});
});
}
manager.isUndoingOrRedoing = false;
}
/**
* Redo the last undone snapshot: restore text + foldedBlocks, and push current state into undoStack.
*/
function redo(manager, editor, foldingUtils, onChange) {
if (!manager.redoStack.length) return;
// Push current state into undoStack
manager.undoStack.push({
text: editor.value,
foldedBlocks: JSON.parse(JSON.stringify(foldingUtils.getFoldedBlocksById())),
cursorStart: editor.selectionStart,
cursorEnd: editor.selectionEnd
});
// Pop the last redo snapshot and restore
var next = manager.redoStack.pop();
manager.isUndoingOrRedoing = true;
editor.value = next.text;
editor.selectionStart = next.cursorStart;
editor.selectionEnd = next.cursorEnd;
foldingUtils.updateFoldedBlocks(next.foldedBlocks);
// Notify React state if provided
if (typeof onChange === 'function') {
onChange(function (prevState) {
return _objectSpread(_objectSpread({}, prevState), {}, {
undoStack: _toConsumableArray(manager.undoStack),
redoStack: _toConsumableArray(manager.redoStack)
});
});
}
manager.isUndoingOrRedoing = false;
}
/**
* Keydown handler for undo/redo shortcuts.
*/
// Keydown handler
export function handleUndoRedo(e, stateManager, editor, foldingUtils, onChange) {
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, onChange);
}
// Ctrl+Y or Cmd+Shift+Z => redo
else if (e.key === "y" || e.key === "z" && e.shiftKey) {
e.preventDefault();
redo(stateManager, editor, foldingUtils, onChange);
}
}