Files
outline/shared/editor/queries/isMarkActive.ts
T
Tom MoorandClaude Opus 5 f6ad3cb6e1 Improvements to mobile formatting toolbar (#13533)
* fix: Selection toolbar not displaying on mobile

The mobile branch of FloatingToolbar rendered nothing until `position.visible`
was true, but `usePosition` only reports a visible position once the menu
element has been measured through `menuRef`. On desktop the wrapper is always
mounted (it animates in via opacity) so the ref is populated on first render,
while on mobile the toolbar is the only thing that mounts the ref — leaving it
permanently null and the bar never displayed.

Gate the mobile bar on there being toolbar content instead. Position is
irrelevant on mobile: the bar is fixed to the bottom of the viewport and its
vertical offset is owned by useKeyboardStickyOffset.

Also size the bar with border-box and center its content vertically, so the
declared 60px is the height of the bar rather than 60px of content plus
padding with the buttons pinned to the top.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01GhGyxA663bMFZ1NxBhLAoF

* fix: Keep the mobile selection toolbar visible while editing

On mobile the toolbar is a bar docked above the on-screen keyboard rather than
a floating menu anchored to the selection, but it was only rendered while
something was selected — so it appeared and vanished as the selection came and
went while typing.

The formatting menu was already written for this: `canFormatBlock` shows
headings, quotes, toggles and list indent/outdent precisely when the selection
is empty on mobile, mirroring `canFormatInline` for selected text. That branch
was unreachable, as an empty selection always resolved the toolbar to nothing.

Show the menu whenever the editor is focused on mobile, so the bar carries
block formatting for a bare cursor and swaps to inline formatting on selection.
Visibility is derived at render rather than folded into `activeToolbar` so that
focus moving into the link editor's input cannot tear down a toolbar the user
opened. Focus is threaded to widgets through WidgetProps.

Tapping the bar now also prevents the default action of mousedown, keeping
focus (and the keyboard) in the editor. Without it a tap would blur the editor
and unmount the bar before the tap became a click; fields inside the bar are
excluded so the link editor still takes focus.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01GhGyxA663bMFZ1NxBhLAoF

* fix: Mobile toolbar is not visible

* Update mobile toolbar styling
fix: Does not account for stored marks

---------

Co-authored-by: Claude <noreply@anthropic.com>
2026-08-23 11:20:30 -04:00

93 lines
2.6 KiB
TypeScript

import type { MarkType } from "prosemirror-model";
import type { EditorState } from "prosemirror-state";
import { NodeSelection } from "prosemirror-state";
import type { Primitive } from "utility-types";
import { getMarksBetween } from "./getMarksBetween";
import { getMarkRangeNodeSelection } from "./getMarkRange";
type Options = {
/** Only return match if the range and attrs is exact */
exact?: boolean;
/** If true then mark must contain entire selection */
inclusive?: boolean;
};
const isNodeMarkActive =
(type: MarkType) =>
(state: EditorState): boolean => {
if (!type) {
return false;
}
const { selection } = state;
if (!(selection instanceof NodeSelection)) {
return false;
}
const mark = getMarkRangeNodeSelection(selection, type);
if (!mark) {
return false;
}
return true;
};
const isInlineMarkActive =
(type: MarkType, attrs?: Record<string, Primitive>, options?: Options) =>
(state: EditorState): boolean => {
if (!type) {
return false;
}
const { from, $from, to, empty } = state.selection;
const marksAtCursor = state.storedMarks || $from.marks();
const hasMark = !!(empty
? type.isInSet(marksAtCursor)
: state.doc.rangeHasMark(from, to, type));
if (!hasMark) {
return false;
}
if (attrs || options) {
// A cursor covers no range, so the marks that will apply to the text
// typed there answer the question, and the range options do not apply.
if (empty) {
return marksAtCursor.some(
(mark) =>
mark.type === type &&
(!attrs ||
Object.keys(attrs).every((key) => mark.attrs[key] === attrs[key]))
);
}
const results = getMarksBetween(from, to, state);
return results.some(
({ mark, start, end }) =>
mark.type === type &&
(!attrs ||
Object.keys(attrs).every(
(key) => mark.attrs[key] === attrs[key]
)) &&
(!options?.exact || (start === from && end === to)) &&
(!options?.inclusive || (start <= from && end >= to))
);
}
return true;
};
/**
* Checks if a mark is active in the current selection or not.
*
* @param type The mark type to check.
* @param attrs The attributes to check.
* @param options The options to use.
* @returns A function that checks if a mark is active in the current selection or not.
*/
export const isMarkActive =
(type: MarkType, attrs?: Record<string, Primitive>, options?: Options) =>
(state: EditorState) =>
isInlineMarkActive(type, attrs, options)(state) ||
isNodeMarkActive(type)(state);