-
Notifications
You must be signed in to change notification settings - Fork 90
Open links with ctrl/cmd+click #976
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
Merged
+126
−1
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| import { defineExtension } from "lexical" | ||
| import { IS_APPLE, mergeRegister } from "@lexical/utils" | ||
| import { registerEventListener } from "../helpers/listener_helper.js" | ||
| import LexxyExtension from "./lexxy_extension.js" | ||
|
|
||
| export class LinkOpenerExtension extends LexxyExtension { | ||
| get enabled() { | ||
| return this.editorElement.supportsRichText | ||
| } | ||
|
|
||
| get lexicalExtension() { | ||
| return defineExtension({ | ||
| name: "lexxy/link-opener", | ||
| register: () => { | ||
| return mergeRegister( | ||
| registerEventListener(window, "keydown", this.#update.bind(this)), | ||
| registerEventListener(window, "keyup", this.#update.bind(this)), | ||
| registerEventListener(window, "blur", this.#disable.bind(this)), | ||
| registerEventListener(window, "focus", this.#refresh.bind(this)) | ||
| ) | ||
| } | ||
| }) | ||
| } | ||
|
|
||
| #update(event) { | ||
| if (this.#isModified(event)) { | ||
| this.#enable() | ||
| } else { | ||
| this.#disable() | ||
| } | ||
| } | ||
|
|
||
| #refresh() { | ||
| // Chrome dispatches events without modifier keys *for a while* after changing tabs | ||
| setTimeout(() => { | ||
| window.addEventListener("mousemove", this.#update.bind(this), { once: true }) | ||
| }, 200) | ||
|
zachasme marked this conversation as resolved.
|
||
| } | ||
|
zachasme marked this conversation as resolved.
|
||
|
|
||
| #isModified(event) { | ||
| return IS_APPLE ? event.metaKey : event.ctrlKey | ||
| } | ||
|
|
||
| #enable() { | ||
| for (const anchor of this.#anchors) { | ||
| anchor.setAttribute("contenteditable", "false") | ||
| anchor.setAttribute("target", "_blank") | ||
| anchor.setAttribute("rel", "noopener noreferrer") | ||
| } | ||
| } | ||
|
|
||
| #disable() { | ||
| for (const anchor of this.#anchors) { | ||
| anchor.removeAttribute("contenteditable") | ||
| anchor.removeAttribute("target") | ||
| anchor.removeAttribute("rel") | ||
| } | ||
| } | ||
|
|
||
| get #anchors() { | ||
| return this.editorElement.editorContentElement?.querySelectorAll("a") ?? [] | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| import { test } from "../../test_helper.js" | ||
| import { expect } from "@playwright/test" | ||
|
|
||
| const modifier = process.platform === "darwin" ? "Meta" : "Control" | ||
|
|
||
| test.describe("Link opener", () => { | ||
| test.beforeEach(async ({ page, editor }) => { | ||
| await page.goto("/") | ||
| await editor.waitForConnected() | ||
| await editor.setValue('<p>Visit <a href="https://example.com">example</a> today</p>') | ||
| await editor.flush() | ||
| }) | ||
|
|
||
| test("holding modifier makes links non-editable", async ({ page, editor }) => { | ||
| const anchor = editor.content.locator("a") | ||
|
|
||
| await expect(anchor).not.toHaveAttribute("contenteditable") | ||
| await page.keyboard.down(modifier) | ||
| await expect(anchor).toHaveAttribute("contenteditable", "false") | ||
| await page.keyboard.up(modifier) | ||
| await expect(anchor).not.toHaveAttribute("contenteditable") | ||
| }) | ||
|
|
||
| test("holding modifier sets target and rel on links", async ({ page, editor }) => { | ||
| const anchor = editor.content.locator("a") | ||
|
|
||
| await page.keyboard.down(modifier) | ||
| await expect(anchor).toHaveAttribute("target", "_blank") | ||
| await expect(anchor).toHaveAttribute("rel", "noopener noreferrer") | ||
| await page.keyboard.up(modifier) | ||
| await expect(anchor).not.toHaveAttribute("target") | ||
| await expect(anchor).not.toHaveAttribute("rel") | ||
| }) | ||
|
|
||
| test("applies to all links in the editor", async ({ editor, page }) => { | ||
| await editor.setValue( | ||
| '<p><a href="https://a.com">first</a> and <a href="https://b.com">second</a></p>', | ||
| ) | ||
| await editor.flush() | ||
|
|
||
| const anchors = editor.content.locator("a") | ||
|
|
||
| await page.keyboard.down(modifier) | ||
| await expect(anchors.nth(0)).toHaveAttribute("contenteditable", "false") | ||
| await expect(anchors.nth(1)).toHaveAttribute("contenteditable", "false") | ||
| await page.keyboard.up(modifier) | ||
| await expect(anchors.nth(0)).not.toHaveAttribute("contenteditable") | ||
| await expect(anchors.nth(1)).not.toHaveAttribute("contenteditable") | ||
| }) | ||
|
|
||
| test("clears link attributes on window blur", async ({ page, editor }) => { | ||
| const anchor = editor.content.locator("a") | ||
|
|
||
| await page.keyboard.down(modifier) | ||
| await expect(anchor).toHaveAttribute("contenteditable", "false") | ||
|
|
||
| await page.evaluate(() => window.dispatchEvent(new Event("blur"))) | ||
| await expect(anchor).not.toHaveAttribute("contenteditable") | ||
| }) | ||
| }) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.