UNPKG

@blocknote/core

Version:

A "Notion-style" block-based extensible text editor built on top of Prosemirror and Tiptap.

1 lines 3.15 kB
{"version":3,"file":"ShowSelection-BxnbRvy4.cjs","sources":["../src/extensions/ShowSelection/ShowSelection.ts"],"sourcesContent":["import { Plugin, PluginKey } from \"prosemirror-state\";\nimport { Decoration, DecorationSet } from \"prosemirror-view\";\nimport {\n createExtension,\n createStore,\n} from \"../../editor/BlockNoteExtension.js\";\n\nconst PLUGIN_KEY = new PluginKey(`blocknote-show-selection`);\n\n/**\n * Plugin that shows adds a decoration around the current selection\n * This can be used to highlight the current selection in the UI even when the\n * text editor is not focused.\n */\nexport const ShowSelectionExtension = createExtension(({ editor }) => {\n const store = createStore(\n { enabledSet: new Set<string>() },\n {\n onUpdate() {\n editor.transact((tr) => tr.setMeta(PLUGIN_KEY, {}));\n },\n },\n );\n return {\n key: \"showSelection\",\n store,\n prosemirrorPlugins: [\n new Plugin({\n key: PLUGIN_KEY,\n props: {\n decorations: (state) => {\n const { doc, selection } = state;\n if (store.state.enabledSet.size === 0) {\n return DecorationSet.empty;\n }\n const dec = Decoration.inline(selection.from, selection.to, {\n \"data-show-selection\": \"true\",\n });\n return DecorationSet.create(doc, [dec]);\n },\n },\n }),\n ],\n /**\n * Show or hide the selection decoration\n *\n * @param shouldShow - Whether to show the selection decoration\n * @param key - The key of the selection to show or hide,\n * this is necessary to prevent disabling ShowSelection from one place\n * will interfere with other parts of the code that need to show the selection decoration\n * (e.g.: CreateLinkButton and AIExtension)\n */\n showSelection(shouldShow: boolean, key: string) {\n store.setState({\n enabledSet: shouldShow\n ? new Set([...store.state.enabledSet, key])\n : new Set([...store.state.enabledSet].filter((k) => k !== key)),\n });\n },\n } as const;\n});\n"],"names":["PLUGIN_KEY","PluginKey","ShowSelectionExtension","createExtension","editor","store","createStore","tr","Plugin","state","doc","selection","DecorationSet","dec","Decoration","shouldShow","key","k"],"mappings":"+HAOMA,EAAa,IAAIC,EAAAA,UAAU,0BAA0B,EAO9CC,EAAyBC,EAAAA,gBAAgB,CAAC,CAAE,OAAAC,KAAa,CACpE,MAAMC,EAAQC,EAAAA,YACZ,CAAE,WAAY,IAAI,GAAY,EAC9B,CACE,UAAW,CACTF,EAAO,SAAUG,GAAOA,EAAG,QAAQP,EAAY,CAAA,CAAE,CAAC,CACpD,CAAA,CACF,EAEF,MAAO,CACL,IAAK,gBACL,MAAAK,EACA,mBAAoB,CAClB,IAAIG,SAAO,CACT,IAAKR,EACL,MAAO,CACL,YAAcS,GAAU,CACtB,KAAM,CAAE,IAAAC,EAAK,UAAAC,CAAA,EAAcF,EAC3B,GAAIJ,EAAM,MAAM,WAAW,OAAS,EAClC,OAAOO,EAAAA,cAAc,MAEvB,MAAMC,EAAMC,EAAAA,WAAW,OAAOH,EAAU,KAAMA,EAAU,GAAI,CAC1D,sBAAuB,MAAA,CACxB,EACD,OAAOC,EAAAA,cAAc,OAAOF,EAAK,CAACG,CAAG,CAAC,CACxC,CAAA,CACF,CACD,CAAA,EAWH,cAAcE,EAAqBC,EAAa,CAC9CX,EAAM,SAAS,CACb,WAAYU,EACR,IAAI,IAAI,CAAC,GAAGV,EAAM,MAAM,WAAYW,CAAG,CAAC,EACxC,IAAI,IAAI,CAAC,GAAGX,EAAM,MAAM,UAAU,EAAE,OAAQY,GAAMA,IAAMD,CAAG,CAAC,CAAA,CACjE,CACH,CAAA,CAEJ,CAAC"}