Skip to content

Commit f01ac66

Browse files
committed
Improve Paste and Match Style operations in Chrome
Work around https://bugs.chromium.org/p/chromium/issues/detail?id=934448
1 parent 1f3bafa commit f01ac66

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

src/trix/controllers/level_2_input_controller.coffee

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,16 @@ class Trix.Level2InputController extends Trix.InputController
4040
event.preventDefault()
4141
@attachFiles(event.clipboardData.files)
4242

43+
else if pasteEventHasPlainTextOnly(event)
44+
event.preventDefault()
45+
paste =
46+
type: "text/plain"
47+
string: event.clipboardData.getData("text/plain")
48+
@delegate?.inputControllerWillPaste(paste)
49+
@responder?.insertString(paste.string)
50+
@render()
51+
@delegate?.inputControllerDidPaste(paste)
52+
4353
beforeinput: (event) ->
4454
if handler = @inputTypes[event.inputType]
4555
@withEvent(event, handler)
@@ -392,6 +402,11 @@ class Trix.Level2InputController extends Trix.InputController
392402
clipboard.types.length is 1 and
393403
clipboard.files.length >= 1
394404

405+
pasteEventHasPlainTextOnly = (event) ->
406+
if clipboard = event.clipboardData
407+
"text/plain" in clipboard.types and
408+
clipboard.types.length is 1
409+
395410
keyboardCommandFromKeyEvent = (event) ->
396411
command = []
397412
command.push("alt") if event.altKey

test/src/system/level_2_input_test.coffee

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
{assert, after, clickToolbarButton, defer, insertString, isToolbarButtonActive, selectAll, test, testIf, testGroup, triggerEvent, triggerInputEvent} = Trix.TestHelpers
1+
{assert, after, clickToolbarButton, defer, insertString, insertNode, isToolbarButtonActive, selectAll, test, testIf, testGroup, triggerEvent, triggerInputEvent, typeCharacters} = Trix.TestHelpers
22

33
test = ->
44
testIf(Trix.config.input.getLevel() is 2, arguments...)
@@ -160,6 +160,20 @@ testGroup "Level 2 Input", testOptions, ->
160160
assert.equal attachments.length, 0
161161
expectDocument "abc\n"
162162

163+
# "beforeinput" event is not fired for Paste and Match Style operations
164+
# - https://bugs.chromium.org/p/chromium/issues/detail?id=934448
165+
test "Paste and Match Style in Chrome", (expectDocument) ->
166+
done = -> expectDocument("a\n\nb\n\nc\n")
167+
typeCharacters "a\n\n", ->
168+
clipboardData = createDataTransfer("text/plain": "b\n\nc")
169+
pasteEvent = createEvent("paste", {clipboardData})
170+
if document.activeElement.dispatchEvent(pasteEvent)
171+
node = document.createElement("div")
172+
node.innerHTML = """<div>b</div><div><br></div><div>c</div>"""
173+
insertNode(node, done)
174+
else
175+
requestAnimationFrame(done)
176+
163177

164178
createFile = (callback) ->
165179
canvas = document.createElement("canvas")

0 commit comments

Comments
 (0)