Skip to content

Commit c58f398

Browse files
committed
VEditorKit: If user insert media at last then automatically make empty text content for continus typing
1 parent 71a9c17 commit c58f398

2 files changed

Lines changed: 20 additions & 10 deletions

File tree

Example/Tests/VEditorNodeSpec.swift

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -200,16 +200,17 @@ class VEditorNodeSpec: QuickSpec {
200200
node.fetchNewContent(mockContent, scope: .last)
201201

202202
expect(node.editorContents.count)
203-
.to(equal(10))
203+
.to(equal(11))
204204
expect(node.editorContents.filter({ $0 is NSAttributedString }).count)
205-
.to(equal(5))
205+
.to(equal(6))
206206
expect(node.editorContents.filter({ $0 is VVideoContent }).count)
207207
.to(equal(2))
208208
expect(node.editorContents.filter({ $0 is VImageContent }).count)
209209
.to(equal(2))
210210
expect(node.editorContents.filter({ $0 is VOpenGraphContent }).count)
211211
.to(equal(1))
212-
expect(node.editorContents.last is VImageContent).to(beTrue())
212+
expect(node.editorContents.last is VImageContent).to(beFalse())
213+
expect(node.editorContents[node.editorContents.count - 2] is VImageContent).to(beTrue())
213214
}
214215

215216
it("should be append contents at last") {
@@ -229,16 +230,17 @@ class VEditorNodeSpec: QuickSpec {
229230
node.fetchNewContents([mockContent, mockContent, mockContent], scope: .last)
230231

231232
expect(node.editorContents.count)
232-
.to(equal(12))
233+
.to(equal(13))
233234
expect(node.editorContents.filter({ $0 is NSAttributedString }).count)
234-
.to(equal(5))
235+
.to(equal(6))
235236
expect(node.editorContents.filter({ $0 is VVideoContent }).count)
236237
.to(equal(5))
237238
expect(node.editorContents.filter({ $0 is VImageContent }).count)
238239
.to(equal(1))
239240
expect(node.editorContents.filter({ $0 is VOpenGraphContent }).count)
240241
.to(equal(1))
241-
expect(node.editorContents.last is VVideoContent).to(beTrue())
242+
expect(node.editorContents.last is VVideoContent).to(beFalse())
243+
expect(node.editorContents[node.editorContents.count - 2] is VVideoContent).to(beTrue())
242244
}
243245

244246
it("should be append content at fisrt") {
@@ -408,16 +410,17 @@ class VEditorNodeSpec: QuickSpec {
408410
node.fetchNewContent(mockContent, scope: .automatic)
409411

410412
expect(node.editorContents.count)
411-
.to(equal(10))
413+
.to(equal(11))
412414
expect(node.editorContents.filter({ $0 is NSAttributedString }).count)
413-
.to(equal(5))
415+
.to(equal(6))
414416
expect(node.editorContents.filter({ $0 is VVideoContent }).count)
415417
.to(equal(3))
416418
expect(node.editorContents.filter({ $0 is VImageContent }).count)
417419
.to(equal(1))
418420
expect(node.editorContents.filter({ $0 is VOpenGraphContent }).count)
419421
.to(equal(1))
420-
expect(node.editorContents.last is VVideoContent).to(beTrue())
422+
expect(node.editorContents[node.editorContents.count - 2] is VVideoContent).to(beTrue())
423+
expect(node.editorContents.last is VVideoContent).to(beFalse())
421424
}
422425
}
423426
}

VEditorKit/Classes/VEditorNode.swift

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -388,10 +388,17 @@ open class VEditorNode: ASDisplayNode, ASTableDelegate, ASTableDataSource {
388388

389389
self.editorContents.insert(contentsOf: contents, at: indexPath.row)
390390

391-
let contentIndexPaths: [IndexPath] = contents.enumerated().map({ index, _ -> IndexPath in
391+
var contentIndexPaths: [IndexPath] = contents.enumerated().map({ index, _ -> IndexPath in
392392
return .init(row: indexPath.row + index, section: indexPath.section)
393393
})
394394

395+
// NOTE: ** automatically append textContent if last is mediaContent **
396+
if !(self.editorContents.last is NSAttributedString),
397+
let lastRow = contentIndexPaths.last?.row {
398+
contentIndexPaths.append(.init(row: lastRow + 1, section: indexPath.section))
399+
self.editorContents.append(NSAttributedString(string: ""))
400+
}
401+
395402
self.tableNode.performBatchUpdates({
396403
self.tableNode.insertRows(at: contentIndexPaths,
397404
with: animated ? .automatic: .none)

0 commit comments

Comments
 (0)