Skip to content
This repository has been archived by the owner on Aug 21, 2019. It is now read-only.

Commit

Permalink
Prevent adding block attributes to text for inserted files
Browse files Browse the repository at this point in the history
  • Loading branch information
javan committed May 29, 2018
1 parent 38bb5a9 commit 10657b4
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
6 changes: 4 additions & 2 deletions src/trix/models/composition.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -113,15 +113,17 @@ class Trix.Composition extends Trix.BasicObject
@insertAttachment(attachment)

insertFiles: (files) ->
attributes = @getCurrentTextAttributes()
text = new Trix.Text
for file in files when @delegate?.compositionShouldAcceptFile(file)
attachment = Trix.Attachment.attachmentForFile(file)
attachmentText = Trix.Text.textForAttachmentWithAttributes(attachment, @currentAttributes)
attachmentText = Trix.Text.textForAttachmentWithAttributes(attachment, attributes)
text = text.appendText(attachmentText)
@insertText(text)

insertAttachment: (attachment) ->
text = Trix.Text.textForAttachmentWithAttributes(attachment, @currentAttributes)
attributes = @getCurrentTextAttributes()
text = Trix.Text.textForAttachmentWithAttributes(attachment, attributes)
@insertText(text)

deleteInDirection: (direction) ->
Expand Down
20 changes: 19 additions & 1 deletion test/src/system/attachment_test.coffee
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{after, assert, clickElement, defer, dragToCoordinates, moveCursor, pressKey, test, testGroup, triggerEvent, typeCharacters} = Trix.TestHelpers
{after, assert, clickElement, clickToolbarButton, createFile, defer, dragToCoordinates, moveCursor, pressKey, test, testGroup, triggerEvent, typeCharacters} = Trix.TestHelpers

testGroup "Attachments", template: "editor_with_image", ->
test "moving an image by drag and drop", (expectDocument) ->
Expand Down Expand Up @@ -67,6 +67,24 @@ testGroup "Attachments", template: "editor_with_image", ->
assert.locationRange index: 0, offset: 3
expectDocument "ab#{Trix.OBJECT_REPLACEMENT_CHARACTER}\n"

testGroup "File insertion", template: "editor_empty", ->
test "inserting a file in a formatted block", (expectDocument) ->
clickToolbarButton attribute: "bullet", ->
clickToolbarButton attribute: "bold", ->
getComposition().insertFile(createFile())
assert.blockAttributes([0, 1], ["bulletList", "bullet"])
assert.textAttributes([0, 1], bold: true)
expectDocument("#{Trix.OBJECT_REPLACEMENT_CHARACTER}\n")

test "inserting a files in a formatted block", (expectDocument) ->
clickToolbarButton attribute: "quote", ->
clickToolbarButton attribute: "italic", ->
getComposition().insertFiles([createFile(), createFile()])
assert.blockAttributes([0, 2], ["quote"])
assert.textAttributes([0, 1], italic: true)
assert.textAttributes([1, 2], italic: true)
expectDocument("#{Trix.OBJECT_REPLACEMENT_CHARACTER}#{Trix.OBJECT_REPLACEMENT_CHARACTER}\n")

getFigure = ->
findElement("figure")

Expand Down

0 comments on commit 10657b4

Please sign in to comment.