oficina / slides Public

Clone
git clone https://amadan.net/oficina/slides

Plain git — no account needed to clone.

Download

Download this file

1// The format bar's pressed state is the selection's: a mark is "on" when
2// the selected range carries it, or, at a cursor, when the marks there
3// (or the stored ones) do.
4import test from 'node:test'
5import assert from 'node:assert/strict'
6import { EditorState, TextSelection } from 'prosemirror-state'
7import { schema } from './schema.mjs'
8import { markActive, shouldLetGo, actsOn, blockOf } from './format-bar.mjs'
9
10function state () {
11 const p = schema.nodes.paragraph.create(null, [
12 schema.text('plain '),
13 schema.text('bold', [schema.marks.b.create()])
14 ])
15 return EditorState.create({ schema, doc: schema.nodes.doc.create(null, [p]) })
16}
17
18test('a range carrying the mark is pressed; one without is not', () => {
19 const s = state()
20 const bold = s.apply(s.tr.setSelection(TextSelection.create(s.doc, 8, 11)))
21 assert.equal(markActive(bold, schema.marks.b), true)
22 assert.equal(markActive(bold, schema.marks.i), false)
23 const plain = s.apply(s.tr.setSelection(TextSelection.create(s.doc, 1, 5)))
24 assert.equal(markActive(plain, schema.marks.b), false)
25})
26
27test('at a cursor, the stored marks decide', () => {
28 const s = state()
29 const at = s.apply(s.tr.setSelection(TextSelection.create(s.doc, 3)))
30 assert.equal(markActive(at, schema.marks.b), false)
31 const stored = at.apply(at.tr.addStoredMark(schema.marks.b.create()))
32 assert.equal(markActive(stored, schema.marks.b), true)
33})
34
35test('focus moving to another place to type lets go; to a button keeps the view', () => {
36 const bar = { contains: n => n.inBar === true }
37 const el = (tagName, attrs = {}, extra = {}) => ({ tagName, getAttribute: k => attrs[k] ?? null, isContentEditable: false, ...extra })
38 assert.equal(shouldLetGo(null, bar), false, 'the window losing focus')
39 assert.equal(shouldLetGo(el('BUTTON', {}, { inBar: true }), bar), false, 'the bar itself')
40 assert.equal(shouldLetGo(el('INPUT', { type: 'checkbox' }), bar), false, 'the notes switch, on the way to the bar')
41 assert.equal(shouldLetGo(el('INPUT', { type: 'radio' }), bar), false, 'the view switcher')
42 assert.equal(shouldLetGo(el('BUTTON'), bar), false, 'any other button')
43 assert.equal(shouldLetGo(el('INPUT', { type: 'text' }), bar), true, 'the inspector Title')
44 assert.equal(shouldLetGo(el('INPUT'), bar), true, 'an input with no type')
45 assert.equal(shouldLetGo(el('SELECT'), bar), true, 'a select')
46 assert.equal(shouldLetGo(el('DIV', {}, { isContentEditable: true }), bar), true, 'another text')
47})
48
49test('the whole card is the bar: its own fields keep the view', () => {
50 const card = { contains: n => n.inCard === true }
51 const el = (tagName, attrs = {}, extra = {}) => ({ tagName, getAttribute: k => attrs[k] ?? null, isContentEditable: false, ...extra })
52 assert.equal(shouldLetGo(el('SELECT', {}, { inCard: true }), card), false, 'a select in the card, on the way to the marks')
53 assert.equal(shouldLetGo(el('INPUT', { type: 'text' }, { inCard: true }), card), false, 'a text field in the card')
54 assert.equal(shouldLetGo(el('INPUT', { type: 'text' }), card), true, 'the title, outside the card')
55})
56
57test('the marks act on the box the card shows, and on no other', () => {
58 const a = { where: 'stage', block: 'A' }
59 assert.equal(actsOn(a, { where: 'stage', block: 'A' }), true, 'the caret in the box the card shows')
60 assert.equal(actsOn(a, { where: 'stage', block: 'B' }), false, 'another box chosen from the list: not the text the caret was in')
61 assert.equal(actsOn(a, { where: 'notes' }), false, 'the card on the notes, the caret last in a box')
62 assert.equal(actsOn({ where: 'notes', block: '' }, { where: 'notes' }), true, 'the caret in the notes')
63 assert.equal(actsOn({ where: 'notes', block: '' }, { where: 'stage', block: 'A' }), false, 'a box shown, the caret last in the notes')
64 assert.equal(actsOn(a, null), false, 'the card on the slide')
65 assert.equal(actsOn(null, { where: 'stage', block: 'A' }), false, 'no text has had the caret')
66 assert.equal(actsOn({ where: 'stage', block: '' }, { where: 'stage', block: '' }), false, 'a view in no box')
67})
68
69test("a view's box is the nearest block around it: a table cell's is the table", () => {
70 const table = { getAttribute: k => (k === 'data-id' ? 'T' : null) }
71 const cell = { closest: sel => (sel === '[slides-block][data-id]' ? table : null) }
72 assert.equal(blockOf(cell), 'T')
73 assert.equal(blockOf({ closest: () => null }), '', 'the notes are in no box')
74 assert.equal(blockOf(null), '')
75})
76