lexical-vue
Version:
An extensible Vue 3 web text-editor based on Lexical.
33 lines (32 loc) • 1.23 kB
JavaScript
import { COMMAND_PRIORITY_CRITICAL } from "lexical";
import { onMounted, onUnmounted, readonly, ref } from "vue";
function registerLexicalCommandLogger(editor, loggedCommands, setLoggedCommands) {
const unregisterCommandListeners = new Set();
let i = 0;
for (const [command] of editor._commands)unregisterCommandListeners.add(editor.registerCommand(command, (payload)=>{
i += 1;
const newState = [
...loggedCommands.value
];
newState.push({
index: i,
payload,
type: command.type ? command.type : 'UNKNOWN'
});
if (newState.length > 10) newState.shift();
setLoggedCommands(newState);
return false;
}, COMMAND_PRIORITY_CRITICAL));
return ()=>unregisterCommandListeners.forEach((unregister)=>unregister());
}
function useLexicalCommandsLog(editor) {
const loggedCommands = ref([]);
onMounted(()=>{
const unregister = registerLexicalCommandLogger(editor, loggedCommands, (newState)=>{
loggedCommands.value = newState;
});
onUnmounted(unregister);
});
return readonly(loggedCommands);
}
export { registerLexicalCommandLogger, useLexicalCommandsLog };