| index 908dd75..daf315f 100644 |
| --- a/fixtures/toolbar.test.mjs |
| +++ b/fixtures/toolbar.test.mjs |
| @@ -48,7 +48,7 @@ import { schema as basic } from 'prosemirror-schema-basic' |
| import { addListNodes } from 'prosemirror-schema-list' |
| import { alignedParagraph } from './align.mjs' |
| import { withIndent } from './indent.mjs' |
| -import { SIZE_MARKS, SIZES } from './sizes.mjs' |
| +import { SIZE_MARKS, SIZES, sizeMark, sizeForMarks } from './sizes.mjs' |
| import { FONTS } from './fonts.mjs' |
| import { createToolbar } from './toolbar.mjs' |
| import { mountMenu } from './menu.mjs' |
| @@ -132,7 +132,7 @@ const schema = new Schema({ nodes, marks }) |
| // and both hold elements carrying data-docs-command. A single root |
| // would hand the toolbar the menu's items and test something the |
| // browser never does. |
| -function makeDOM() { |
| +function makeDOM({ chips = false } = {}) { |
| const log = [] |
| const bar = [] |
| const menu = [] |
| @@ -149,6 +149,7 @@ function makeDOM() { |
| let disabled = 'disabled' in own |
| let tabIndex = 'tabindex' in own ? Number(own.tabindex) : -1 |
| let value = '' |
| + const listeners = {} |
| const node = { |
| tag, |
| id, |
| @@ -176,7 +177,14 @@ function makeDOM() { |
| querySelector: (sel) => node.children.find((c) => c.matches(sel)) || null, |
| querySelectorAll: (sel) => node.children.filter((c) => c.matches(sel)), |
| closest: () => null, |
| - addEventListener() {}, |
| + // Listeners are kept, so a test can play a click (emit); none of |
| + // the write-counting tests emits anything. |
| + addEventListener(type, fn) { |
| + ;(listeners[type] = listeners[type] || []).push(fn) |
| + }, |
| + emit(type, event = {}) { |
| + for (const fn of listeners[type] || []) fn({ preventDefault() {}, stopPropagation() {}, ...event }) |
| + }, |
| // Focus is recorded as the page's active element, so the roving |
| // tab stop's keyboard handler can be driven here. |
| focus() { |
| @@ -312,6 +320,9 @@ function makeDOM() { |
| SIZES.map((pt) => [pt, String(pt)]), |
| ) |
| for (const name of ['strong', 'em', 'code']) button(name) |
| + // The phone's Format sheet's style chips (views/show.html): buttons |
| + // named by the block_ commands, with a pressed state. |
| + if (chips) for (const name of ['block_paragraph', 'block_heading-2']) button(name) |
| for (const name of ['align_left', 'align_center', 'align_right']) button(name) |
| for (const name of ['bullet_list', 'ordered_list', 'blockquote']) button(name) |
| |
| @@ -364,14 +375,23 @@ function makeDOM() { |
| // to commands called with a null dispatch, which is a contract every |
| // command in the table honours (buildCommands' own comment has the |
| // evening that cost). |
| -function mount(state) { |
| - const dom = makeDOM() |
| - const view = { state, focus() {}, dispatch() {} } |
| +function mount(state, { chips = false, refocus } = {}) { |
| + const dom = makeDOM({ chips }) |
| + // dispatch and focus as ProseMirror's are called: passed on bare, |
| + // so they close over the view rather than trust `this`. |
| + const view = { state, focused: 0 } |
| + view.focus = () => { |
| + view.focused++ |
| + } |
| + view.dispatch = (tr) => { |
| + view.state = view.state.apply(tr) |
| + } |
| let menu = null |
| const toolbar = createToolbar({ |
| root: dom.toolbarRoot, |
| history: dom.historyRoots, |
| schema, |
| + ...(refocus ? { refocus } : {}), |
| getView: () => view, |
| onSync: (v) => { |
| if (menu) menu.sync(v) |
| @@ -386,6 +406,8 @@ function mount(state) { |
| const pluginView = toolbar.plugin.spec.view(view) |
| return { |
| dom, |
| + view, |
| + commands: toolbar.commands, |
| to(next) { |
| view.state = next |
| pluginView.update(view) |
| @@ -593,3 +615,56 @@ test('the arrow keys walk the bar in reading order: the pickers, then the button |
| assert.deepEqual(walked, want) |
| assert.equal(globalThis.document.activeElement.id, want[0], 'ArrowRight from the last control did not wrap to the first') |
| }) |
| + |
| +// The phone's Format sheet (design/phone-web, P2; Fable's review 10 on |
| +// #181). The size steps walk SIZES in order and stop at its ends; called |
| +// with no dispatch they only say whether they would act. |
| +function sizedState(pt) { |
| + const marks = pt === 12 ? [] : [schema.marks[sizeMark(pt)].create()] |
| + const doc = schema.node('doc', null, [schema.node('paragraph', null, [schema.text('Some words', marks)])]) |
| + return EditorState.create({ doc, selection: TextSelection.create(doc, 1, 11) }) |
| +} |
| +const sizeAt = (state) => sizeForMarks(state.doc.firstChild.firstChild.marks.map((m) => m.type.name)) |
| + |
| +test('the size steps walk the ladder one size at a time and stop at its ends', () => { |
| + const { commands } = mount(EditorState.create({ schema })) |
| + const ladder = [...SIZES].sort((a, b) => a - b) |
| + for (const [from, cmd, to] of [[12, 'size_up', 14], [12, 'size_down', 11], [8, 'size_up', 9], [72, 'size_down', 48]]) { |
| + let next = null |
| + assert.equal(commands[cmd](sizedState(from), (tr) => { next = sizedState(from).apply(tr) }), true, `${cmd} from ${from}`) |
| + assert.equal(sizeAt(next), to, `${cmd} from ${from}`) |
| + } |
| + for (const [at, cmd] of [[ladder[0], 'size_down'], [ladder[ladder.length - 1], 'size_up']]) { |
| + let dispatched = false |
| + assert.equal(commands[cmd](sizedState(at), () => { dispatched = true }), false, `${cmd} at ${at}, the ladder's end`) |
| + assert.equal(dispatched, false) |
| + } |
| + // A dry run: the answer, and nothing done. |
| + const state = sizedState(12) |
| + assert.equal(commands.size_up(state), true) |
| + assert.equal(commands.size_up(state, null), true) |
| + assert.equal(sizeAt(state), 12) |
| + assert.equal(commands.size_up(sizedState(72)), false) |
| +}) |
| + |
| +test('a control hands the caret back to the document only where refocus says so', () => { |
| + for (const [refocus, want] of [[() => true, 1], [() => false, 0]]) { |
| + const bar = mount(sizedState(12), { refocus }) |
| + bar.dom.byId('strong').emit('click') |
| + assert.equal(bar.view.focused, want, `refocus() ${refocus()}`) |
| + assert.ok(bar.view.state.doc.firstChild.firstChild.marks.some((m) => m.type.name === 'strong'), 'B did not act') |
| + } |
| +}) |
| + |
| +test('a style chip is pressed for the style the selection is in', () => { |
| + const heading = schema.node('doc', null, [schema.node('heading', { level: 2 }, [schema.text('A heading')])]) |
| + const bar = mount(sizedState(12), { chips: true }) |
| + assert.equal(bar.dom.byId('block_paragraph').getAttribute('aria-pressed'), 'true') |
| + assert.equal(bar.dom.byId('block_heading-2').getAttribute('aria-pressed'), 'false') |
| + bar.to(EditorState.create({ doc: heading, selection: TextSelection.create(heading, 1, 3) })) |
| + assert.equal(bar.dom.byId('block_paragraph').getAttribute('aria-pressed'), 'false') |
| + assert.equal(bar.dom.byId('block_heading-2').getAttribute('aria-pressed'), 'true') |
| + // And a chip applies its style. |
| + bar.dom.byId('block_paragraph').emit('click') |
| + assert.equal(bar.view.state.doc.firstChild.type.name, 'paragraph') |
| +}) |