oficina / slides Public

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

Plain git — no account needed to clone.

Download

Download this file

1// fixtures/format-bar.mjs — the text marks as buttons (editor.html's
2// [slides-format], the inspector card's Text group): Bold, Italic,
3// Code, Highlight and Link, over the text the cursor is in — any block
4// on the stage, or the notes.
5//
6// Nothing here is a second way to format. Each mark button runs the
7// keymap's own command (text-keys.mjs: toggleMark for b, i, c and h;
8// Mod-k's link dialog for href) on the view that last had focus, so a
9// button and its key cannot disagree, and a code mark still passes the
10// font gate's plugin like a typed one. The buttons take no focus on
11// press (mousedown is prevented), so the selection they act on is the
12// one the person made.
13//
14// Each view the stage binds carries plugin(where): it tells the bar when
15// the view takes focus and whenever its state changes, which is what keeps
16// aria-pressed true to the selection. A view that is destroyed (the
17// slide changed, a resync) lets go, and the bar disables until the next
18// view takes the cursor. So does focus moving to another place to type
19// outside the card (shouldLetGo); focus moving to a button, or to
20// anything in the card the bar lives in — reached by Tab on the way to
21// the marks — keeps it, so the bar works from a keyboard. The card's
22// own fields still take focus as any field does: the bar only goes on
23// acting on the text it had.
24//
25// where is 'stage' for a block's view and 'notes' for the notes': a
26// view taking focus calls onCaret(where, block), block being the id of
27// the box its text is in (a table cell's is the table's), '' for the
28// notes. That is how the card knows the caret went into the notes, and
29// how a box whose text takes focus — by a click, Tab, or anything else —
30// becomes the selection (editor-entry.js, inspect.mjs).
31//
32// The marks act on the box the card is showing and on nothing else
33// (target(), the entry's: {where: 'notes'}, {where: 'stage', block}, or
34// null). The view the bar holds must be that box's text, or the notes'
35// when the card is on the notes; otherwise the marks are disabled, and a
36// press does nothing. Without this, a box selected with the caret
37// elsewhere showed its own options while Bold went on formatting the
38// text the caret was last in. sync() is called whenever the target
39// changes.
40import { Plugin } from 'prosemirror-state'
41import { schema } from './schema.mjs'
42
43const KEY = { b: 'Mod-b', i: 'Mod-i', c: 'Mod-e', h: 'Mod-Shift-h', href: 'Mod-k' }
44
45/**
46 * Whether focus moving to `next` should end the bar's hold on the view
47 * it is leaving: yes for another place to type (a field, a select, an
48 * editable text outside the bar), whose selection is not the view's;
49 * no for anything else, the bar's own buttons and the controls a
50 * keyboard passes on the way to them included.
51 */
52export function shouldLetGo (next, bar) {
53 if (!next || (bar && bar.contains(next))) return false
54 if (next.isContentEditable) return true
55 const tag = (next.tagName || '').toLowerCase()
56 if (tag === 'textarea' || tag === 'select') return true
57 if (tag !== 'input') return false
58 const type = (next.getAttribute && next.getAttribute('type') || 'text').toLowerCase()
59 return !['button', 'submit', 'reset', 'checkbox', 'radio', 'image', 'color', 'file', 'range'].includes(type)
60}
61
62/** Whether the selection in state carries mark type t. */
63export function markActive (state, t) {
64 const { from, $from, to, empty } = state.selection
65 if (empty) return !!t.isInSet(state.storedMarks || $from.marks())
66 return state.doc.rangeHasMark(from, to, t)
67}
68
69/**
70 * Whether the text the bar holds (at: {where, block}, where the view
71 * that last had focus is) is the target the card shows ({where,
72 * block}, or null): the notes for the notes, the same box for a box.
73 */
74export function actsOn (at, target) {
75 if (!at || !target || at.where !== target.where) return false
76 return at.where === 'notes' || (!!at.block && at.block === target.block)
77}
78
79/** The id of the box a view's text is in: '' for none (the notes). */
80export function blockOf (dom) {
81 const el = dom && dom.closest ? dom.closest('[slides-block][data-id]') : null
82 return el ? el.getAttribute('data-id') || '' : ''
83}
84
85export function mountFormatBar ({ root, keys, onCaret = () => {}, target = () => null }) {
86 const bar = root.querySelector('[slides-format]')
87 // What counts as the bar for letting go: the whole card it is in.
88 const card = (bar && bar.closest('[slides-inspector]')) || bar
89 let current = null
90 // Where current is: {where, block}.
91 let at = null
92 const acting = () => !!current && actsOn(at, target())
93
94 const buttons = bar ? [...bar.querySelectorAll('[data-mark]')] : []
95 function sync () {
96 const on = acting()
97 for (const b of buttons) {
98 b.disabled = !on
99 const t = schema.marks[b.dataset.mark]
100 b.setAttribute('aria-pressed', String(!!(on && t && markActive(current.state, t))))
101 }
102 }
103
104 if (bar) {
105 // Keep the cursor where it is: a press on a button must not move
106 // focus out of the text it is about to format.
107 bar.addEventListener('mousedown', e => { if (e.target.closest('[data-mark]')) e.preventDefault() })
108 bar.addEventListener('click', e => {
109 const b = e.target.closest('[data-mark]')
110 if (!b || b.disabled || !acting()) return
111 const view = current
112 const command = keys[KEY[b.dataset.mark]]
113 if (!command) return
114 command(view.state, view.dispatch, view)
115 if (b.dataset.mark !== 'href') view.focus()
116 })
117 }
118 sync()
119
120 return {
121 sync,
122 plugin (where = 'stage') {
123 return new Plugin({
124 view: v => ({
125 update (view) { if (view === current) sync() },
126 destroy () { if (current === v) { current = null; at = null; sync() } }
127 }),
128 props: {
129 handleDOMEvents: {
130 focus (view) {
131 const block = where === 'notes' ? '' : blockOf(view.dom)
132 current = view
133 at = { where, block }
134 onCaret(where, block)
135 sync()
136 return false
137 },
138 // Focus gone to another place to type outside the card (the
139 // title, another text): the bar no longer acts on this
140 // view's old selection. Focus on a button, or anywhere in
141 // the card — Tab on its way to the marks — keeps it.
142 blur (view, e) {
143 if (current === view && shouldLetGo(e.relatedTarget, card)) {
144 current = null
145 at = null
146 sync()
147 }
148 return false
149 }
150 }
151 }
152 })
153 }
154 }
155}
156