@@ -10,6 +10,7 @@ import type {
10
10
StyleSchema ,
11
11
} from "../../schema/index.js" ;
12
12
import { EventEmitter } from "../../util/EventEmitter.js" ;
13
+ import { ySyncPluginKey } from "y-prosemirror" ;
13
14
14
15
export type FilePanelState <
15
16
I extends InlineContentSchema ,
@@ -31,7 +32,7 @@ export class FilePanelView<I extends InlineContentSchema, S extends StyleSchema>
31
32
I ,
32
33
S
33
34
> ,
34
- private readonly pluginKey : PluginKey ,
35
+ private readonly pluginKey : PluginKey < FilePanelState < I , S > > ,
35
36
private readonly pmView : EditorView ,
36
37
emitUpdate : ( state : FilePanelState < I , S > ) => void
37
38
) {
@@ -81,11 +82,10 @@ export class FilePanelView<I extends InlineContentSchema, S extends StyleSchema>
81
82
} ;
82
83
83
84
update ( view : EditorView , prevState : EditorState ) {
84
- const pluginState : {
85
- block : BlockFromConfig < FileBlockConfig , I , S > ;
86
- } = this . pluginKey . getState ( view . state ) ;
85
+ const pluginState = this . pluginKey . getState ( view . state ) ;
86
+ const prevPluginState = this . pluginKey . getState ( prevState ) ;
87
87
88
- if ( ! this . state ?. show && pluginState . block && this . editor . isEditable ) {
88
+ if ( ! this . state ?. show && pluginState ? .block && this . editor . isEditable ) {
89
89
const blockElement = this . pmView . root . querySelector (
90
90
`[data-node-type="blockContainer"][data-id="${ pluginState . block . id } "]`
91
91
) ;
@@ -103,16 +103,15 @@ export class FilePanelView<I extends InlineContentSchema, S extends StyleSchema>
103
103
return ;
104
104
}
105
105
106
- if (
107
- ! view . state . selection . eq ( prevState . selection ) ||
108
- ! view . state . doc . eq ( prevState . doc ) ||
109
- ! this . editor . isEditable
110
- ) {
111
- if ( this . state ?. show ) {
112
- this . state . show = false ;
113
-
114
- this . emitUpdate ( ) ;
115
- }
106
+ const isOpening = pluginState ?. block && ! prevPluginState ?. block ;
107
+ const isClosing = ! pluginState ?. block && prevPluginState ?. block ;
108
+ if ( isOpening && this . state && ! this . state . show ) {
109
+ this . state . show = true ;
110
+ this . emitUpdate ( ) ;
111
+ }
112
+ if ( isClosing && this . state ?. show ) {
113
+ this . state . show = false ;
114
+ this . emitUpdate ( ) ;
116
115
}
117
116
}
118
117
@@ -132,7 +131,9 @@ export class FilePanelView<I extends InlineContentSchema, S extends StyleSchema>
132
131
}
133
132
}
134
133
135
- const filePanelPluginKey = new PluginKey ( "FilePanelPlugin" ) ;
134
+ const filePanelPluginKey = new PluginKey < FilePanelState < any , any > > (
135
+ "FilePanelPlugin"
136
+ ) ;
136
137
137
138
export class FilePanelProsemirrorPlugin <
138
139
I extends InlineContentSchema ,
@@ -173,13 +174,21 @@ export class FilePanelProsemirrorPlugin<
173
174
block : undefined ,
174
175
} ;
175
176
} ,
176
- apply : ( transaction ) => {
177
- const block : BlockFromConfig < FileBlockConfig , I , S > | undefined =
178
- transaction . getMeta ( filePanelPluginKey ) ?. block ;
177
+ apply : ( transaction , prev ) => {
178
+ const state : FilePanelState < I , S > | undefined =
179
+ transaction . getMeta ( filePanelPluginKey ) ;
179
180
180
- return {
181
- block,
182
- } ;
181
+ if ( state ) {
182
+ return state ;
183
+ }
184
+
185
+ if (
186
+ ! transaction . getMeta ( ySyncPluginKey ) &&
187
+ ( transaction . selectionSet || transaction . docChanged )
188
+ ) {
189
+ return { block : undefined } ;
190
+ }
191
+ return prev ;
183
192
} ,
184
193
} ,
185
194
} ) ;
0 commit comments