Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/3114-send-key.added.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
General → Shortcuts now offers **Shift+Enter to send** (Enter starts a new line) beside the default Enter-to-send; the choice is kept for this window. ([#3114](https://github.com/ThinkFlowLab/FlyBot/issues/3114))
130 changes: 130 additions & 0 deletions core/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -722,6 +722,11 @@ var ZH = {
"Unread marks, badges, and the brand mark.": "未读标记、徽标和品牌标志。",
"Theme accent": "主题强调色",
"Custom accent": "自定义强调色",
Shortcuts: "快捷键",
"Send shortcut": "发送快捷键",
"Enter to send": "Enter 发送",
"Shift+Enter to send": "Shift+Enter 发送",
"Choose which key sends a message; the other one starts a new line.": "选择哪个键发送消息,另一个键换行。",
On: "开",
None: "无",
"Bots · {count}": "Bot · {count}",
Expand Down Expand Up @@ -13150,6 +13155,102 @@ var import_react24 = require("react");
var import_react_dom7 = require("react-dom");
var speechApi = () => window.SpeechRecognition ?? window.webkitSpeechRecognition;
var keepComposerFocus = (event) => event.preventDefault();
var SEND_KEYS = ["enter", "shift-enter"];
var DEFAULT_SEND_KEY = "enter";
var SEND_KEY_STORAGE = "flybot-send-key";
function normalizeSendKey(value) {
return SEND_KEYS.includes(value) ? value : DEFAULT_SEND_KEY;
}
function readSendKey(storage = globalThis.localStorage) {
try {
return normalizeSendKey(storage?.getItem(SEND_KEY_STORAGE));
} catch {
return DEFAULT_SEND_KEY;
}
}
function writeSendKey(value, storage = globalThis.localStorage) {
const key = normalizeSendKey(value);
try {
storage?.setItem(SEND_KEY_STORAGE, key);
} catch {
}
return key;
}
var plainEnter = (event) => {
if (event?.key !== "Enter" || event.metaKey || event.ctrlKey || event.altKey) return false;
return !(event.isComposing || event.nativeEvent?.isComposing || event.keyCode === 229 || event.which === 229);
};
function shouldSendOnKey(event, sendKey) {
if (!plainEnter(event)) return false;
return normalizeSendKey(sendKey) === "shift-enter" ? event.shiftKey === true : event.shiftKey !== true;
}
function shouldBreakOnKey(event, sendKey) {
if (!plainEnter(event)) return false;
return normalizeSendKey(sendKey) === "shift-enter" ? event.shiftKey !== true : event.shiftKey === true;
}
function composerFieldOf(target) {
const field = target?.closest?.('[data-composer-input], textarea, [contenteditable="true"]');
return field ?? null;
}
function composerCardOf(field) {
return field?.closest?.("[data-composer-card]") ?? null;
}
var NOT_SEND = /dictate|mic|voice|add|plus|stop|录音|听写|添加|停止/i;
function findSendButton(card) {
const named = card?.querySelector?.("button[type=submit], button[data-send], [data-composer-send]");
if (named) return named;
const buttons = card?.querySelectorAll?.("button") ?? [];
for (const button of buttons) {
const label = [button.getAttribute?.("aria-label"), button.getAttribute?.("title"), button.textContent].filter(Boolean).join(" ");
if (/send|发送/i.test(label) && !NOT_SEND.test(label)) return button;
}
return null;
}
function insertComposerNewline(field) {
if (!field) return false;
if (typeof field.value === "string" && typeof field.selectionStart === "number") {
const start = field.selectionStart;
const end = field.selectionEnd ?? start;
field.value = `${field.value.slice(0, start)}
${field.value.slice(end)}`;
field.selectionStart = field.selectionEnd = start + 1;
field.dispatchEvent?.(new Event("input", { bubbles: true }));
return true;
}
if (field.isContentEditable !== true && field.getAttribute?.("contenteditable") !== "true") return false;
const doc = field.ownerDocument ?? globalThis.document;
if (typeof doc?.execCommand !== "function") return false;
return doc.execCommand("insertLineBreak") === true || doc.execCommand("insertParagraph") === true || doc.execCommand("insertText", false, "\n") === true;
}
function installSendShortcut({ doc = globalThis.document } = {}) {
const view = doc?.defaultView;
if (!view?.addEventListener) return () => {
};
const onKey = (event) => {
if (event.defaultPrevented) return;
const field = composerFieldOf(event.target);
if (!field || field.disabled || field.readOnly || field.getAttribute?.("aria-readonly") === "true") return;
const card = composerCardOf(field);
if (!card) return;
const sendKey = readSendKey(view.localStorage);
if (sendKey === DEFAULT_SEND_KEY) return;
if (shouldSendOnKey(event, sendKey)) {
const button = findSendButton(card);
if (!button || button.disabled || button.getAttribute?.("aria-disabled") === "true") return;
event.preventDefault();
event.stopPropagation();
button.click?.();
return;
}
if (shouldBreakOnKey(event, sendKey)) {
event.preventDefault();
event.stopPropagation();
insertComposerNewline(field);
}
};
view.addEventListener("keydown", onKey, true);
return () => view.removeEventListener("keydown", onKey, true);
}
function useDictation(onText) {
const [listening, setListening] = (0, import_react24.useState)(false);
const [note, setNote] = (0, import_react24.useState)("");
Expand Down Expand Up @@ -13245,6 +13346,7 @@ function ComposerVoice({ sessionId, inputActions, actions, useUi }) {
selection?.collapseToEnd();
}
}, [quote?.token]);
(0, import_react24.useEffect)(() => installSendShortcut(), []);
if (sessionId === void 0) return null;
const pluginMic = actions.speech() !== void 0;
return (0, import_react24.createElement)(
Expand Down Expand Up @@ -19733,6 +19835,8 @@ var NOTIFY_KINDS = [
["finished", "Finished"],
["failed", "Failed"]
];
var SEND_KEY_LABELS = [["enter", "Enter to send"], ["shift-enter", "Shift+Enter to send"]];
var sendKeyLabel = (key) => SEND_KEY_LABELS.find(([id]) => id === key)?.[1] ?? SEND_KEY_LABELS[0][1];
function PluginsView({ answer = null, error = "", onFlip }) {
const plugins = answer?.plugins ?? [];
const profile = typeof answer?.profile === "string" && answer.profile !== "" ? answer.profile : "<profile>";
Expand Down Expand Up @@ -19799,6 +19903,10 @@ function GeneralPage({ roster, actions, run }) {
const themes = allThemes();
const current = themes[prefs.theme] ? prefs.theme : "deepseek";
const [custom, setCustom] = (0, import_react44.useState)(prefs.accent ?? "#4d6bfe");
const [sendKey, setSendKey] = (0, import_react44.useState)(() => readSendKey());
const chooseSendKey = (id) => {
if (sendKey !== id) setSendKey(writeSendKey(id));
};
const timer = (0, import_react44.useRef)(0);
(0, import_react44.useEffect)(() => () => clearTimeout(timer.current), []);
const pickCustom = (value) => {
Expand All @@ -19815,6 +19923,11 @@ function GeneralPage({ roster, actions, run }) {
(0, import_react44.createElement)(SetRow, { label: t("Theme"), hint: t(themes[current].label) }),
(0, import_react44.createElement)(SetRow, { label: t("Accent"), hint: accent ? t(capitalize2(accent)) : prefs.accent ?? t("Default") })
),
(0, import_react44.createElement)(
SetSection,
{ key: "shortcuts", title: t("Shortcuts") },
(0, import_react44.createElement)(SetRow, { label: t("Send shortcut"), hint: t(sendKeyLabel(sendKey)) })
),
(0, import_react44.createElement)(
SetSection,
{ key: "notify", title: t("Notifications") },
Expand Down Expand Up @@ -19861,6 +19974,23 @@ function GeneralPage({ roster, actions, run }) {
)
)
),
(0, import_react44.createElement)(
SetSection,
{ key: "shortcuts", title: t("Shortcuts") },
(0, import_react44.createElement)(
SetRow,
{
label: t("Send shortcut"),
hint: t("Choose which key sends a message; the other one starts a new line.")
},
(0, import_react44.createElement)(Segmented, { label: t("Send shortcut"), value: sendKey, className: "bt-appearance-seg" }, SEND_KEYS.map((id) => (0, import_react44.createElement)("button", {
key: id,
type: "button",
"aria-pressed": sendKey === id,
onClick: () => chooseSendKey(id)
}, t(sendKeyLabel(id)))))
)
),
// One switch for every system notification (#353), then one per kind (#469).
(0, import_react44.createElement)(
SetSection,
Expand Down
117 changes: 117 additions & 0 deletions core/src/client/composer.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,121 @@ import { lockedWindow } from './schedules.js'
export const speechApi = () => window.SpeechRecognition ?? window.webkitSpeechRecognition
const keepComposerFocus = event => event.preventDefault()

// -------------------------------------------------------------------------
// Which key sends. The shell sends on Enter and keeps Shift+Enter for a new line;
// General → Shortcuts can flip that pair for this window. The choice is local
// (localStorage): the host stores no send-key field, so nothing here writes it.

export const SEND_KEYS = ['enter', 'shift-enter']
export const DEFAULT_SEND_KEY = 'enter'
export const SEND_KEY_STORAGE = 'flybot-send-key'

export function normalizeSendKey(value) {
return SEND_KEYS.includes(value) ? value : DEFAULT_SEND_KEY
}

export function readSendKey(storage = globalThis.localStorage) {
try { return normalizeSendKey(storage?.getItem(SEND_KEY_STORAGE)) } catch { return DEFAULT_SEND_KEY }
}

export function writeSendKey(value, storage = globalThis.localStorage) {
const key = normalizeSendKey(value)
try { storage?.setItem(SEND_KEY_STORAGE, key) } catch { /* storage unavailable */ }
return key
}

// A plain Enter is the only key in play: modifiers stay the shell's, and an IME's
// Enter (composition, or the legacy keyCode 229) never sends or breaks a line.
const plainEnter = (event) => {
if (event?.key !== 'Enter' || event.metaKey || event.ctrlKey || event.altKey) return false
return !(event.isComposing || event.nativeEvent?.isComposing || event.keyCode === 229 || event.which === 229)
}

export function shouldSendOnKey(event, sendKey) {
if (!plainEnter(event)) return false
return normalizeSendKey(sendKey) === 'shift-enter' ? event.shiftKey === true : event.shiftKey !== true
}

export function shouldBreakOnKey(event, sendKey) {
if (!plainEnter(event)) return false
return normalizeSendKey(sendKey) === 'shift-enter' ? event.shiftKey !== true : event.shiftKey === true
}

export function composerFieldOf(target) {
const field = target?.closest?.('[data-composer-input], textarea, [contenteditable="true"]')
return field ?? null
}

export function composerCardOf(field) {
return field?.closest?.('[data-composer-card]') ?? null
}

// The shell's own Send button: the component leaves it without a marker, so its label is
// the only handle. Dictation, the voice button and "+" are not sends.
const NOT_SEND = /dictate|mic|voice|add|plus|stop|录音|听写|添加|停止/i
export function findSendButton(card) {
const named = card?.querySelector?.('button[type=submit], button[data-send], [data-composer-send]')
if (named) return named
const buttons = card?.querySelectorAll?.('button') ?? []
for (const button of buttons) {
const label = [button.getAttribute?.('aria-label'), button.getAttribute?.('title'), button.textContent].filter(Boolean).join(' ')
if (/send|发送/i.test(label) && !NOT_SEND.test(label)) return button
}
return null
}

// Enter starts a new line when Shift+Enter sends. A value field splices one in; the
// shell's Lexical field takes the editing command the browser uses for a line break.
export function insertComposerNewline(field) {
if (!field) return false
if (typeof field.value === 'string' && typeof field.selectionStart === 'number') {
const start = field.selectionStart
const end = field.selectionEnd ?? start
field.value = `${field.value.slice(0, start)}\n${field.value.slice(end)}`
field.selectionStart = field.selectionEnd = start + 1
field.dispatchEvent?.(new Event('input', { bubbles: true }))
return true
}
if (field.isContentEditable !== true && field.getAttribute?.('contenteditable') !== 'true') return false
const doc = field.ownerDocument ?? globalThis.document
if (typeof doc?.execCommand !== 'function') return false
return doc.execCommand('insertLineBreak') === true
|| doc.execCommand('insertParagraph') === true
|| doc.execCommand('insertText', false, '\n') === true
}

// A capture-phase listener, so a line the shell would have sent is stopped before the
// Lexical editor sees it. A window that has no Send to press (a running turn's button
// reads Stop, or is disabled) falls through and keeps the shell's own Enter-to-send.
export function installSendShortcut({ doc = globalThis.document } = {}) {
const view = doc?.defaultView
if (!view?.addEventListener) return () => {}
const onKey = (event) => {
if (event.defaultPrevented) return
const field = composerFieldOf(event.target)
if (!field || field.disabled || field.readOnly || field.getAttribute?.('aria-readonly') === 'true') return
const card = composerCardOf(field)
if (!card) return
const sendKey = readSendKey(view.localStorage)
if (sendKey === DEFAULT_SEND_KEY) return
if (shouldSendOnKey(event, sendKey)) {
const button = findSendButton(card)
if (!button || button.disabled || button.getAttribute?.('aria-disabled') === 'true') return
event.preventDefault()
event.stopPropagation()
button.click?.()
return
}
if (shouldBreakOnKey(event, sendKey)) {
event.preventDefault()
event.stopPropagation()
insertComposerNewline(field)
}
}
view.addEventListener('keydown', onKey, true)
return () => view.removeEventListener('keydown', onKey, true)
}

export function useDictation(onText) {
const [listening, setListening] = useState(false)
const [note, setNote] = useState('')
Expand Down Expand Up @@ -88,6 +203,8 @@ export function ComposerVoice({ sessionId, inputActions, actions, useUi }) {
selection?.collapseToEnd()
}
}, [quote?.token])
// One capture listener per mounted chat; the preference is read at each key press.
useEffect(() => installSendShortcut(), [])
if (sessionId === undefined) return null
// With the Voice Input plugin on, its own microphone sits in this composer.
const pluginMic = actions.speech() !== undefined
Expand Down
5 changes: 5 additions & 0 deletions core/src/client/i18n.js
Original file line number Diff line number Diff line change
Expand Up @@ -663,6 +663,11 @@ const ZH = {
'Unread marks, badges, and the brand mark.': '未读标记、徽标和品牌标志。',
'Theme accent': '主题强调色',
'Custom accent': '自定义强调色',
Shortcuts: '快捷键',
'Send shortcut': '发送快捷键',
'Enter to send': 'Enter 发送',
'Shift+Enter to send': 'Shift+Enter 发送',
'Choose which key sends a message; the other one starts a new line.': '选择哪个键发送消息,另一个键换行。',
On: '开',
None: '无',
'Bots · {count}': 'Bot · {count}',
Expand Down
20 changes: 20 additions & 0 deletions core/src/client/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { useDialogFocus, useEscape } from './overlay-hooks.js'
import { BotSettingsForm, SwitchRow } from './bot-settings.js'
import { Segmented, useGlide } from './motion.js'
import { GroupSettingsForm, NewGroupForm, seatedCount } from './group-settings.js'
import { SEND_KEYS, readSendKey, writeSendKey } from './composer.js'
import { CreateBot } from './create-bot.js'
import { KeyIcon, SecretsPage } from './secret-card.js'
import { UsagePage } from './usage-page.js'
Expand Down Expand Up @@ -47,6 +48,12 @@ const NOTIFY_KINDS = [
['finished', 'Finished'],
['failed', 'Failed'],
]

// Which key sends (General → Shortcuts). The shell sends on Enter and keeps Shift+Enter
// for a new line; the other choice flips that pair. The answer stays in this window
// (composer.js writes localStorage), so the host stores no send-key field.
const SEND_KEY_LABELS = [['enter', 'Enter to send'], ['shift-enter', 'Shift+Enter to send']]
const sendKeyLabel = key => SEND_KEY_LABELS.find(([id]) => id === key)?.[1] ?? SEND_KEY_LABELS[0][1]
// Settings → Plugins (#2564): the four feature plugins, each with one switch. The
// switch writes FlyBot's state.json; the packages stay installed and the next start
// reads the answer, so nothing unloads from the session that flipped it.
Expand Down Expand Up @@ -108,6 +115,8 @@ function GeneralPage({ roster, actions, run }) {
const themes = allThemes()
const current = themes[prefs.theme] ? prefs.theme : 'deepseek'
const [custom, setCustom] = useState(prefs.accent ?? '#4d6bfe')
const [sendKey, setSendKey] = useState(() => readSendKey())
const chooseSendKey = (id) => { if (sendKey !== id) setSendKey(writeSendKey(id)) }
const timer = useRef(0)
useEffect(() => () => clearTimeout(timer.current), [])
const pickCustom = (value) => {
Expand All @@ -123,6 +132,8 @@ function GeneralPage({ roster, actions, run }) {
h(SetSection, { key: 'look', title: t('Appearance') },
h(SetRow, { label: t('Theme'), hint: t(themes[current].label) }),
h(SetRow, { label: t('Accent'), hint: accent ? t(capitalize(accent)) : prefs.accent ?? t('Default') })),
h(SetSection, { key: 'shortcuts', title: t('Shortcuts') },
h(SetRow, { label: t('Send shortcut'), hint: t(sendKeyLabel(sendKey)) })),
h(SetSection, { key: 'notify', title: t('Notifications') },
h(SetRow, { label: t('In other chats'), hint: prefs.notifications !== false ? t('On') : t('Off') }),
NOTIFY_KINDS.map(([kind, label]) => h(SetRow, { key: kind, label: t(label), hint: prefs.notifications !== false && prefs.notify?.[kind] !== false ? t('On') : t('Off') }))),
Expand All @@ -142,6 +153,15 @@ function GeneralPage({ roster, actions, run }) {
})),
h('label', { className: 'bt-swatch bt-swatch-custom', title: t('Custom accent'), 'data-on': Boolean(prefs.accent) && !ACCENT_INKS.some(ink => INKS[ink][0] === prefs.accent) },
h('input', { type: 'color', value: custom, 'aria-label': t('Custom accent'), onChange: event => pickCustom(event.target.value) }))))),
h(SetSection, { key: 'shortcuts', title: t('Shortcuts') },
h(SetRow, {
label: t('Send shortcut'),
hint: t('Choose which key sends a message; the other one starts a new line.'),
},
h(Segmented, { label: t('Send shortcut'), value: sendKey, className: 'bt-appearance-seg' }, SEND_KEYS.map(id => h('button', {
key: id, type: 'button', 'aria-pressed': sendKey === id,
onClick: () => chooseSendKey(id),
}, t(sendKeyLabel(id))))))),
// One switch for every system notification (#353), then one per kind (#469).
h(SetSection, { key: 'notify', title: t('Notifications') },
h(SwitchRow, {
Expand Down
Loading
Loading