@@ -14,7 +14,7 @@ internal class TextSplitterViewModel : INotifyPropertyChanged, ITimelineToolView
1414 {
1515 private Timeline ? _timeline ;
1616 private UndoRedoManager ? _undoRedoManager ;
17- private IItem ? _selectedItem ;
17+ private IReadOnlyList < IItem > _selectedItems = [ ] ;
1818
1919 public ICommand SplitTextCommand { get ; }
2020
@@ -29,27 +29,43 @@ public TextSplitterViewModel()
2929 private void SplitText ( )
3030 {
3131 if ( _timeline is null ) return ;
32- if ( _selectedItem is null ) return ;
3332 if ( _undoRedoManager is null ) return ;
33+ if ( _selectedItems . Count == 0 ) return ;
3434
35+ var settings = TextSplitterSettings . Default ;
36+
37+ var itemsToSplit = _selectedItems
38+ . Where ( item => item is TextItem || item is VoiceItem )
39+ . ToList ( ) ;
40+
41+ if ( itemsToSplit . Count == 0 ) return ;
42+
43+ foreach ( var item in itemsToSplit )
44+ {
45+ SplitSingleItem ( item , settings , _timeline ) ;
46+ }
47+
48+ _undoRedoManager . Record ( ) ;
49+ }
50+
51+ private static void SplitSingleItem ( IItem selectedItem , TextSplitterSettings settings , Timeline timeline )
52+ {
3553 string ? textToSplit = null ;
36- if ( _selectedItem is TextItem textItem )
54+ if ( selectedItem is TextItem textItem )
3755 {
3856 textToSplit = textItem . Text ;
3957 }
40- else if ( _selectedItem is VoiceItem voiceItem )
58+ else if ( selectedItem is VoiceItem voiceItem )
4159 {
4260 textToSplit = voiceItem . Serif ;
4361 }
4462
4563 if ( string . IsNullOrEmpty ( textToSplit ) ) return ;
4664
47- var settings = TextSplitterSettings . Default ;
48-
49- int startFrame = _selectedItem . Frame ;
65+ int startFrame = selectedItem . Frame ;
5066 int startLayer = settings . IsDeleteOriginalItem
51- ? _selectedItem . Layer
52- : _selectedItem . Layer + 1 ;
67+ ? selectedItem . Layer
68+ : selectedItem . Layer + 1 ;
5369
5470 var itemsToAdd = new List < IItem > ( ) ;
5571 IEnumerable < string > textElements ;
@@ -78,24 +94,24 @@ private void SplitText()
7894 int remainder = 0 ;
7995 if ( settings . IsKeepLength && settings . SplitDirection == SplitDirection . Horizontal && totalSplitItems > 0 )
8096 {
81- baseNewLength = _selectedItem . Length / totalSplitItems ;
82- remainder = _selectedItem . Length % totalSplitItems ;
97+ baseNewLength = selectedItem . Length / totalSplitItems ;
98+ remainder = selectedItem . Length % totalSplitItems ;
8399 }
84100
85101 int i = 0 ;
86102 int currentFrame = ( settings . SplitDirection == SplitDirection . Horizontal && ! settings . IsDeleteOriginalItem )
87- ? startFrame + _selectedItem . Length
103+ ? startFrame + selectedItem . Length
88104 : startFrame ;
89105 int targetStartFrame = currentFrame ;
90106
91107 foreach ( string textElement in validTextElements )
92108 {
93- var newItem = _selectedItem . GetClone ( ) ;
109+ var newItem = selectedItem . GetClone ( ) ;
94110 if ( newItem is TextItem newTextItem )
95111 {
96112 newTextItem . Text = textElement ;
97113 }
98- else if ( newItem is VoiceItem newVoiceItem )
114+ else if ( newItem is VoiceItem newVoiceItem )
99115 {
100116 newVoiceItem . Serif = textElement ;
101117 }
@@ -132,79 +148,75 @@ private void SplitText()
132148
133149 if ( settings . IsDeleteOriginalItem )
134150 {
135- _timeline . DeleteItems ( [ _selectedItem ] ) ;
136- _timeline . TryAddItems ( itemsArray , targetStartFrame , startLayer ) ;
151+ timeline . DeleteItems ( [ selectedItem ] ) ;
152+ timeline . TryAddItems ( itemsArray , targetStartFrame , startLayer ) ;
137153 }
138154 else
139155 {
140- _timeline . TryAddItems ( itemsArray , targetStartFrame , startLayer ) ;
156+ timeline . TryAddItems ( itemsArray , targetStartFrame , startLayer ) ;
141157 }
142-
143- _undoRedoManager . Record ( ) ;
144158 }
145159
146160 private bool CanSplitText ( )
147161 {
148- string ? text = null ;
149- if ( _selectedItem is TextItem textItem )
162+ return _selectedItems . Any ( item =>
150163 {
151- text = textItem . Text ;
152- }
153- else if ( _selectedItem is VoiceItem voiceItem )
154- {
155- text = voiceItem . Serif ;
156- }
157-
158- return ! string . IsNullOrEmpty ( text ) ;
164+ if ( item is TextItem textItem )
165+ {
166+ return ! string . IsNullOrEmpty ( textItem . Text ) ;
167+ }
168+ if ( item is VoiceItem voiceItem )
169+ {
170+ return ! string . IsNullOrEmpty ( voiceItem . Serif ) ;
171+ }
172+ return false ;
173+ } ) ;
159174 }
160175
161176 public void SetTimelineToolInfo ( TimelineToolInfo info )
162177 {
163178 if ( _timeline != null ) _timeline . PropertyChanged -= Timeline_PropertyChanged ;
164- if ( _selectedItem is INotifyPropertyChanged oldItem )
165- {
166- oldItem . PropertyChanged -= SelectedItem_PropertyChanged ;
167- }
179+ UpdateSelectedItems ( [ ] ) ;
168180
169- _selectedItem = null ;
170181 _timeline = info . Timeline ;
171182 _undoRedoManager = info . UndoRedoManager ;
172183
173184 if ( _timeline != null )
174185 {
175186 _timeline . PropertyChanged += Timeline_PropertyChanged ;
176- UpdateSelectedItem ( _timeline . SelectedItem ) ;
187+ UpdateSelectedItems ( _timeline . SelectedItems ) ;
177188 }
178189 }
179190
180191 private void Timeline_PropertyChanged ( object ? sender , PropertyChangedEventArgs e )
181192 {
182- if ( e . PropertyName == nameof ( Timeline . SelectedItem ) )
193+ if ( e . PropertyName == nameof ( Timeline . SelectedItems ) )
183194 {
184- UpdateSelectedItem ( _timeline ? . SelectedItem ) ;
195+ UpdateSelectedItems ( _timeline ? . SelectedItems ?? [ ] ) ;
185196 }
186197 }
187198
188- private void UpdateSelectedItem ( IItem ? newItem )
199+ private void UpdateSelectedItems ( IReadOnlyList < IItem > newItems )
189200 {
190- if ( _selectedItem is INotifyPropertyChanged oldItem )
201+ foreach ( var oldItem in _selectedItems )
191202 {
192- oldItem . PropertyChanged -= SelectedItem_PropertyChanged ;
203+ if ( oldItem is INotifyPropertyChanged oldINotifyPropertyChanged )
204+ {
205+ oldINotifyPropertyChanged . PropertyChanged -= SelectedItem_PropertyChanged ;
206+ }
193207 }
194208
195- if ( newItem is TextItem newTextItem )
196- {
197- _selectedItem = newTextItem ;
198- newTextItem . PropertyChanged += SelectedItem_PropertyChanged ;
199- }
200- else if ( newItem is VoiceItem newVoiceItem )
201- {
202- _selectedItem = newVoiceItem ;
203- newVoiceItem . PropertyChanged += SelectedItem_PropertyChanged ;
204- }
205- else
209+ _selectedItems = newItems ;
210+
211+ foreach ( var newItem in _selectedItems )
206212 {
207- _selectedItem = null ;
213+ if ( newItem is TextItem || newItem is VoiceItem )
214+ {
215+ if ( newItem is INotifyPropertyChanged newINotifyPropertyChanged )
216+ {
217+ newINotifyPropertyChanged . PropertyChanged += SelectedItem_PropertyChanged ;
218+ }
219+ }
208220 }
209221
210222 ( SplitTextCommand as ActionCommand ) ? . RaiseCanExecuteChanged ( ) ;
0 commit comments