// The format bar's pressed state is the selection's: a mark is "on" when // the selected range carries it, or, at a cursor, when the marks there // (or the stored ones) do. import test from 'node:test' import assert from 'node:assert/strict' import { EditorState, TextSelection } from 'prosemirror-state' import { schema } from './schema.mjs' import { markActive, shouldLetGo, actsOn, blockOf } from './format-bar.mjs' function state () { const p = schema.nodes.paragraph.create(null, [ schema.text('plain '), schema.text('bold', [schema.marks.b.create()]) ]) return EditorState.create({ schema, doc: schema.nodes.doc.create(null, [p]) }) } test('a range carrying the mark is pressed; one without is not', () => { const s = state() const bold = s.apply(s.tr.setSelection(TextSelection.create(s.doc, 8, 11))) assert.equal(markActive(bold, schema.marks.b), true) assert.equal(markActive(bold, schema.marks.i), false) const plain = s.apply(s.tr.setSelection(TextSelection.create(s.doc, 1, 5))) assert.equal(markActive(plain, schema.marks.b), false) }) test('at a cursor, the stored marks decide', () => { const s = state() const at = s.apply(s.tr.setSelection(TextSelection.create(s.doc, 3))) assert.equal(markActive(at, schema.marks.b), false) const stored = at.apply(at.tr.addStoredMark(schema.marks.b.create())) assert.equal(markActive(stored, schema.marks.b), true) }) test('focus moving to another place to type lets go; to a button keeps the view', () => { const bar = { contains: n => n.inBar === true } const el = (tagName, attrs = {}, extra = {}) => ({ tagName, getAttribute: k => attrs[k] ?? null, isContentEditable: false, ...extra }) assert.equal(shouldLetGo(null, bar), false, 'the window losing focus') assert.equal(shouldLetGo(el('BUTTON', {}, { inBar: true }), bar), false, 'the bar itself') assert.equal(shouldLetGo(el('INPUT', { type: 'checkbox' }), bar), false, 'the notes switch, on the way to the bar') assert.equal(shouldLetGo(el('INPUT', { type: 'radio' }), bar), false, 'the view switcher') assert.equal(shouldLetGo(el('BUTTON'), bar), false, 'any other button') assert.equal(shouldLetGo(el('INPUT', { type: 'text' }), bar), true, 'the inspector Title') assert.equal(shouldLetGo(el('INPUT'), bar), true, 'an input with no type') assert.equal(shouldLetGo(el('SELECT'), bar), true, 'a select') assert.equal(shouldLetGo(el('DIV', {}, { isContentEditable: true }), bar), true, 'another text') }) test('the whole card is the bar: its own fields keep the view', () => { const card = { contains: n => n.inCard === true } const el = (tagName, attrs = {}, extra = {}) => ({ tagName, getAttribute: k => attrs[k] ?? null, isContentEditable: false, ...extra }) assert.equal(shouldLetGo(el('SELECT', {}, { inCard: true }), card), false, 'a select in the card, on the way to the marks') assert.equal(shouldLetGo(el('INPUT', { type: 'text' }, { inCard: true }), card), false, 'a text field in the card') assert.equal(shouldLetGo(el('INPUT', { type: 'text' }), card), true, 'the title, outside the card') }) test('the marks act on the box the card shows, and on no other', () => { const a = { where: 'stage', block: 'A' } assert.equal(actsOn(a, { where: 'stage', block: 'A' }), true, 'the caret in the box the card shows') assert.equal(actsOn(a, { where: 'stage', block: 'B' }), false, 'another box chosen from the list: not the text the caret was in') assert.equal(actsOn(a, { where: 'notes' }), false, 'the card on the notes, the caret last in a box') assert.equal(actsOn({ where: 'notes', block: '' }, { where: 'notes' }), true, 'the caret in the notes') assert.equal(actsOn({ where: 'notes', block: '' }, { where: 'stage', block: 'A' }), false, 'a box shown, the caret last in the notes') assert.equal(actsOn(a, null), false, 'the card on the slide') assert.equal(actsOn(null, { where: 'stage', block: 'A' }), false, 'no text has had the caret') assert.equal(actsOn({ where: 'stage', block: '' }, { where: 'stage', block: '' }), false, 'a view in no box') }) test("a view's box is the nearest block around it: a table cell's is the table", () => { const table = { getAttribute: k => (k === 'data-id' ? 'T' : null) } const cell = { closest: sel => (sel === '[slides-block][data-id]' ? table : null) } assert.equal(blockOf(cell), 'T') assert.equal(blockOf({ closest: () => null }), '', 'the notes are in no box') assert.equal(blockOf(null), '') })