11/** @file Provides a Vue reactive API for Markdown formatting in CodeMirror. */
22import {
3- canInsertCodeBlock ,
43 getBlockType ,
54 insertCodeBlock ,
65 removeCodeBlock ,
@@ -24,6 +23,7 @@ export { type BlockType }
2423interface ReactiveFormatting {
2524 inline : Record < InlineFormattingNode , Ref < boolean | undefined > >
2625 blockType : Ref < BlockType | undefined >
26+ unformattable : Ref < boolean >
2727}
2828
2929const reactiveFormattingFacet = Facet . define < ReactiveFormatting , ReactiveFormatting > ( {
@@ -47,8 +47,10 @@ export function useMarkdownFormatting(view: EditorView) {
4747 insertLink : computed (
4848 ( ) => canInsertLink ( view . state ) && ( ( ) => view . dispatch ( insertLink ( view . state ) ) ) ,
4949 ) ,
50- insertCodeBlock : computed (
51- ( ) => canInsertCodeBlock ( view . state ) && ( ( ) => view . dispatch ( insertCodeBlock ( view . state ) ) ) ,
50+ insertCodeBlock : computed ( ( ) =>
51+ reactiveFormatting . unformattable . value ?
52+ undefined
53+ : ( ) => view . dispatch ( insertCodeBlock ( view . state ) ) ,
5254 ) ,
5355 blockType : proxyRefs ( {
5456 value : readonly ( reactiveFormatting . blockType ) ,
@@ -68,13 +70,14 @@ export function useMarkdownFormatting(view: EditorView) {
6870
6971/** Returns an extension that supports reactively watch the formatting of the selected text. */
7072export function markdownFormatting ( ) : Extension {
71- const reactiveFormatting = {
73+ const reactiveFormatting : ReactiveFormatting = {
7274 inline : {
73- Emphasis : ref < boolean > ( ) ,
74- StrongEmphasis : ref < boolean > ( ) ,
75- Strikethrough : ref < boolean > ( ) ,
75+ Emphasis : ref ( ) ,
76+ StrongEmphasis : ref ( ) ,
77+ Strikethrough : ref ( ) ,
7678 } ,
77- blockType : ref < BlockType | undefined > ( ) ,
79+ blockType : ref ( ) ,
80+ unformattable : ref ( false ) ,
7881 }
7982 const reactiveFormattingFacetExt = reactiveFormattingFacet . of ( reactiveFormatting )
8083 return [
@@ -85,6 +88,7 @@ export function markdownFormatting(): Extension {
8588 for ( const key of objects . unsafeKeys ( reactiveFormatting . inline ) )
8689 reactiveFormatting . inline [ key ] . value = formatting ?. [ key ]
8790 reactiveFormatting . blockType . value = getBlockType ( update . view . state )
91+ reactiveFormatting . unformattable . value = formatting === undefined
8892 } ) ,
8993 ]
9094}
0 commit comments