@@ -74,6 +74,60 @@ export const codeBlockShiki = CodeBlock.extend<CodeBlockShikiOptions, CodeBlockS
7474 } ) ,
7575 ]
7676 } ,
77+ addNodeView ( ) {
78+ return ( { editor, node, getPos, HTMLAttributes } ) => {
79+ const dom = window . document . createElement ( 'pre' )
80+ dom . classList . add ( 'note-editor__code-block-shiki' )
81+ Object . keys ( HTMLAttributes ) . forEach ( ( key ) => {
82+ dom . setAttribute ( key , HTMLAttributes [ key ] )
83+ } )
84+
85+ const content = window . document . createElement ( 'code' )
86+ // @ts -expect-error language
87+ const langClass = this . options . languageClassPrefix + node . attrs . language
88+ content . classList . add ( langClass )
89+ dom . append ( content )
90+
91+ if ( this . editor . isEditable ) {
92+ const selectLang = window . document . createElement ( 'select' )
93+ selectLang . classList . add ( 'note-editor__code-block-shiki__select-lang' )
94+ selectLang . addEventListener ( 'change' , ( event ) => {
95+ // @ts -expect-error value
96+ const lang = event . target . value
97+ editor . commands . command ( ( { tr } ) => {
98+ const pos = ( getPos as ( ) => number ) ( )
99+ tr . setNodeAttribute ( pos , 'language' , lang )
100+
101+ return true
102+ } )
103+ } )
104+ const options = bundledLanguagesInfo . map ( ( item ) => {
105+ const option = document . createElement ( 'option' )
106+ option . setAttribute ( 'value' , item . id )
107+ // @ts -expect-error language
108+ if ( node . attrs . language === item . id || node . attrs . language === item . name || item . aliases ?. includes ( node . attrs . language ) )
109+ option . setAttribute ( 'selected' , '' )
110+
111+ option . textContent = item . id
112+ return option
113+ } )
114+ selectLang . append ( ...options )
115+ dom . append ( selectLang )
116+ }
117+ else {
118+ const langTag = window . document . createElement ( 'div' )
119+ langTag . classList . add ( 'note-editor__code-block-shiki__lang-tag' )
120+ // @ts -expect-error language
121+ langTag . textContent = node . attrs . language ?? 'text'
122+ dom . append ( langTag )
123+ }
124+
125+ return {
126+ dom,
127+ contentDOM : content ,
128+ }
129+ }
130+ } ,
77131} )
78132
79133function getDecorations ( {
0 commit comments