@lobehub/editor
Version:
A powerful and extensible rich text editor built on Meta's Lexical framework, providing a modern editing experience with React integration.
45 lines • 1.78 kB
JavaScript
import { $getSelection, $isNodeSelection, $isRangeSelection } from 'lexical';
export function registerFileNodeSelectionObserver(editor) {
var selectFileKeys = [];
return editor.registerUpdateListener(function (_ref) {
var editorState = _ref.editorState;
var selection = editorState.read(function () {
return $getSelection();
});
var newSelectFileKeys = [];
if ($isNodeSelection(selection)) {
var nodes = editorState.read(function () {
return selection.getNodes();
});
nodes.forEach(function (node) {
if (node.getType() === 'file') {
newSelectFileKeys.push(node.getKey());
}
});
} else if ($isRangeSelection(selection) && !selection.isCollapsed()) {
editorState.read(function () {
selection.getNodes().forEach(function (node) {
if (node.getType() === 'file') {
newSelectFileKeys.push(node.getKey());
}
});
});
}
var removeKeys = selectFileKeys.filter(function (key) {
return !newSelectFileKeys.includes(key);
});
var addKeys = newSelectFileKeys.filter(function (key) {
return !selectFileKeys.includes(key);
});
selectFileKeys.length = 0;
selectFileKeys.push.apply(selectFileKeys, newSelectFileKeys);
removeKeys.forEach(function (key) {
var _editor$getElementByK;
(_editor$getElementByK = editor.getElementByKey(key)) === null || _editor$getElementByK === void 0 || _editor$getElementByK.classList.remove('selected');
});
addKeys.forEach(function (key) {
var _editor$getElementByK2;
(_editor$getElementByK2 = editor.getElementByKey(key)) === null || _editor$getElementByK2 === void 0 || _editor$getElementByK2.classList.add('selected');
});
});
}