@@ -7,7 +7,7 @@ import { Comment } from './comment'
77import { FrameComment } from './frame-comment'
88import { InlineComment } from './inline-comment'
99import type { ExpectedSchemes } from './types'
10- import { trackedTranslate } from './utils'
10+ import { trackedTranslate , trackedTranslateComment } from './utils'
1111
1212export { Comment , FrameComment , InlineComment }
1313export type { ExpectedSchemes }
@@ -38,7 +38,8 @@ export type Props = {
3838 * A plugin that provides comments for nodes
3939 * @priority 8
4040 */
41- export class CommentPlugin < Schemes extends ExpectedSchemes , K = BaseArea < Schemes > > extends Scope < Produces , [ BaseArea < Schemes > | K ] > {
41+ export class CommentPlugin < Schemes extends ExpectedSchemes , K = BaseArea < Schemes > >
42+ extends Scope < Produces , [ BaseArea < Schemes > | K ] > {
4243 public comments = new Map < Comment [ 'id' ] , Comment > ( )
4344 private area ! : BaseAreaPlugin < Schemes , K >
4445 private editor ! : NodeEditor < Schemes >
@@ -57,17 +58,13 @@ export class CommentPlugin<Schemes extends ExpectedSchemes, K = BaseArea<Schemes
5758 this . area = this . parentScope < BaseAreaPlugin < Schemes , K > > ( BaseAreaPlugin )
5859 this . editor = this . area . parentScope < NodeEditor < Schemes > > ( NodeEditor )
5960
60- let picked : string [ ] = [ ]
61-
6261 const { translate, isTranslating } = trackedTranslate ( { area : this . area } )
62+ const commentTracker = trackedTranslateComment ( this . comments )
6363
6464 // eslint-disable-next-line max-statements, complexity
65- this . addPipe ( context => {
65+ this . addPipe ( async context => {
6666 if ( ! context || typeof context !== 'object' || ! ( 'type' in context ) ) return context
6767
68- if ( context . type === 'nodepicked' ) {
69- picked . push ( context . data . id )
70- }
7168 if ( context . type === 'reordered' ) {
7269 const views = Array . from ( this . area . nodeViews . entries ( ) )
7370 const matchedView = views . find ( ( [ , view ] ) => view . element === context . data . element )
@@ -92,47 +89,48 @@ export class CommentPlugin<Schemes extends ExpectedSchemes, K = BaseArea<Schemes
9289 const dy = position . y - previous . y
9390 const comments = Array . from ( this . comments . values ( ) )
9491
95- comments
92+ await Promise . all ( comments
9693 . filter ( comment => comment . linkedTo ( id ) )
97- . map ( comment => {
98- if ( comment instanceof InlineComment ) comment . translate ( dx , dy , [ id ] )
99- if ( comment instanceof FrameComment && ! picked . includes ( id ) ) comment . resize ( )
100- } )
94+ . map ( async comment => {
95+ if ( comment instanceof InlineComment && ! commentTracker . isTranslating ( comment . id ) ) {
96+ await commentTracker . translate ( comment . id , dx , dy , [ id ] )
97+ }
98+ if ( comment instanceof FrameComment && ! commentTracker . isResizing ( comment . id ) ) {
99+ await commentTracker . resize ( comment . id )
100+ }
101+ } ) )
101102 }
102103 if ( context . type === 'commenttranslated' ) {
103104 const { id, dx, dy, sources } = context . data
104105 const comment = this . comments . get ( id )
105106
106- if ( ! ( comment instanceof FrameComment && comment ) ) return context
107+ if ( ! ( comment instanceof FrameComment ) ) return context
107108
108- comment . links
109+ await Promise . all ( comment . links
109110 . filter ( linkId => ! sources ?. includes ( linkId ) )
110111 . map ( linkId => ( { linkId, view : this . area . nodeViews . get ( linkId ) } ) )
111- // eslint-disable-next-line @typescript-eslint/no-misused-promises
112- . forEach ( async ( { linkId, view } ) => {
112+ . map ( async ( { linkId, view } ) => {
113113 if ( ! view ) return
114114 // prevent an infinite loop if a node is selected and translated along with the selected comment
115115 if ( ! await this . emit ( { type : 'commentlinktranslate' , data : { id, link : linkId } } ) ) return
116116
117- if ( ! isTranslating ( linkId ) ) void translate ( linkId , view . position . x + dx , view . position . y + dy )
118- } )
117+ if ( ! isTranslating ( linkId ) ) await translate ( linkId , view . position . x + dx , view . position . y + dy )
118+ } ) )
119119 }
120120 if ( context . type === 'nodedragged' ) {
121121 const { id } = context . data
122122 const comments = Array . from ( this . comments . values ( ) )
123123
124124 comments
125125 . filter ( ( comment ) : comment is FrameComment => comment instanceof FrameComment )
126- . filter ( comment => {
126+ . forEach ( comment => {
127127 const contains = comment . intersects ( id )
128128 const links = comment . links . filter ( nodeId => nodeId !== id )
129129
130130 comment . linkTo ( contains
131131 ? [ ...links , id ]
132132 : links )
133133 } )
134-
135- picked = picked . filter ( p => p !== id )
136134 }
137135 return context
138136 } )
@@ -168,7 +166,7 @@ export class CommentPlugin<Schemes extends ExpectedSchemes, K = BaseArea<Schemes
168166 if ( newText !== null ) {
169167 comment . text = newText
170168 comment . update ( )
171- comment . translate ( 0 , 0 )
169+ await comment . translate ( 0 , 0 )
172170 }
173171 }
174172
@@ -188,7 +186,9 @@ export class CommentPlugin<Schemes extends ExpectedSchemes, K = BaseArea<Schemes
188186 this . area . area . content . reorder ( comment . element , null )
189187 void this . emit ( { type : 'commentselected' , data } )
190188 } ,
191- translate : ( { id } , dx , dy , sources ) => void this . emit ( { type : 'commenttranslated' , data : { id, dx, dy, sources } } )
189+ translate : async ( { id } , dx , dy , sources ) => {
190+ await this . emit ( { type : 'commenttranslated' , data : { id, dx, dy, sources } } )
191+ }
192192 } )
193193
194194 comment . x = x
@@ -214,7 +214,9 @@ export class CommentPlugin<Schemes extends ExpectedSchemes, K = BaseArea<Schemes
214214 this . area . area . content . reorder ( comment . element , this . area . area . content . holder . firstChild )
215215 void this . emit ( { type : 'commentselected' , data } )
216216 } ,
217- translate : ( { id } , dx , dy , sources ) => void this . emit ( { type : 'commenttranslated' , data : { id, dx, dy, sources } } )
217+ translate : async ( { id } , dx , dy , sources ) => {
218+ await this . emit ( { type : 'commenttranslated' , data : { id, dx, dy, sources } } )
219+ }
218220 } )
219221
220222 comment . linkTo ( links )
@@ -260,7 +262,7 @@ export class CommentPlugin<Schemes extends ExpectedSchemes, K = BaseArea<Schemes
260262
261263 if ( ! comment ) return
262264
263- comment . translate ( dx , dy )
265+ void comment . translate ( dx , dy )
264266 }
265267
266268 /**
0 commit comments