@yuntijs/ui
Version:
☁️ Yunti UI - an open-source UI component library for building Cloud Native web apps
45 lines • 1.84 kB
JavaScript
import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
import { $convertFromMarkdownString, $convertToMarkdownString } from '@lexical/markdown';
import { useLexicalComposerContext } from '@lexical/react/LexicalComposerContext';
import { CLEAR_EDITOR_COMMAND } from 'lexical';
import { useCallback } from 'react';
import { PLAYGROUND_TRANSFORMERS } from "../plugins/MarkdownTransformers";
var shouldPreserveNewLinesInMarkdown = false;
export function useActions() {
var _useLexicalComposerCo = useLexicalComposerContext(),
_useLexicalComposerCo2 = _slicedToArray(_useLexicalComposerCo, 1),
editor = _useLexicalComposerCo2[0];
var handleClear = useCallback(function () {
// eslint-disable-next-line unicorn/no-useless-undefined
editor.dispatchCommand(CLEAR_EDITOR_COMMAND, undefined);
editor.focus();
}, [editor]);
var handleEditable = useCallback(function (isEditable) {
editor.setEditable(isEditable);
}, [editor]);
var initiateValue = useCallback(function (defaultValue) {
if (!defaultValue) return;
editor.update(function () {
$convertFromMarkdownString(defaultValue, PLAYGROUND_TRANSFORMERS, undefined, shouldPreserveNewLinesInMarkdown);
});
}, [editor]);
var getValue = useCallback(function () {
var text = editor.getEditorState().read(function () {
var md = $convertToMarkdownString(PLAYGROUND_TRANSFORMERS, undefined, shouldPreserveNewLinesInMarkdown);
return md;
});
return text;
}, [editor]);
var handleFocus = useCallback(function (callbackFn, options) {
editor.focus(callbackFn, options || {
defaultSelection: 'rootEnd'
});
}, [editor]);
return {
handleClear: handleClear,
handleEditable: handleEditable,
initiateValue: initiateValue,
getValue: getValue,
handleFocus: handleFocus
};
}