// fixtures/format-bar.mjs — the text marks as buttons (editor.html's // [slides-format], the inspector card's Text group): Bold, Italic, // Code, Highlight and Link, over the text the cursor is in — any block // on the stage, or the notes. // // Nothing here is a second way to format. Each mark button runs the // keymap's own command (text-keys.mjs: toggleMark for b, i, c and h; // Mod-k's link dialog for href) on the view that last had focus, so a // button and its key cannot disagree, and a code mark still passes the // font gate's plugin like a typed one. The buttons take no focus on // press (mousedown is prevented), so the selection they act on is the // one the person made. // // Each view the stage binds carries plugin(where): it tells the bar when // the view takes focus and whenever its state changes, which is what keeps // aria-pressed true to the selection. A view that is destroyed (the // slide changed, a resync) lets go, and the bar disables until the next // view takes the cursor. So does focus moving to another place to type // outside the card (shouldLetGo); focus moving to a button, or to // anything in the card the bar lives in — reached by Tab on the way to // the marks — keeps it, so the bar works from a keyboard. The card's // own fields still take focus as any field does: the bar only goes on // acting on the text it had. // // where is 'stage' for a block's view and 'notes' for the notes': a // view taking focus calls onCaret(where, block), block being the id of // the box its text is in (a table cell's is the table's), '' for the // notes. That is how the card knows the caret went into the notes, and // how a box whose text takes focus — by a click, Tab, or anything else — // becomes the selection (editor-entry.js, inspect.mjs). // // The marks act on the box the card is showing and on nothing else // (target(), the entry's: {where: 'notes'}, {where: 'stage', block}, or // null). The view the bar holds must be that box's text, or the notes' // when the card is on the notes; otherwise the marks are disabled, and a // press does nothing. Without this, a box selected with the caret // elsewhere showed its own options while Bold went on formatting the // text the caret was last in. sync() is called whenever the target // changes. import { Plugin } from 'prosemirror-state' import { schema } from './schema.mjs' const KEY = { b: 'Mod-b', i: 'Mod-i', c: 'Mod-e', h: 'Mod-Shift-h', href: 'Mod-k' } /** * Whether focus moving to `next` should end the bar's hold on the view * it is leaving: yes for another place to type (a field, a select, an * editable text outside the bar), whose selection is not the view's; * no for anything else, the bar's own buttons and the controls a * keyboard passes on the way to them included. */ export function shouldLetGo (next, bar) { if (!next || (bar && bar.contains(next))) return false if (next.isContentEditable) return true const tag = (next.tagName || '').toLowerCase() if (tag === 'textarea' || tag === 'select') return true if (tag !== 'input') return false const type = (next.getAttribute && next.getAttribute('type') || 'text').toLowerCase() return !['button', 'submit', 'reset', 'checkbox', 'radio', 'image', 'color', 'file', 'range'].includes(type) } /** Whether the selection in state carries mark type t. */ export function markActive (state, t) { const { from, $from, to, empty } = state.selection if (empty) return !!t.isInSet(state.storedMarks || $from.marks()) return state.doc.rangeHasMark(from, to, t) } /** * Whether the text the bar holds (at: {where, block}, where the view * that last had focus is) is the target the card shows ({where, * block}, or null): the notes for the notes, the same box for a box. */ export function actsOn (at, target) { if (!at || !target || at.where !== target.where) return false return at.where === 'notes' || (!!at.block && at.block === target.block) } /** The id of the box a view's text is in: '' for none (the notes). */ export function blockOf (dom) { const el = dom && dom.closest ? dom.closest('[slides-block][data-id]') : null return el ? el.getAttribute('data-id') || '' : '' } export function mountFormatBar ({ root, keys, onCaret = () => {}, target = () => null }) { const bar = root.querySelector('[slides-format]') // What counts as the bar for letting go: the whole card it is in. const card = (bar && bar.closest('[slides-inspector]')) || bar let current = null // Where current is: {where, block}. let at = null const acting = () => !!current && actsOn(at, target()) const buttons = bar ? [...bar.querySelectorAll('[data-mark]')] : [] function sync () { const on = acting() for (const b of buttons) { b.disabled = !on const t = schema.marks[b.dataset.mark] b.setAttribute('aria-pressed', String(!!(on && t && markActive(current.state, t)))) } } if (bar) { // Keep the cursor where it is: a press on a button must not move // focus out of the text it is about to format. bar.addEventListener('mousedown', e => { if (e.target.closest('[data-mark]')) e.preventDefault() }) bar.addEventListener('click', e => { const b = e.target.closest('[data-mark]') if (!b || b.disabled || !acting()) return const view = current const command = keys[KEY[b.dataset.mark]] if (!command) return command(view.state, view.dispatch, view) if (b.dataset.mark !== 'href') view.focus() }) } sync() return { sync, plugin (where = 'stage') { return new Plugin({ view: v => ({ update (view) { if (view === current) sync() }, destroy () { if (current === v) { current = null; at = null; sync() } } }), props: { handleDOMEvents: { focus (view) { const block = where === 'notes' ? '' : blockOf(view.dom) current = view at = { where, block } onCaret(where, block) sync() return false }, // Focus gone to another place to type outside the card (the // title, another text): the bar no longer acts on this // view's old selection. Focus on a button, or anywhere in // the card — Tab on its way to the marks — keeps it. blur (view, e) { if (current === view && shouldLetGo(e.relatedTarget, card)) { current = null at = null sync() } return false } } } }) } } }