diff --git a/KNOWLEDGE_BASE.md b/KNOWLEDGE_BASE.md index c860972480..4acc4fa7b3 100644 --- a/KNOWLEDGE_BASE.md +++ b/KNOWLEDGE_BASE.md @@ -263,6 +263,8 @@ Sources: [translation endpoint](app/Http/Controllers/CommitTranslationController Commit and reply text supports inline code in backticks and fenced code blocks with syntax highlighting, a **Wrap lines** toggle, and a copy control. A title after the language, as in ```` ```php title="app/Models/User.php" ````, labels the block in place of the language. Text outside code supports emoji shortcodes, clickable URLs, `@username` mentions, and `#hashtags`, which open commit search for signed-in members and stay plain text for guests. This is developer-oriented text formatting, not a full rich-text editor. +In commits, replies, quotes, poll options and remotes, `/shrug` and `/tableflip` typed at the start of the text or after a space become `¯\_(ツ)_/¯` and `(╯°□°)╯︵ ┻━┻` once a space or a new line follows. Typing `/` suggests them; in a fresh commit draft they are listed after `/bug` and `/feature`. Code keeps them as typed, and so does a field whose length limit has no room left for the face; it is expanded as soon as there is. They are expanded in the browser: text sent through the API stays as written. + The smiley button in the commit, reply and quote composers opens the same searchable emoji picker as reactions in remotes. Picking an emoji inserts it where the caret was, or over the selected text, and puts the caret right after it. In commits, replies, and remotes, start a line with `>` to render a quote block. Consecutive quoted lines stay together; an unquoted line ends the block. Quotes keep inline code, links, mentions, and emoji formatting. A `>` inside code or in the middle of a line stays literal. @@ -273,7 +275,7 @@ Typing **#123** in a composer looks up that exact issue or pull request in the / Write `color(#ff8a4c)` or `color(#fff)` outside code to show the hex value with a small color swatch. The `color(...)` markup stays in the draft and is hidden in the rendered commit. It accepts 3, 4, 6 or 8 hex digits. A bare `#` followed by a letter is a hashtag. On the web, if the text of a commit, reply, quote, or Remotes message contains a hashtag that could be a color, the composer shows the swatch syntax and offers to apply it. Colors that start with a digit can still show a swatch without the markup: `#000`, `#000000` and `#333333` are colors, while `#123` and `#333` stay plain text. The full-width `#` also works for automatic swatches, as in `#fff`. A swatch written with `color(...)` does not add the commit to a guild. -Sources: [body renderer](resources/js/components/commit-body.tsx), [emoji picker button](resources/js/components/emoji-picker.tsx), [colors](resources/js/lib/colors.ts), [code blocks](resources/js/components/code-block.tsx), [mention suggestions](app/Http/Controllers/MentionSuggestionController.php), [mention parsing](app/Services/Mentions.php). +Sources: [body renderer](resources/js/components/commit-body.tsx), [emoji picker button](resources/js/components/emoji-picker.tsx), [colors](resources/js/lib/colors.ts), [text expansions](resources/js/lib/text-expansions.ts), [code blocks](resources/js/components/code-block.tsx), [mention suggestions](app/Http/Controllers/MentionSuggestionController.php), [mention parsing](app/Services/Mentions.php). ### Draft previews diff --git a/resources/js/components/commit-composer.tsx b/resources/js/components/commit-composer.tsx index 1fd8cc5d82..92ce81bdf5 100644 --- a/resources/js/components/commit-composer.tsx +++ b/resources/js/components/commit-composer.tsx @@ -302,6 +302,7 @@ function CommitComposerBody({ rows={1} value={body} onValueChange={issue.change} + suggestsExpansions={!issue.ownsExpansions} onPaste={ poll === null ? attachments.onPaste : undefined } @@ -390,8 +391,8 @@ function CommitComposerBody({ { - issue.start(kind, ''); + onPick={(command) => { + issue.pick(command); focusBody(id); }} /> diff --git a/resources/js/components/issue-command.tsx b/resources/js/components/issue-command.tsx index 56b0513c75..5af16cd0e6 100644 --- a/resources/js/components/issue-command.tsx +++ b/resources/js/components/issue-command.tsx @@ -1,5 +1,5 @@ import { useHttp, usePage } from '@inertiajs/react'; -import { Bug, Lightbulb, X } from 'lucide-react'; +import { Bug, Lightbulb, Smile, X } from 'lucide-react'; import type { KeyboardEvent } from 'react'; import { useState } from 'react'; import { toast } from 'sonner'; @@ -16,8 +16,12 @@ import { typedCommand, } from '@/lib/issue-commands'; import { hasModKey } from '@/lib/keyboard'; +import type { TextExpansion } from '@/lib/text-expansions'; +import { suggestedExpansions } from '@/lib/text-expansions'; import { cn } from '@/lib/utils'; +export type ComposerCommand = IssueCommand | TextExpansion; + const hintDismissedKey = 'issue-command-hint-dismissed'; function isHintDismissed(): boolean { @@ -57,10 +61,13 @@ export function useIssueCommand({ >({ command: '', body: '' }); const command = issueCommands.find((candidate) => candidate.kind === kind) ?? null; + // While the draft is a lone command, this menu lists the text expansions too, so the field doesn't open its own. + const ownsExpansions = + enabled && kind === null && suggestedExpansions(body).length > 0; // A draft restored from the stash can be a lone slash: the menu only opens while the user is typing in it. - const suggestions = + const suggestions: ComposerCommand[] = enabled && kind === null && !dismissed && focused - ? suggestedCommands(body) + ? [...suggestedCommands(body), ...suggestedExpansions(body)] : []; const hint = enabled && kind === null && !hintDismissed ? hintedCommand(body) : null; @@ -71,6 +78,14 @@ export function useIssueCommand({ updateBody(rest); }; + const pick = (picked: ComposerCommand) => { + if ('kind' in picked) { + start(picked.kind, ''); + } else { + updateBody(`${picked.text} `); + } + }; + const change = (value: string) => { setDismissed(false); const typed = enabled && kind === null ? typedCommand(value) : null; @@ -164,7 +179,7 @@ export function useIssueCommand({ !hasModKey(event) ) { event.preventDefault(); - start(suggestions[selected % suggestions.length].kind, ''); + pick(suggestions[selected % suggestions.length]); return true; } @@ -202,6 +217,7 @@ export function useIssueCommand({ return { command, suggestions, + ownsExpansions, hint, dismissHint: () => { setHintDismissed(true); @@ -217,6 +233,7 @@ export function useIssueCommand({ focus: () => setFocused(true), blur: () => setFocused(false), start, + pick, exit: () => setKind(null), submit, handleKeyDown, @@ -264,9 +281,9 @@ export function IssueCommandSuggestions({ selected, onPick, }: { - suggestions: IssueCommand[]; + suggestions: ComposerCommand[]; selected: number; - onPick: (kind: IssueKind) => void; + onPick: (command: ComposerCommand) => void; }) { return (
{suggestions.map((command, index) => { - const Icon = icons[command.kind]; + const Icon = 'kind' in command ? icons[command.kind] : Smile; return ( ); diff --git a/resources/js/components/mention-textarea.tsx b/resources/js/components/mention-textarea.tsx index aa6a06bfe0..fb35ffca33 100644 --- a/resources/js/components/mention-textarea.tsx +++ b/resources/js/components/mention-textarea.tsx @@ -14,12 +14,17 @@ import type { EmojiSuggestion } from '@/lib/emoji'; import { emojifyInput, searchEmoji } from '@/lib/emoji'; import { issueReferenceBeforeCaret } from '@/lib/issue-reference'; import { suggestionPosition } from '@/lib/suggestion-position'; +import { + expandTextCommands, + expansionCommandBeforeCaret, + expansionsStartingWith, +} from '@/lib/text-expansions'; import { toastWhenThrottled } from '@/lib/throttle'; import { characterCount, cn, laneColor, limitCharacters } from '@/lib/utils'; import type { Organization, OrgMember, User } from '@/types'; type Trigger = { - kind: 'mention' | 'emoji' | 'issue' | 'guild'; + kind: 'mention' | 'emoji' | 'expansion' | 'issue' | 'guild'; start: number; query: string; }; @@ -86,6 +91,7 @@ function triggerAt( suggestsMentions: boolean, suggestsIssues: boolean, suggestsGuilds: boolean, + suggestsExpansions: boolean, ): Trigger | null { if (textarea.selectionStart !== textarea.selectionEnd) { return null; @@ -117,6 +123,14 @@ function triggerAt( }; } + const expansion = suggestsExpansions + ? expansionCommandBeforeCaret(before) + : null; + + if (expansion) { + return { kind: 'expansion', ...expansion }; + } + if (isTypingExplicitColor(before)) { return null; } @@ -263,8 +277,8 @@ function Highlighted({ text, query }: { text: string; query: string }) { /** * A textarea that suggests developers to mention as soon as "@" is typed, right under the "@", unless it doesn't - * suggest mentions, suggests emoji for the ":shortcode" being typed, and turns emoji shortcodes into emoji as they - * are typed. Within an org, it only suggests the org's members. + * suggest mentions, suggests emoji for the ":shortcode" being typed and text expansions for the "/command" being typed, and + * turns both into what they stand for as they are typed. Within an org, it only suggests the org's members. */ export function MentionTextarea({ ref, @@ -276,6 +290,7 @@ export function MentionTextarea({ maxLength, suggestsMentions = true, suggestsIssues = suggestsMentions, + suggestsExpansions = true, org, members, reply, @@ -287,6 +302,7 @@ export function MentionTextarea({ onValueChange: (value: string) => void; suggestsMentions?: boolean; suggestsIssues?: boolean; + suggestsExpansions?: boolean; org?: string; members?: OrgMember[]; reply?: string; @@ -336,6 +352,7 @@ export function MentionTextarea({ suggestsMentions, suggestsIssues, suggestsIssues && !!guilds?.length, + suggestsExpansions, ); if ( @@ -508,6 +525,11 @@ export function MentionTextarea({ const emojiQuery = trigger?.kind === 'emoji' ? trigger.query.toLowerCase() : ''; const emojis = emojiQuery === '' ? [] : searchEmoji(emojiQuery); + // The prop can turn off between a keystroke and the next render, so it is read here too, not only when tracking. + const expansions = + suggestsExpansions && trigger?.kind === 'expansion' + ? expansionsStartingWith(`/${trigger.query}`) + : []; const guildQuery = trigger?.kind === 'guild' ? trigger.query.toLowerCase() : ''; const matchingGuilds = @@ -534,7 +556,9 @@ export function MentionTextarea({ : 0 : trigger?.kind === 'emoji' ? emojis.length - : suggestions.length + organizations.length; + : trigger?.kind === 'expansion' + ? expansions.length + : suggestions.length + organizations.length; const selectedIndex = Math.min(activeIndex, Math.max(0, optionCount - 1)); const isOpen = trigger !== null && @@ -545,8 +569,10 @@ export function MentionTextarea({ ? issue !== null : trigger.kind === 'emoji' ? emojis.length > 0 - : suggestions.length + organizations.length > 0 || - query !== ''); + : trigger.kind === 'expansion' + ? expansions.length > 0 + : suggestions.length + organizations.length > 0 || + query !== ''); const insert = (text: string, separator: string) => { const textarea = textareaRef.current; @@ -569,9 +595,12 @@ export function MentionTextarea({ const after = value.slice(caret); const spacing = /^\s/u.test(after) ? '' : separator; let next = `${value.slice(0, trigger.start)}${text}${spacing}${after}`; + // The caret skips the separator only when the text keeps one: added here, or already there. + let skipped = separator.length; if (maxLength !== undefined && characterCount(next) > maxLength) { next = `${value.slice(0, trigger.start)}${text}${after}`; + skipped = spacing === '' ? skipped : 0; } if (maxLength !== undefined && characterCount(next) > maxLength) { @@ -583,7 +612,7 @@ export function MentionTextarea({ const nextCaret = trigger.kind === 'issue' ? textarea.selectionStart + next.length - value.length - : trigger.start + text.length + separator.length; + : trigger.start + text.length + skipped; onValueChange(next); close(); @@ -633,6 +662,8 @@ export function MentionTextarea({ insert(`#${matchingGuilds[selectedIndex].handle}`, ' '); } else if (trigger?.kind === 'emoji') { selectEmoji(emojis[selectedIndex]); + } else if (trigger?.kind === 'expansion') { + insert(expansions[selectedIndex].text, ' '); } else { select( selectedIndex < suggestions.length @@ -670,10 +701,12 @@ export function MentionTextarea({ : trigger?.kind === 'emoji' ? emojis[selectedIndex] && `${id}-emoji-${emojis[selectedIndex].name}` - : selectedIndex < suggestions.length - ? `${id}-mention-${suggestions[selectedIndex].id}` - : organizations[selectedIndex - suggestions.length] && - `${id}-mention-${organizations[selectedIndex - suggestions.length].handle}`; + : trigger?.kind === 'expansion' + ? `${id}-expansion-${expansions[selectedIndex].name.slice(1)}` + : selectedIndex < suggestions.length + ? `${id}-mention-${suggestions[selectedIndex].id}` + : organizations[selectedIndex - suggestions.length] && + `${id}-mention-${organizations[selectedIndex - suggestions.length].handle}`; const isSearching = current?.key !== key; const listId = @@ -683,7 +716,9 @@ export function MentionTextarea({ ? `${id}-issues` : trigger?.kind === 'emoji' ? `${id}-emojis` - : `${id}-mentions`; + : trigger?.kind === 'expansion' + ? `${id}-expansions` + : `${id}-mentions`; return ( <> @@ -693,10 +728,19 @@ export function MentionTextarea({ value={value} onChange={(event) => { const target = event.currentTarget; - const { text, caret } = emojifyInput( + const emojified = emojifyInput( target.value, target.selectionStart ?? target.value.length, ); + const expanded = expandTextCommands( + emojified.text, + emojified.caret, + ); + const { text, caret } = + maxLength !== undefined && + characterCount(expanded.text) > maxLength + ? emojified + : expanded; const limited = maxLength === undefined @@ -831,6 +875,43 @@ export function MentionTextarea({ + ) : trigger?.kind === 'expansion' ? ( + ) : trigger?.kind === 'emoji' ? (