@@ -14,6 +14,7 @@ type PluginOptions = {
1414} ;
1515
1616type ContainerSet = {
17+ selector : string ;
1718 previous : HTMLElement ;
1819 next : HTMLElement ;
1920} ;
@@ -23,9 +24,7 @@ export default class SwupParallelPlugin extends Plugin {
2324
2425 requires = { swup : '>=4' } ;
2526
26- defaults : PluginOptions = {
27- containers : [ ]
28- } ;
27+ defaults : PluginOptions = { containers : [ ] } ;
2928 options : PluginOptions ;
3029
3130 originalContainers : string [ ] | null = null ;
@@ -59,39 +58,28 @@ export default class SwupParallelPlugin extends Plugin {
5958 startVisit : Handler < 'visit:start' > = ( visit ) => {
6059 this . originalContainers = null ;
6160
62- if ( ! visit . animation . animate || visit . animation . parallel === false ) {
63- return ;
64- }
65-
66- // Only mark as parallel visit if containers found
67- if ( this . visitHasParallelContainers ( visit ) ) {
61+ // Only mark as parallel visit if containers found and animation matches
62+ if ( this . visitHasPotentialParallelAnimation ( visit ) ) {
6863 visit . animation . wait = true ;
6964 visit . animation . parallel = true ;
7065 }
7166 } ;
7267
7368 skipOutAnimation : Handler < 'animation:out:await' > = ( visit , args ) => {
74- if ( visit . animation . animate && visit . animation . parallel ) {
69+ if ( this . isParallelVisit ( visit ) ) {
7570 args . skip = true ;
7671 }
7772 } ;
7873
7974 insertContainers : Handler < 'content:replace' > = ( visit , { page } ) => {
80- if ( ! visit . animation . animate || ! visit . animation . parallel ) {
81- return ;
82- }
83-
84- const parallelContainers = this . options . containers ;
85- const hasParallelContainers = parallelContainers . every ( ( s ) => visit . containers . includes ( s ) ) ;
86- if ( ! hasParallelContainers ) {
87- console . warn (
88- '[parallel-plugin] Parallel containers not found in list of replaced containers'
89- ) ;
75+ if ( ! this . isParallelVisit ( visit ) ) {
9076 return ;
9177 }
9278
9379 // Replace parallel containers ourselves
94- this . parseContainers ( page ) . forEach ( ( { previous, next } ) => {
80+ const containerSets = this . getContainersForVisit ( visit , page ) ;
81+ const parallelContainers = containerSets . map ( ( { selector } ) => selector ) ;
82+ containerSets . forEach ( ( { previous, next } ) => {
9583 this . previousContainers . push ( previous ) ;
9684 this . nextContainers . push ( next ) ;
9785
@@ -122,15 +110,42 @@ export default class SwupParallelPlugin extends Plugin {
122110 this . nextContainers = [ ] ;
123111 } ;
124112
125- parseContainers ( { html } : { html : string } ) : ContainerSet [ ] {
113+ getContainersForVisit ( visit : Visit , { html } : { html : string } ) : ContainerSet [ ] {
114+ const { containers : parallelContainers } = this . options ;
115+ const containersInVisit = parallelContainers . filter ( ( s ) => visit . containers . includes ( s ) ) ;
116+ if ( ! containersInVisit . length ) {
117+ console . warn ( 'No parallel containers found in list of replaced containers' ) ;
118+ return [ ] ;
119+ }
120+
126121 const incomingDocument = new DOMParser ( ) . parseFromString ( html , 'text/html' ) ;
127- return this . options . containers . reduce ( ( containers , selector : string ) => {
122+
123+ return containersInVisit . reduce ( ( containers , selector : string ) => {
128124 const previous = document . querySelector < HTMLElement > ( selector ) ;
129125 const next = incomingDocument . querySelector < HTMLElement > ( selector ) ;
130- return previous && next ? [ ...containers , { previous, next } ] : containers ;
126+ return previous && next ? [ ...containers , { selector , previous, next } ] : containers ;
131127 } , [ ] as ContainerSet [ ] ) ;
132128 }
133129
130+ isParallelVisit ( visit : Visit ) {
131+ return visit . animation . animate && visit . animation . parallel ;
132+ }
133+
134+ markVisitAsParallelAnimation ( visit : Visit ) {
135+ visit . animation . wait = true ;
136+ visit . animation . parallel = true ;
137+ }
138+
139+ visitHasPotentialParallelAnimation ( visit : Visit ) {
140+ // Checking for visit.animation.parallel !== false here allows explicitly
141+ // disabling parallel animations in user hooks before this plugin executes
142+ return (
143+ visit . animation . animate &&
144+ visit . animation . parallel !== false &&
145+ this . visitHasParallelContainers ( visit )
146+ ) ;
147+ }
148+
134149 visitHasParallelContainers ( visit : Visit ) {
135150 return this . options . containers . some ( ( selector ) => {
136151 const container = document . querySelector ( selector ) ;
0 commit comments