Skip to content

Commit

Permalink
Replace tribute with text-expander-element for textarea (#23985)
Browse files Browse the repository at this point in the history
The completion popup now behaves now much more as expected than before
for the raw textarea:
- You can press <kbd>Tab</kbd> or <kbd>Enter</kbd> once the completion
popup is open to accept the selected item
- The menu does not close automatically when moving the cursor
- When you delete text, previously correct suggestions are shown again
- If you delete all text until the opening char (`@` or `:`) after
applying a suggestion, the popup reappears again
- Menu UI has been improved

<img width="278" alt="Screenshot 2023-04-07 at 19 43 42"
src="https://user-images.githubusercontent.com/115237/230653601-d6517b9f-0988-445e-aa57-5ebfaf5039f3.png">
  • Loading branch information
silverwind authored Apr 9, 2023
1 parent 8bc8ca1 commit 9f6bc7c
Show file tree
Hide file tree
Showing 6 changed files with 172 additions and 5 deletions.
14 changes: 14 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"@citation-js/plugin-software-formats": "0.6.1",
"@claviska/jquery-minicolors": "2.3.6",
"@github/markdown-toolbar-element": "2.1.1",
"@github/text-expander-element": "2.3.0",
"@mcaptcha/vanilla-glue": "0.1.0-alpha-3",
"@primer/octicons": "18.3.0",
"@vue/compiler-sfc": "3.2.47",
Expand Down
4 changes: 3 additions & 1 deletion templates/shared/combomarkdowneditor.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ Template Attributes:
<span class="markdown-toolbar-button markdown-switch-easymde">{{svg "octicon-arrow-switch"}}</span>
</div>
</markdown-toolbar>
<textarea class="markdown-text-editor js-quick-submit" name="{{.TextareaName}}" placeholder="{{.TextareaPlaceholder}}">{{.TextareaContent}}</textarea>
<text-expander keys=": @">
<textarea class="markdown-text-editor js-quick-submit" name="{{.TextareaName}}" placeholder="{{.TextareaPlaceholder}}">{{.TextareaContent}}</textarea>
</text-expander>
</div>
<div class="ui tab markup" data-tab-panel="markdown-previewer">
{{.locale.Tr "loading"}}
Expand Down
63 changes: 63 additions & 0 deletions web_src/css/editor-markdown.css
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,66 @@
.combo-markdown-editor .CodeMirror-scroll {
max-height: calc(100vh - 200px);
}

text-expander {
display: block;
position: relative;
}

text-expander .suggestions {
position: absolute;
min-width: 180px;
padding: 0;
margin-top: 24px;
list-style: none;
background: var(--color-box-body);
border-radius: 5px;
border: 1px solid var(--color-secondary);
box-shadow: 0 .5rem 1rem var(--color-shadow);
}

text-expander .suggestions li {
display: flex;
align-items: center;
cursor: pointer;
padding: 4px 8px;
font-weight: 500;
}

text-expander .suggestions li + li {
border-top: 1px solid var(--color-secondary-alpha-40);
}

text-expander .suggestions li:first-child {
border-radius: 4px 4px 0 0;
}

text-expander .suggestions li:last-child {
border-radius: 0 0 4px 4px;
}

text-expander .suggestions li:only-child {
border-radius: 4px;
}

text-expander .suggestions li:hover {
background: var(--color-hover);
}

text-expander .suggestions .fullname {
font-weight: normal;
margin-left: 4px;
color: var(--color-text-light-1);
}

text-expander .suggestions li[aria-selected="true"],
text-expander .suggestions li[aria-selected="true"] span {
background: var(--color-primary);
color: var(--color-primary-contrast);
}

text-expander .suggestions img {
width: 24px;
height: 24px;
margin-right: 8px;
}
17 changes: 17 additions & 0 deletions web_src/css/form.css
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
.ui.input textarea,
.ui.form textarea,
.ui.form input:not([type]),
.ui.form input[type="date"],
.ui.form input[type="datetime-local"],
.ui.form input[type="email"],
.ui.form input[type="number"],
.ui.form input[type="password"],
.ui.form input[type="search"],
.ui.form input[type="tel"],
.ui.form input[type="time"],
.ui.form input[type="text"],
.ui.form input[type="file"],
.ui.form input[type="url"] {
transition: none;
}

input,
textarea,
.ui.input > input,
Expand Down
78 changes: 74 additions & 4 deletions web_src/js/features/comp/ComboMarkdownEditor.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import '@github/markdown-toolbar-element';
import '@github/text-expander-element';
import $ from 'jquery';
import {attachTribute} from '../tribute.js';
import {hideElem, showElem, autosize} from '../../utils/dom.js';
import {initEasyMDEImagePaste, initTextareaImagePaste} from './ImagePaste.js';
import {initMarkupContent} from '../../markup/content.js';
import {handleGlobalEnterQuickSubmit} from './QuickSubmit.js';
import {attachRefIssueContextPopup} from '../contextpopup.js';
import {emojiKeys, emojiString} from '../emoji.js';

let elementIdCounter = 0;
const maxExpanderMatches = 6;

/**
* validate if the given textarea is non-empty.
Expand Down Expand Up @@ -40,13 +43,10 @@ class ComboMarkdownEditor {

async init() {
this.prepareEasyMDEToolbarActions();

this.setupTab();
this.setupDropzone();

this.setupTextarea();

await attachTribute(this.textarea, {mentions: true, emoji: true});
this.setupExpander();

if (this.userPreferredEditor === 'easymde') {
await this.switchToEasyMDE();
Expand Down Expand Up @@ -83,6 +83,76 @@ class ComboMarkdownEditor {
}
}

setupExpander() {
const expander = this.container.querySelector('text-expander');
expander?.addEventListener('text-expander-change', ({detail: {key, provide, text}}) => {
if (key === ':') {
const matches = [];
for (const name of emojiKeys) {
if (name.includes(text)) {
matches.push(name);
if (matches.length >= maxExpanderMatches) break;
}
}
if (!matches.length) return provide({matched: false});

const ul = document.createElement('ul');
ul.classList.add('suggestions');
for (const name of matches) {
const emoji = emojiString(name);
const li = document.createElement('li');
li.setAttribute('role', 'option');
li.setAttribute('data-value', emoji);
li.textContent = `${emoji} ${name}`;
ul.append(li);
}

provide({matched: true, fragment: ul});
} else if (key === '@') {
const matches = [];
for (const obj of window.config.tributeValues) {
if (obj.key.includes(text)) {
matches.push(obj);
if (matches.length >= maxExpanderMatches) break;
}
}
if (!matches.length) return provide({matched: false});

const ul = document.createElement('ul');
ul.classList.add('suggestions');
for (const {value, name, fullname, avatar} of matches) {
const li = document.createElement('li');
li.setAttribute('role', 'option');
li.setAttribute('data-value', `${key}${value}`);

const img = document.createElement('img');
img.src = avatar;
li.append(img);

const nameSpan = document.createElement('span');
nameSpan.textContent = name;
li.append(nameSpan);

if (fullname && fullname.toLowerCase() !== name) {
const fullnameSpan = document.createElement('span');
fullnameSpan.classList.add('fullname');
fullnameSpan.textContent = fullname;
li.append(fullnameSpan);
}

ul.append(li);
}

provide({matched: true, fragment: ul});
}
});
expander?.addEventListener('text-expander-value', ({detail}) => {
if (detail?.item) {
detail.value = detail.item.getAttribute('data-value');
}
});
}

setupDropzone() {
const dropzoneParentContainer = this.container.getAttribute('data-dropzone-parent-container');
if (dropzoneParentContainer) {
Expand Down

0 comments on commit 9f6bc7c

Please sign in to comment.