Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use the target range to insert the replacement text #1131

Merged
merged 1 commit into from
Feb 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions src/test/system/level_2_input_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,15 +139,22 @@ testGroup("Level 2 Input", testOptions, () => {
})

// https://input-inspector.now.sh/profiles/hVXS1cHYFvc2EfdRyTWQ
test("correcting a misspelled word in Chrome", async () => {
test("correcting a misspelled word", async () => {
insertString("onr")
getComposition().setSelectedRange([ 0, 3 ])
await nextFrame()

const inputType = "insertReplacementText"
const dataTransfer = createDataTransfer({ "text/plain": "one" })
const event = createEvent("beforeinput", { inputType, dataTransfer })

const targetRange = document.createRange()
const textNode = getEditorElement().firstElementChild.lastChild
targetRange.setStart(textNode, 0)
targetRange.setEnd(textNode, 3)

const event = createEvent("beforeinput", { inputType, dataTransfer, getTargetRanges: () => [ targetRange ] })
document.activeElement.dispatchEvent(event)

await nextFrame()
expectDocument("one\n")
})
Expand Down
8 changes: 6 additions & 2 deletions src/trix/controllers/level_2_input_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -443,8 +443,12 @@ export default class Level2InputController extends InputController {
},

insertReplacementText() {
this.insertString(this.event.dataTransfer.getData("text/plain"), { updatePosition: false })
this.requestRender()
const replacement = this.event.dataTransfer.getData("text/plain")
const domRange = this.event.getTargetRanges()[0]

this.withTargetDOMRange(domRange, () => {
this.insertString(replacement, { updatePosition: false })
})
},

insertText() {
Expand Down
1 change: 1 addition & 0 deletions src/trix/models/selection_manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export default class SelectionManager extends BasicObject {
this.lockCount = 0
handleEvent("mousedown", { onElement: this.element, withCallback: this.didMouseDown })
}

getLocationRange(options = {}) {
if (options.strict === false) {
return this.createLocationRangeFromDOMRange(getDOMRange())
Expand Down
Loading