UNPKG

lexical-vue

Version:

An extensible Vue 3 web text-editor based on Lexical.

49 lines (48 loc) 1.58 kB
import { defineComponent, toValue, watchEffect } from "vue"; import { AutoLinkNode, createLinkMatcherWithRegExp, registerAutoLink } from "@lexical/link"; import tiny_invariant from "tiny-invariant"; import { useLexicalComposer } from "./LexicalComposer.vine.js"; function useAutoLink(editor, matchers, onChange) { watchEffect((onInvalidate)=>{ if (!editor.hasNodes([ AutoLinkNode ])) tiny_invariant(false, 'LexicalAutoLinkPlugin: AutoLinkNode not registered on editor'); const unregister = registerAutoLink(editor, { changeHandlers: onChange ? [ onChange ] : [], matchers: toValue(matchers) }); onInvalidate(unregister); }); } const AutoLinkPlugin = (()=>{ const __vine = defineComponent({ name: 'AutoLinkPlugin', props: { matchers: { required: true } }, emits: [ 'change' ], setup (__props, param) { let { emit: __emit, expose: __expose } = param; const emit = __emit; __expose(); const props = __props; const editor = useLexicalComposer(); useAutoLink(editor, ()=>props.matchers, (url, prevUrl)=>{ emit('change', { url, prevUrl }); }); return (_ctx, _cache)=>null; } }); __vine.__vue_vine = true; return __vine; })(); export { AutoLinkPlugin, createLinkMatcherWithRegExp };