Skip to content

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

UX: display URL on rich editor link toolbar (#33188)

Renato Atilio15ec15f2a516
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 28f742e289a..6b16faa0207 100644
--- a/app/assets/javascripts/discourse/app/components/composer/toolbar-buttons.gjs
+++ b/app/assets/javascripts/discourse/app/components/composer/toolbar-buttons.gjs
@@ -19,6 +19,16 @@ export default class ComposerToolbarButtons extends Component {
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 (
@@ -36,41 +46,58 @@ export default class ComposerToolbarButtons extends Component {
{{#each @data.groups key="group" as |group|}}
{{#each group.buttons key="id" as |button|}}
{{#if (button.condition @data.context)}}
- {{#if button.href}}
- <a
- href={{this.getHref button}}
- target="_blank"
- rel="noopener noreferrer"
- class={{concatClass "btn no-text btn-icon" button.className}}
- title={{button.title}}
- tabindex={{this.tabIndex button}}
- {{on "keydown" this.rovingButtonBar}}
- >
- {{icon button.icon}}
- </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=button.icon focusAfterOnChange=false}}
- class={{button.className}}
- />
- {{else if (eq button.type "separator")}}
+ {{#if (eq button.type "separator")}}
<div class="toolbar-separator"></div>
{{else}}
- <DButton
- @action={{fn button.action button}}
- @translatedTitle={{button.title}}
- @label={{button.label}}
- @icon={{button.icon}}
- @preventFocus={{button.preventFocus}}
- @onKeyDown={{this.rovingButtonBar}}
- tabindex={{this.tabIndex button}}
- class={{button.className}}
- />
+ {{#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}}
{{/if}}
{{/if}}
{{/each}}
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 a9c5f105cc2..a81d7f77392 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
@@ -43,16 +43,17 @@ class LinkToolbar extends ToolbarBase {
action: opts.unlinkText,
});
- this.addSeparator({ condition: opts.canVisit });
+ this.addSeparator({ condition: () => opts.canVisit() || opts.canUnlink() });
this.addButton({
id: "link-visit",
- icon: "up-right-from-square",
+ icon: () => (opts.canVisit() ? "up-right-from-square" : null),
title: "composer.link_toolbar.visit",
- className: "btn-flat composer-link-toolbar__visit",
+ className: "composer-link-toolbar__visit",
preventFocus: true,
- href: opts.getHref,
- condition: opts.canVisit,
+ condition: () => opts.canVisit() || opts.canUnlink(),
+ href: () => (opts.canVisit() ? opts.getHref() : null),
+ label: () => (opts.canUnlink() ? opts.getHref() : null),
});
}
}
@@ -141,7 +142,7 @@ class LinkToolbarPluginView {
#displayToolbar() {
if (this.#getContext().capabilities.viewport.sm) {
- this.#showMobileToolbar();
+ this.#showFloatingToolbar();
} else {
this.#getContext().replaceToolbar(this.#linkToolbar);
this.#toolbarReplaced = true;
@@ -233,7 +234,7 @@ class LinkToolbarPluginView {
return !AUTO_LINKS.includes(this.#linkState.markup);
}
- #showMobileToolbar() {
+ #showFloatingToolbar() {
const element = this.#view.domAtPos(this.#linkState.head).node;
const trigger =
element.nodeType === Node.TEXT_NODE ? element.parentElement : element;
diff --git a/app/assets/stylesheets/common/rich-editor/rich-editor.scss b/app/assets/stylesheets/common/rich-editor/rich-editor.scss
index 167e51211e9..9b1c29131af 100644
--- a/app/assets/stylesheets/common/rich-editor/rich-editor.scss
+++ b/app/assets/stylesheets/common/rich-editor/rich-editor.scss
@@ -205,12 +205,15 @@
.btn {
background-color: var(--secondary);
+ color: var(--primary-medium);
.d-icon {
color: var(--primary-medium);
}
&:hover {
+ color: var(--primary);
+
.d-icon {
color: var(--primary);
}
@@ -224,6 +227,30 @@
@include default-focus;
}
}
+
+ .composer-link-toolbar {
+ &__visit {
+ .toolbar-link__label {
+ max-width: 12rem;
+ }
+ }
+ }
+}
+
+.composer-link-toolbar {
+ &__visit {
+ display: flex;
+ align-items: center;
+ gap: 0.5rem;
+ min-width: 0;
+
+ .toolbar-link__label {
+ font-size: var(--font-down-1-rem);
+ overflow: hidden;
+ text-overflow: ellipsis;
+ white-space: nowrap;
+ }
+ }
}
/*********************************************************
diff --git a/spec/system/composer/prosemirror_editor_spec.rb b/spec/system/composer/prosemirror_editor_spec.rb
index 80fd7cfe9f5..0ca412bc613 100644
--- a/spec/system/composer/prosemirror_editor_spec.rb
+++ b/spec/system/composer/prosemirror_editor_spec.rb
@@ -897,7 +897,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")
+ expect(page).to have_css("a.composer-link-toolbar__visit", text: "https://example.com")
end
it "allows editing a link via toolbar" do
@@ -958,6 +958,7 @@ describe "Composer - ProseMirror editor", type: :system do
expect(page).to have_css("[data-identifier='composer-link-toolbar']")
expect(page).to have_no_css("button.composer-link-toolbar__unlink")
+ expect(page).to have_css("a.composer-link-toolbar__visit", text: "")
end
it "doesn't show unlink button for auto-linkified URLs" do
@@ -967,6 +968,7 @@ describe "Composer - ProseMirror editor", type: :system do
expect(page).to have_css("[data-identifier='composer-link-toolbar']")
expect(page).to have_no_css("button.composer-link-toolbar__unlink")
+ expect(page).to have_css("a.composer-link-toolbar__visit", text: "")
end
it "shows visit button for valid URLs" do
@@ -974,9 +976,10 @@ describe "Composer - ProseMirror editor", type: :system do
composer.type_content("[Example](https://example.com)")
- expect(page).to have_css("a.composer-link-toolbar__visit")
-
- expect(find("a.composer-link-toolbar__visit")["href"]).to eq("https://example.com/")
+ expect(page).to have_css(
+ "a.composer-link-toolbar__visit[href='https://example.com']",
+ text: "https://example.com",
+ )
end
it "doesn't show visit button for invalid URLs" do
@@ -997,6 +1000,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")
composer.send_keys(:right)