Skip to content

A platform for community discussion. Free, open, simple.

UX: strip base url and https/mailto protocol from link toolbar (#33228)

Renato Atilio2362bf740d9d
diff --git a/app/assets/javascripts/discourse/app/components/composer/toolbar-buttons.gjs b/app/assets/javascripts/discourse/app/components/composer/toolbar-buttons.gjs
index 6b16faa0207..ed8c5c7b06b 100644
--- a/app/assets/javascripts/discourse/app/components/composer/toolbar-buttons.gjs
+++ b/app/assets/javascripts/discourse/app/components/composer/toolbar-buttons.gjs
@@ -1,11 +1,9 @@
import Component from "@glimmer/component";
-import { fn, hash } from "@ember/helper";
-import { on } from "@ember/modifier";
+import { hash } from "@ember/helper";
import { action } from "@ember/object";
import { eq } from "truth-helpers";
import DButton from "discourse/components/d-button";
import concatClass from "discourse/helpers/concat-class";
-import icon from "discourse/helpers/d-icon";
import ToolbarPopupMenuOptions from "select-kit/components/toolbar-popup-menu-options";
export default class ComposerToolbarButtons extends Component {
@@ -14,21 +12,6 @@ export default class ComposerToolbarButtons extends Component {
return button === this.firstButton ? 0 : button.tabindex;
}
- @action
- getHref(button) {
- return typeof button.href === "function" ? button.href() : button.href;
- }
-
- @action
- getLabel(button) {
- return typeof button.label === "function" ? button.label() : button.label;
- }
-
- @action
- getIcon(button) {
- return typeof button.icon === "function" ? button.icon() : button.icon;
- }
-
get firstButton() {
const { isFirst = true } = this.args;
return (
@@ -48,56 +31,32 @@ export default class ComposerToolbarButtons extends Component {
{{#if (button.condition @data.context)}}
{{#if (eq button.type "separator")}}
<div class="toolbar-separator"></div>
+ {{else if button.popupMenu}}
+ <ToolbarPopupMenuOptions
+ @content={{(button.popupMenu.options)}}
+ @onChange={{button.popupMenu.action}}
+ @onOpen={{button.action}}
+ @tabindex={{this.tabIndex button}}
+ @onKeydown={{this.rovingButtonBar}}
+ @options={{hash icon=button.icon focusAfterOnChange=false}}
+ class={{button.className}}
+ />
{{else}}
- {{#let
- (this.getHref button) (this.getLabel button) (this.getIcon button)
- as |href label buttonIcon|
- }}
- {{#if href}}
- <a
- href={{href}}
- target="_blank"
- rel="noopener noreferrer"
- class={{concatClass
- "btn no-text btn-icon toolbar-link"
- button.className
- }}
- title={{button.title}}
- tabindex={{this.tabIndex button}}
- {{on "keydown" this.rovingButtonBar}}
- >
- {{#if label}}
- <span title={{label}} class="toolbar-link__label">
- {{label}}
- </span>
- {{/if}}
- {{#if buttonIcon}}
- {{icon buttonIcon}}
- {{/if}}
- </a>
- {{else if button.popupMenu}}
- <ToolbarPopupMenuOptions
- @content={{(button.popupMenu.options)}}
- @onChange={{button.popupMenu.action}}
- @onOpen={{fn button.action button}}
- @tabindex={{this.tabIndex button}}
- @onKeydown={{this.rovingButtonBar}}
- @options={{hash icon=buttonIcon focusAfterOnChange=false}}
- class={{button.className}}
- />
- {{else}}
- <DButton
- @action={{fn button.action button}}
- @translatedTitle={{button.title}}
- @label={{label}}
- @icon={{buttonIcon}}
- @preventFocus={{button.preventFocus}}
- @onKeyDown={{this.rovingButtonBar}}
- tabindex={{this.tabIndex button}}
- class={{button.className}}
- />
- {{/if}}
- {{/let}}
+ <DButton
+ @href={{button.href}}
+ @action={{unless button.href button.action}}
+ @translatedTitle={{button.title}}
+ @label={{button.label}}
+ @translatedLabel={{button.translatedLabel}}
+ @disabled={{button.disabled}}
+ @icon={{button.icon}}
+ @preventFocus={{button.preventFocus}}
+ @onKeyDown={{this.rovingButtonBar}}
+ tabindex={{this.tabIndex button}}
+ class={{concatClass "toolbar-link" button.className}}
+ rel={{if button.href "noopener noreferrer"}}
+ target={{if button.href "_blank"}}
+ />
{{/if}}
{{/if}}
{{/each}}
diff --git a/app/assets/javascripts/discourse/app/components/d-editor.gjs b/app/assets/javascripts/discourse/app/components/d-editor.gjs
index aacacf10f3d..86d4d1201e4 100644
--- a/app/assets/javascripts/discourse/app/components/d-editor.gjs
+++ b/app/assets/javascripts/discourse/app/components/d-editor.gjs
@@ -492,20 +492,6 @@ export default class DEditor extends Component {
};
}
- @action
- toolbarButton(button) {
- if (this.disabled) {
- return;
- }
-
- const toolbarEvent = this.newToolbarEvent(button.trimLeading);
- if (button.sendAction) {
- return button.sendAction(toolbarEvent);
- } else {
- button.perform(toolbarEvent);
- }
- }
-
@action
showLinkModal(toolbarEvent) {
if (this.disabled) {
diff --git a/app/assets/javascripts/discourse/app/lib/composer/toolbar.js b/app/assets/javascripts/discourse/app/lib/composer/toolbar.js
index 61b856cf0f2..c883f340e8c 100644
--- a/app/assets/javascripts/discourse/app/lib/composer/toolbar.js
+++ b/app/assets/javascripts/discourse/app/lib/composer/toolbar.js
@@ -18,36 +18,56 @@ export class ToolbarBase {
this.capabilities = opts.capabilities || {};
}
+ /**
+ * @param {Object} buttonAttrs
+ * @param {string=} buttonAttrs.id
+ * @param {string=} buttonAttrs.group
+ * @param {string=} buttonAttrs.tabindex
+ * @param {string=} buttonAttrs.className
+ * @param {string=} buttonAttrs.label
+ * @param {string=} buttonAttrs.icon
+ * @param {string=} buttonAttrs.href
+ * @param {Function=} buttonAttrs.action
+ * @param {Function=} buttonAttrs.perform
+ * @param {boolean=} buttonAttrs.trimLeading
+ * @param {boolean=} buttonAttrs.popupMenu
+ * @param {boolean=} buttonAttrs.preventFocus
+ * @param {Function=} buttonAttrs.condition
+ * @param {Function=} buttonAttrs.sendAction
+ * @param {Function=} buttonAttrs.shortcutAction custom shortcut action
+ * @param {boolean=} buttonAttrs.hideShortcutInTitle hide shortcut in title
+ * @param {string=} buttonAttrs.title
+ * @param {string=} buttonAttrs.shortcut
+ * @param {boolean=} buttonAttrs.unshift
+ * @param {boolean=} buttonAttrs.disabled
+ */
addButton(buttonAttrs) {
const g = this.groups.findBy("group", buttonAttrs.group || DEFAULT_GROUP);
- const createdButton = {
- id: buttonAttrs.id,
- tabindex: buttonAttrs.tabindex || "-1",
- className: buttonAttrs.className || buttonAttrs.id,
- label: buttonAttrs.label,
- icon: buttonAttrs.icon,
- href: buttonAttrs.href,
- action: (button) => {
- buttonAttrs.action
- ? buttonAttrs.action(button)
- : this.context.send("toolbarButton", button);
-
- // appEvents is only available on the main toolbar
- // only custom plugins listen to this event
- this.context.appEvents?.trigger(
- "d-editor:toolbar-button-clicked",
- button
- );
- },
- perform: buttonAttrs.perform || function () {},
- trimLeading: buttonAttrs.trimLeading,
- popupMenu: buttonAttrs.popupMenu,
- preventFocus: buttonAttrs.preventFocus || false,
- condition: buttonAttrs.condition || (() => true),
- sendAction: buttonAttrs.sendAction,
- shortcutAction: buttonAttrs.shortcutAction, // (optional) custom shortcut action
- hideShortcutInTitle: buttonAttrs.hideShortcutInTitle || false, // (optional) hide shortcut in title
+ const createdButton = Object.defineProperties(
+ {},
+ Object.getOwnPropertyDescriptors(buttonAttrs)
+ );
+
+ createdButton.tabindex ||= "-1";
+ createdButton.className ||= buttonAttrs.id;
+ createdButton.condition ||= () => true;
+
+ createdButton.action = () => {
+ const toolbarEvent = this.context.newToolbarEvent?.(
+ buttonAttrs.trimLeading
+ );
+
+ const actionFn =
+ buttonAttrs.action ?? buttonAttrs.sendAction ?? buttonAttrs.perform;
+ actionFn?.(toolbarEvent);
+
+ // appEvents is only available on the main toolbar
+ // only custom plugins listen to this event
+ this.context.appEvents?.trigger(
+ "d-editor:toolbar-button-clicked",
+ createdButton
+ );
};
const title = i18n(buttonAttrs.title || `composer.${buttonAttrs.id}_title`);
@@ -64,8 +84,6 @@ export class ToolbarBase {
this.shortcuts[
`${PLATFORM_KEY_MODIFIER}+${buttonAttrs.shortcut}`.toLowerCase()
] = createdButton;
- } else {
- createdButton.title = title;
}
if (buttonAttrs.unshift) {
diff --git a/app/assets/javascripts/discourse/app/static/prosemirror/extensions/link-toolbar.js b/app/assets/javascripts/discourse/app/static/prosemirror/extensions/link-toolbar.js
index a81d7f77392..31af3458177 100644
--- a/app/assets/javascripts/discourse/app/static/prosemirror/extensions/link-toolbar.js
+++ b/app/assets/javascripts/discourse/app/static/prosemirror/extensions/link-toolbar.js
@@ -10,6 +10,7 @@ import { updatePosition } from "float-kit/lib/update-position";
const AUTO_LINKS = ["autolink", "linkify"];
const MENU_OFFSET = 12;
+const STRIP_PROTOCOLS = /^(mailto:|https:\/\/)/;
class LinkToolbar extends ToolbarBase {
constructor(opts = {}) {
@@ -43,17 +44,39 @@ class LinkToolbar extends ToolbarBase {
action: opts.unlinkText,
});
- this.addSeparator({ condition: () => opts.canVisit() || opts.canUnlink() });
+ this.addSeparator({
+ condition: () => opts.canVisit() || opts.canUnlink(),
+ });
this.addButton({
id: "link-visit",
- icon: () => (opts.canVisit() ? "up-right-from-square" : null),
+ get icon() {
+ return opts.canVisit() ? "up-right-from-square" : null;
+ },
title: "composer.link_toolbar.visit",
className: "composer-link-toolbar__visit",
preventFocus: true,
condition: () => opts.canVisit() || opts.canUnlink(),
- href: () => (opts.canVisit() ? opts.getHref() : null),
- label: () => (opts.canUnlink() ? opts.getHref() : null),
+ get href() {
+ return opts.canVisit() ? opts.getHref() : null;
+ },
+ get translatedLabel() {
+ if (opts.canUnlink()) {
+ const label = opts.getHref();
+
+ // strip base url from label
+ const origin = window.location.origin;
+ if (label.startsWith(origin)) {
+ return label.replace(origin, "");
+ }
+
+ // strip protocol from label if mailto or https
+ return label.replace(STRIP_PROTOCOLS, "");
+ }
+ },
+ get disabled() {
+ return !opts.canVisit();
+ },
});
}
}
diff --git a/app/assets/stylesheets/common/rich-editor/rich-editor.scss b/app/assets/stylesheets/common/rich-editor/rich-editor.scss
index 9b1c29131af..884ee76687b 100644
--- a/app/assets/stylesheets/common/rich-editor/rich-editor.scss
+++ b/app/assets/stylesheets/common/rich-editor/rich-editor.scss
@@ -211,7 +211,7 @@
color: var(--primary-medium);
}
- &:hover {
+ &:hover:not(:disabled) {
color: var(--primary);
.d-icon {
@@ -230,7 +230,7 @@
.composer-link-toolbar {
&__visit {
- .toolbar-link__label {
+ .d-button-label {
max-width: 12rem;
}
}
@@ -239,12 +239,19 @@
.composer-link-toolbar {
&__visit {
- display: flex;
+ min-width: 0;
align-items: center;
gap: 0.5rem;
- min-width: 0;
- .toolbar-link__label {
+ // visit link has a right-side icon
+ flex-direction: row-reverse;
+
+ .d-icon {
+ // overrides the default from d-button.scss
+ margin-right: 0;
+ }
+
+ .d-button-label {
font-size: var(--font-down-1-rem);
overflow: hidden;
text-overflow: ellipsis;
diff --git a/spec/system/composer/prosemirror_editor_spec.rb b/spec/system/composer/prosemirror_editor_spec.rb
index 2d2cfd57d37..4027dfe43b1 100644
--- a/spec/system/composer/prosemirror_editor_spec.rb
+++ b/spec/system/composer/prosemirror_editor_spec.rb
@@ -928,7 +928,7 @@ describe "Composer - ProseMirror editor", type: :system do
expect(page).to have_css("[data-identifier='composer-link-toolbar']")
expect(page).to have_css("button.composer-link-toolbar__edit")
expect(page).to have_css("button.composer-link-toolbar__copy")
- expect(page).to have_css("a.composer-link-toolbar__visit", text: "https://example.com")
+ expect(page).to have_css("a.composer-link-toolbar__visit", text: "example.com")
end
it "allows editing a link via toolbar" do
@@ -1009,7 +1009,22 @@ describe "Composer - ProseMirror editor", type: :system do
expect(page).to have_css(
"a.composer-link-toolbar__visit[href='https://example.com']",
- text: "https://example.com",
+ text: "example.com",
+ )
+ end
+
+ it "strips base URL from internal links in toolbar display" do
+ open_composer_and_toggle_rich_editor
+
+ internal_link = "#{Discourse.base_url}/t/some-topic/123"
+
+ composer.type_content("[Internal Link](#{internal_link})")
+ composer.send_keys(:left, :left, :left)
+
+ expect(page).to have_css("[data-identifier='composer-link-toolbar']")
+ expect(page).to have_css(
+ "a.composer-link-toolbar__visit[href='#{internal_link}']",
+ text: "/t/some-topic/123",
)
end
@@ -1031,7 +1046,7 @@ describe "Composer - ProseMirror editor", type: :system do
composer.send_keys(:left)
expect(page).to have_css("[data-identifier='composer-link-toolbar']")
- expect(page).to have_css("a.composer-link-toolbar__visit", text: "https://example.com")
+ expect(page).to have_css("a.composer-link-toolbar__visit", text: "example.com")
composer.send_keys(:right)