@@ -10,6 +10,7 @@ import type {
1010 StyleSchema ,
1111} from "../../schema/index.js" ;
1212import { EventEmitter } from "../../util/EventEmitter.js" ;
13+ import { ySyncPluginKey } from "y-prosemirror" ;
1314
1415export type FilePanelState <
1516 I extends InlineContentSchema ,
@@ -31,7 +32,7 @@ export class FilePanelView<I extends InlineContentSchema, S extends StyleSchema>
3132 I ,
3233 S
3334 > ,
34- private readonly pluginKey : PluginKey ,
35+ private readonly pluginKey : PluginKey < FilePanelState < I , S > > ,
3536 private readonly pmView : EditorView ,
3637 emitUpdate : ( state : FilePanelState < I , S > ) => void
3738 ) {
@@ -81,11 +82,10 @@ export class FilePanelView<I extends InlineContentSchema, S extends StyleSchema>
8182 } ;
8283
8384 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 ) ;
8787
88- if ( ! this . state ?. show && pluginState . block && this . editor . isEditable ) {
88+ if ( ! this . state ?. show && pluginState ? .block && this . editor . isEditable ) {
8989 const blockElement = this . pmView . root . querySelector (
9090 `[data-node-type="blockContainer"][data-id="${ pluginState . block . id } "]`
9191 ) ;
@@ -103,16 +103,15 @@ export class FilePanelView<I extends InlineContentSchema, S extends StyleSchema>
103103 return ;
104104 }
105105
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 ( ) ;
116115 }
117116 }
118117
@@ -132,7 +131,9 @@ export class FilePanelView<I extends InlineContentSchema, S extends StyleSchema>
132131 }
133132}
134133
135- const filePanelPluginKey = new PluginKey ( "FilePanelPlugin" ) ;
134+ const filePanelPluginKey = new PluginKey < FilePanelState < any , any > > (
135+ "FilePanelPlugin"
136+ ) ;
136137
137138export class FilePanelProsemirrorPlugin <
138139 I extends InlineContentSchema ,
@@ -173,13 +174,21 @@ export class FilePanelProsemirrorPlugin<
173174 block : undefined ,
174175 } ;
175176 } ,
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 ) ;
179180
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 ;
183192 } ,
184193 } ,
185194 } ) ;
0 commit comments