Skip to content

Commit 12ba679

Browse files
author
Stanislas Chevallier
committed
Add ability to edit text
1 parent 9cf0b19 commit 12ba679

File tree

3 files changed

+30
-8
lines changed

3 files changed

+30
-8
lines changed

Subtitler/Base.lproj/Main.storyboard

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -262,13 +262,13 @@ CA
262262
<autoresizingMask key="autoresizingMask"/>
263263
<subviews>
264264
<scrollView autohidesScrollers="YES" horizontalLineScroll="18" horizontalPageScroll="10" verticalLineScroll="18" verticalPageScroll="10" usesPredominantAxisScrolling="NO" translatesAutoresizingMaskIntoConstraints="NO" id="Ob9-1O-CVB">
265-
<rect key="frame" x="0.0" y="46" width="380" height="162"/>
265+
<rect key="frame" x="0.0" y="46" width="380" height="78"/>
266266
<clipView key="contentView" id="bus-Px-5FW">
267-
<rect key="frame" x="1" y="1" width="378" height="160"/>
267+
<rect key="frame" x="1" y="1" width="378" height="76"/>
268268
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
269269
<subviews>
270270
<tableView verticalHuggingPriority="750" allowsExpansionToolTips="YES" columnAutoresizingStyle="firstColumnOnly" tableStyle="fullWidth" alternatingRowBackgroundColors="YES" columnReordering="NO" columnResizing="NO" multipleSelection="NO" autosaveColumns="NO" rowHeight="18" usesAutomaticRowHeights="YES" headerView="IK9-0e-Sxy" viewBased="YES" id="8su-3M-rp4">
271-
<rect key="frame" x="0.0" y="0.0" width="378" height="132"/>
271+
<rect key="frame" x="0.0" y="0.0" width="378" height="48"/>
272272
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
273273
<size key="intercellSpacing" width="17" height="0.0"/>
274274
<color key="backgroundColor" name="controlBackgroundColor" catalog="System" colorSpace="catalog"/>
@@ -291,13 +291,14 @@ CA
291291
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
292292
<subviews>
293293
<textField horizontalHuggingPriority="251" verticalHuggingPriority="750" horizontalCompressionResistancePriority="250" verticalCompressionResistancePriority="1000" translatesAutoresizingMaskIntoConstraints="NO" id="XT2-sz-zwD">
294-
<rect key="frame" x="2" y="2" width="142" height="32"/>
295-
<textFieldCell key="cell" allowsUndo="NO" sendsActionOnEndEditing="YES" title="Sample line with very long text" id="oNC-L2-X9N">
294+
<rect key="frame" x="2" y="2" width="142" height="16"/>
295+
<textFieldCell key="cell" selectable="YES" editable="YES" allowsUndo="NO" title="Sample line with very long text" id="oNC-L2-X9N">
296296
<font key="font" usesAppearanceFont="YES"/>
297297
<color key="textColor" name="controlTextColor" catalog="System" colorSpace="catalog"/>
298298
<color key="backgroundColor" name="textBackgroundColor" catalog="System" colorSpace="catalog"/>
299299
</textFieldCell>
300300
<connections>
301+
<action selector="tableViewLineTextFieldChangedWithSender:" target="rPt-NT-nkU" id="tYi-E4-pHc"/>
301302
<binding destination="B9k-E3-fIe" name="value" keyPath="objectValue" id="dpA-F4-Bqz"/>
302303
</connections>
303304
</textField>
@@ -443,7 +444,7 @@ CA
443444
<autoresizingMask key="autoresizingMask"/>
444445
<subviews>
445446
<avPlayerView controlsStyle="inline" translatesAutoresizingMaskIntoConstraints="NO" id="fFl-Ef-s8W" customClass="PlayerView" customModule="Subtitler" customModuleProvider="target">
446-
<rect key="frame" x="0.0" y="112" width="419" height="96"/>
447+
<rect key="frame" x="0.0" y="112" width="419" height="12"/>
447448
</avPlayerView>
448449
<button verticalHuggingPriority="750" springLoaded="YES" imageHugsTitle="YES" translatesAutoresizingMaskIntoConstraints="NO" id="EV0-6E-XRp" customClass="PressButton" customModule="Subtitler" customModuleProvider="target">
449450
<rect key="frame" x="2" y="2" width="415" height="40"/>

Subtitler/Models/Subtitle.swift

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,20 @@ class Subtitle: NSDocument {
119119
return newLineIndex
120120
}
121121

122+
func updateText(for lineIndex: Int, text: String) {
123+
isTransient = false
124+
updateChangeCount(.changeDone)
125+
126+
let previousText = lines[lineIndex].text
127+
lines[lineIndex].text = text.replacingOccurrences(of: "\n\n", with: "\n")
128+
129+
undoManager?.registerUndo(withTarget: self, handler: { selfTarget in
130+
selfTarget.updateText(for: lineIndex, text: previousText)
131+
selfTarget.contentViewController?.updateTableView()
132+
})
133+
undoManager?.setActionName("Update text")
134+
}
135+
122136
func updateTimings(for lineIndex: Int, start: TimeInterval, end: TimeInterval) {
123137
isTransient = false
124138
updateChangeCount(.changeDone)
@@ -135,7 +149,7 @@ class Subtitle: NSDocument {
135149
})
136150
undoManager?.setActionName("Update timings")
137151
}
138-
152+
139153
func move(from originalIndex: Int, to destinationIndex: Int) {
140154
isTransient = false
141155
updateChangeCount(.changeDone)

Subtitler/VCs/ViewController.swift

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import Cocoa
99
import AVKit
1010
import SnapKit
1111

12-
// TODO: allow text editing (timings too ? with validation using binding and value transformer ?)
12+
// TODO: allow timing editing as text ? with validation using binding and value transformer ?
1313

1414
class ViewController: NSViewController {
1515

@@ -265,6 +265,13 @@ extension ViewController: NSTableViewDelegate {
265265
player.seek(to: min(desiredTime, maxTime))
266266
}
267267

268+
@IBAction private func tableViewLineTextFieldChanged(sender: AnyObject?) {
269+
guard let field = sender as? NSTextField else { return }
270+
let row = tableView.row(for: field)
271+
guard row >= 0 else { return }
272+
subtitle.updateText(for: row, text: field.stringValue)
273+
}
274+
268275
// https://samwize.com/2018/11/27/drag-and-drop-to-reorder-nstableview/
269276
func tableView(_ tableView: NSTableView, pasteboardWriterForRow row: Int) -> NSPasteboardWriting? {
270277
let pasteboardItem = NSPasteboardItem()

0 commit comments

Comments
 (0)