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-document-cards-switch.added.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Settings → General now has a **Document cards** switch for the long-document cards; switched off, a long document folds like any other reply instead of showing a card or **Show message text**, and the choice is saved for the whole team ([#3114](https://github.com/ThinkFlowLab/FlyBot/issues/3114)).
22 changes: 17 additions & 5 deletions core/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,9 @@ var ZH = {
Standard: "标准",
"No folding": "不折叠",
"Message folding must be compact, standard or none": "消息折叠必须是紧凑、标准或不折叠",
"Document cards": "文档卡片",
"Show a long document as a card you can read beside the chat.": "长文档显示为卡片,可在对话旁阅读全文。",
"Document cards must be on or off": "文档卡片必须是开或关",
"Open beside the conversation": "在对话旁打开",
"Opened beside the conversation.": "已在对话旁打开。",
"Close document": "关闭文档",
Expand Down Expand Up @@ -7276,12 +7279,13 @@ var FOLD_THRESHOLDS = {
compact: { lines: 4, chars: 240 },
standard: { lines: 12, chars: 800 }
};
var DEFAULT_FOLDING = { mode: "standard" };
var DEFAULT_FOLDING = { mode: "standard", documentCards: true };
var MODE_SET = new Set(FOLD_MODES);
function foldingOf(prefs) {
const raw = prefs?.folding;
if (!raw || typeof raw !== "object" || Array.isArray(raw)) return { ...DEFAULT_FOLDING };
return { mode: MODE_SET.has(raw.mode) ? raw.mode : DEFAULT_FOLDING.mode };
const documentCards = prefs?.documentCards !== false;
if (!raw || typeof raw !== "object" || Array.isArray(raw)) return { ...DEFAULT_FOLDING, documentCards };
return { mode: MODE_SET.has(raw.mode) ? raw.mode : DEFAULT_FOLDING.mode, documentCards };
}
function clipWord(line2, max) {
if (line2.length <= max) return line2;
Expand Down Expand Up @@ -7388,6 +7392,7 @@ function documentCount(text2) {
return Math.max(documentHeadings(text2).length, documentSections(text2).length, 1);
}
function showsDocumentCard(text2, folding) {
if (folding?.documentCards === false) return false;
if (folding?.mode === "none") return false;
return isLongDocument(text2);
}
Expand Down Expand Up @@ -20340,7 +20345,8 @@ function GeneralPage({ roster, actions, run }) {
(0, import_react45.createElement)(
SetSection,
{ key: "folding", title: t("Message folding") },
(0, import_react45.createElement)(SetRow, { label: t("Message folding"), hint: t(foldModeLabel(folding.mode)) })
(0, import_react45.createElement)(SetRow, { label: t("Message folding"), hint: t(foldModeLabel(folding.mode)) }),
(0, import_react45.createElement)(SetRow, { label: t("Document cards"), hint: folding.documentCards ? t("On") : t("Off") })
),
(0, import_react45.createElement)(
SetSection,
Expand Down Expand Up @@ -20422,7 +20428,13 @@ function GeneralPage({ roster, actions, run }) {
if (folding.mode !== id) run(actions.setPrefs({ folding: { mode: id } }));
}
}, t(label))))
)
),
(0, import_react45.createElement)(SwitchRow, {
label: t("Document cards"),
hint: t("Show a long document as a card you can read beside the chat."),
on: folding.documentCards,
onChange: (on) => run(actions.setPrefs({ documentCards: on }))
})
),
// One switch for every system notification (#353), then one per kind (#469).
(0, import_react45.createElement)(
Expand Down
3 changes: 3 additions & 0 deletions core/src/client/i18n.js
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,9 @@ const ZH = {
Standard: '标准',
'No folding': '不折叠',
'Message folding must be compact, standard or none': '消息折叠必须是紧凑、标准或不折叠',
'Document cards': '文档卡片',
'Show a long document as a card you can read beside the chat.': '长文档显示为卡片,可在对话旁阅读全文。',
'Document cards must be on or off': '文档卡片必须是开或关',
'Open beside the conversation': '在对话旁打开',
'Opened beside the conversation.': '已在对话旁打开。',
'Close document': '关闭文档',
Expand Down
9 changes: 6 additions & 3 deletions core/src/client/message-fold.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,16 @@ export const FOLD_THRESHOLDS = {
standard: { lines: 12, chars: 800 },
}

export const DEFAULT_FOLDING = { mode: 'standard' }
export const DEFAULT_FOLDING = { mode: 'standard', documentCards: true }

const MODE_SET = new Set(FOLD_MODES)

export function foldingOf(prefs) {
const raw = prefs?.folding
if (!raw || typeof raw !== 'object' || Array.isArray(raw)) return { ...DEFAULT_FOLDING }
return { mode: MODE_SET.has(raw.mode) ? raw.mode : DEFAULT_FOLDING.mode }
// The card switch sits beside the folding record, not inside it (#3114).
const documentCards = prefs?.documentCards !== false
if (!raw || typeof raw !== 'object' || Array.isArray(raw)) return { ...DEFAULT_FOLDING, documentCards }
return { mode: MODE_SET.has(raw.mode) ? raw.mode : DEFAULT_FOLDING.mode, documentCards }
}

function clipWord(line, max) {
Expand Down Expand Up @@ -150,6 +152,7 @@ export function documentCount(text) {
}

export function showsDocumentCard(text, folding) {
if (folding?.documentCards === false) return false
if (folding?.mode === 'none') return false
return isLongDocument(text)
}
Expand Down
10 changes: 8 additions & 2 deletions core/src/client/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,8 @@ function GeneralPage({ roster, actions, run }) {
h(SetSection, { key: 'shortcuts', title: t('Shortcuts') },
h(SetRow, { label: t('Send shortcut'), hint: t(sendKeyLabel(sendKey)) })),
h(SetSection, { key: 'folding', title: t('Message folding') },
h(SetRow, { label: t('Message folding'), hint: t(foldModeLabel(folding.mode)) })),
h(SetRow, { label: t('Message folding'), hint: t(foldModeLabel(folding.mode)) }),
h(SetRow, { label: t('Document cards'), hint: folding.documentCards ? t('On') : t('Off') })),
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 Down Expand Up @@ -178,7 +179,12 @@ function GeneralPage({ roster, actions, run }) {
h(Segmented, { label: t('Message folding'), value: folding.mode, className: 'bt-appearance-seg' }, FOLD_MODE_LABELS.map(([id, label]) => h('button', {
key: id, type: 'button', 'aria-pressed': folding.mode === id,
onClick: () => { if (folding.mode !== id) run(actions.setPrefs({ folding: { mode: id } })) },
}, t(label)))))),
}, t(label))))),
h(SwitchRow, {
label: t('Document cards'),
hint: t('Show a long document as a card you can read beside the chat.'),
on: folding.documentCards, onChange: on => run(actions.setPrefs({ documentCards: on })),
})),
// One switch for every system notification (#353), then one per kind (#469).
h(SetSection, { key: 'notify', title: t('Notifications') },
h(SwitchRow, {
Expand Down
7 changes: 7 additions & 0 deletions core/src/host/channel.js
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,13 @@ export function install(rt) {
else if (typeof raw === 'object' && !Array.isArray(raw) && FOLD_MODES.has(mode)) state.prefs.folding = { mode }
else throw invalid('Message folding must be compact, standard or none')
}
// Document cards (#3114): a switch of its own at the top of `prefs`, so rewriting the
// folding mode above never clears it. On is the default, so only off is stored.
if (body.documentCards !== undefined) {
if (body.documentCards === null || body.documentCards === true) delete state.prefs.documentCards
else if (body.documentCards === false) state.prefs.documentCards = false
else throw invalid('Document cards must be on or off')
}
return rt.save()
}

Expand Down
16 changes: 16 additions & 0 deletions core/test/install.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,22 @@ test('set-prefs stores the message folding mode and refuses any other one (#3114
assert.equal((await call('set-prefs', { folding: { mode: 'none' } })).value.folding.mode, 'none')
})

// The document-cards switch (#3114) is a team preference of its own, kept beside the folding
// mode rather than inside it: off is stored, on is the absence of the record, and a value no
// page can draw is refused instead of stored.
test('set-prefs stores the document-cards switch and refuses anything else (#3114)', async (t) => {
const { call, stop } = start(TEAM)
t.after(stop)
assert.equal((await call('set-prefs', { documentCards: false })).value.documentCards, false)
assert.equal('documentCards' in (await call('set-prefs', { documentCards: null })).value, false)
assert.equal((await call('set-prefs', { documentCards: false })).value.documentCards, false)
for (const documentCards of ['off', 0, ['false'], { on: false }]) {
assert.deepEqual((await call('set-prefs', { documentCards })).error, { code: 'bot/invalid', message: 'Document cards must be on or off', details: {} })
}
assert.equal((await call('state')).value.prefs.documentCards, false, 'a refusal stores nothing')
assert.equal('documentCards' in (await call('set-prefs', { documentCards: true })).value, false)
})

test('in the Desktop profile read_browser is the last tool and reads through the window', async (t) => {
const { ctx, call, stop } = start(TEAM, { services: { profileContext: { name: 'desktop' } } })
t.after(stop)
Expand Down
41 changes: 41 additions & 0 deletions core/test/message-fold.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ test('#3114: a message over the cap folds, one at the cap stays open', () => {
assert.equal(compact.lines, 4)
assert.equal(standard.lines, 12)
assert.equal(foldingOf(undefined).mode, 'standard')
assert.equal(foldingOf(undefined).documentCards, true, 'the document card is on by default')
assert.equal(foldingOf({ folding: { mode: 'compact' } }).documentCards, true, 'a folding mode alone keeps the card on')
assert.equal(shouldFold(lines(standard.lines), 'standard'), false, '12 lines stay open')
assert.equal(shouldFold(lines(standard.lines + 1), 'standard'), true, '13 lines fold')
assert.equal(shouldFold('x'.repeat(compact.chars), 'compact'), false, '240 characters stay open')
Expand Down Expand Up @@ -215,3 +217,42 @@ test('#3114: the document pane draws the whole text and closes', () => {
close.props.onClick()
assert.equal(closed, 1, 'the pane closes once')
})

// General → Document cards (#3114). Off takes away the card and its Show message text at every
// folding mode and leaves the reply folded by the same cap as any other; on is what the bubble
// drew before the switch existed.
test('#3114: with document cards off a long document folds like any other reply', () => {
const body = runbook()
const seen = (mode, documentCards) => fresh({ text: body, roster: { ...roster, prefs: { folding: { mode }, documentCards } } })
for (const mode of ['compact', 'standard']) {
const off = seen(mode, false)
assert.equal(classed(off, 'bt-doc-card').length, 0, `no card when the switch is off and ${mode}`)
assert.ok(!text(off).includes('Show message text'), `no Show message text when the switch is off and ${mode}`)
assert.equal(toggles(off).length, 1, `${mode} folds the document like any other reply`)
assert.equal(text(toggles(off)[0]), 'Show more', 'and offers Show more')
assert.ok(!text(off).includes('Step 10'), 'the preview stops before the end')
}
const none = seen('none', false)
assert.equal(classed(none, 'bt-doc-card').length, 0, 'no card when the switch is off and nothing folds')
assert.ok(text(none).includes('Step 10'), 'and the whole text is drawn')
// On is today: the card, or the whole text when folding itself is off.
for (const mode of ['standard', 'compact']) {
const on = seen(mode, true)
assert.equal(classed(on, 'bt-doc-card').length, 1, `the card is drawn when the switch is on and ${mode}`)
assert.equal(text(toggles(on)[0]), 'Show message text', 'and the text stays one press away')
}
const open = seen('none', true)
assert.equal(classed(open, 'bt-doc-card').length, 0, 'no card when folding is off')
assert.ok(text(open).includes('Step 10'), 'and the whole text is drawn')
// A streaming reply is whole and cardless whatever the switch says.
for (const documentCards of [true, false]) {
const streaming = fresh({ text: body, streaming: true, roster: { ...roster, prefs: { documentCards } } })
assert.equal(classed(streaming, 'bt-doc-card').length, 0, 'a streaming reply draws no card')
assert.ok(text(streaming).includes('Step 10'), 'and is drawn whole')
}
// A long reply with no headings or sections is not a document: the switch changes nothing.
const plain = fresh({ text: numbered(20), roster: { ...roster, prefs: { documentCards: false } } })
assert.equal(classed(plain, 'bt-doc-card').length, 0, 'a long reply draws no card either way')
assert.equal(toggles(plain).length, 1, 'and folds the same')
assert.equal(text(toggles(plain)[0]), 'Show more', 'with Show more')
})
23 changes: 22 additions & 1 deletion core/test/settings-readonly-pages.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ test('a writable window keeps every page as it is today (#2474)', () => {
const general = mount(baseRoster, 'general', null, 'GeneralPage').nodes
assert.equal(general.filter(node => node.type === 'select').length, 1, 'General keeps its theme select')
assert.equal(general.filter(node => node.type === 'input').length, 1, 'General keeps its colour field')
assert.equal(counts(general), 8, 'General keeps its four switches beside the send-key and folding pickers')
assert.equal(counts(general), 9, 'General keeps its five switches beside the send-key and folding pickers')
assert.equal(general.filter(node => node.type === Segmented && node.props?.label === 'Send shortcut').length, 1, 'General draws the send-key picker')

const bots = mount(baseRoster, 'bots', null, 'BotsPage').nodes
Expand Down Expand Up @@ -295,6 +295,27 @@ test('Settings → General picks the message folding mode and writes it through
assert.ok(text(locked).includes('Compact'), 'and keeps the mode in use as text')
})

// General → Document cards (#3114): the switch writes the team preference through `set-prefs`,
// and the card stays on until a team switches it off. The locked page draws no switch and keeps
// the choice as text, the way the folding mode does above.
test('General draws the document-cards switch and writes it through set-prefs (#3114)', () => {
const written = []
const { nodes } = mount(baseRoster, 'general', null, 'GeneralPage', [], { setPrefs: payload => { written.push(payload); return Promise.resolve(payload) } })
const cards = nodes.filter(node => node.type === SwitchRow && node.props?.label === 'Document cards')
assert.equal(cards.length, 1, 'General draws the document-cards switch')
assert.equal(cards[0].props.on, true, 'the card is on until the team switches it off')
cards[0].props.onChange(false)
// The page runs inside a vm realm, so its own object is compared by value, not by prototype.
assert.deepEqual(JSON.parse(JSON.stringify(written)), [{ documentCards: false }], 'off asks the host to store off')
const off = { ...baseRoster, prefs: { ...baseRoster.prefs, documentCards: false } }
const stored = mount(off, 'general', null, 'GeneralPage').nodes
assert.equal(stored.filter(node => node.type === SwitchRow && node.props?.label === 'Document cards')[0].props.on, false, 'the stored choice is the one drawn')
const locked = mount({ ...off, readOnly: SENTENCE }, 'general', null, 'GeneralPage').nodes
assert.equal(locked.filter(node => node.type === SwitchRow && node.props?.label === 'Document cards').length, 0, 'a locked page draws no document-cards switch')
assert.ok(text(locked).includes('Document cards'), 'and still names it')
assert.ok(text(locked).includes('Off'), 'keeping the choice as text')
})

// The window can be unlocked under a page that is already on screen: the other window holding
// the team closes, the host's read-only sentence goes away, and settings.js keeps the same
// dialog instance (its key is the page and the id, not the lock). A page whose locked view
Expand Down
Loading