A Scripture editor React component that works on USJ Scripture data. A utility that converts USX to USJ is also included. It is expected that data conforms to USJ v3.1.
``mermaid --- title: Scripture Data — Editor flow --- graph TB DB[(DB)] <-- USX --> C C[USX-USJ converter] <-- USJ --> A A[USJ-Editor adapter] <-- Editor State --> Editor `
> [!NOTE] > > - Use the component for an editor without commenting features. > - Use the component for an editor with comments (comments appear in the margin).
`typescript import { EditorOptions, immutableNoteCallerNodeName, Marginal, MarginalRef, usxStringToUsj, UsjNodeOptions } from '@biblionexus-foundation/platform-editor'; import { BookChapterControl } from 'platform-bible-react';
const emptyUsx = ''; const usx =
World English Bible (WEB) The Psalms Blessed is the man who doesn’t walk in the counsel of the wicked, nor stand on the path of sinners, nor sit in the seat of scoffers;
- USJ editor with USX support - Read-only and edit mode - History - undo & redo - Cut, copy, paste, paste as plain text - context menu and keyboard shortcuts - Format block type - change
markers. The current implementation is a proof-of-concept and doesn't have all the markers available yet. - Insert markers - type '\\' (backslash - configurable to another key) for a marker menu. If text is selected first the marker will apply to the selection if possible, e.g. use '\\wj' to "red-letter" selected text. - Add comments to selected text, reply in comment threads, delete comments and threads. - To enable comments use the editor component (comments appear in the margin). - To use the editor without comments use the component. - Add and remove different types of annotations. Style the different annotations types with CSS, e.g. style a spelling annotation with a red squiggly underline. - Get and set the cursor location or selection range. - Specify textDirection as "ltr", "rtl", or "auto" ("auto" is unlikely to be useful for minority languages). - BCV linkage - change the book/chapter/verse externally and the cursor moves; move the cursor and it updates the external book/chapter/verse - Nodes supported , , , , , , - Nodes not yet supported
, , , , , , , - Node options - callback for when a link is clicked
Styling
This npm package does not include styling so you need to style the editor component to suit your application. A good place to start is to copy the CSS from this repo:
type via the editor's reference API (see Editorial Ref). This type can then be used to apply custom CSS styles (e.g., a green squiggly underline for a _"grammar"_ type annotation). The CSS classname for an annotation takes the form of .${annotationPrefix}-${type}, where type is the string you pass to the addAnnotation() method and annotationPrefix is set by config.theme.typedMark (defaults to _"editor-typed-mark"_). If annotations overlap with each other an additional CSS classname is added where annotationPrefix is set by config.theme.typedMarkOverlap (defaults to _"editor-typed-markOverlap"_).
For example, if an annotation of type _"grammar"_ is overlapping it will have both CSS classnames
editor-typed-mark-grammar and editor-typed-markOverlap-grammar. If it's not overlapping it still has the first classname. API
$3
`ts /** * Scripture Editor for USJ. Created for use in Platform. * @see https://github.com/usfm-bible/tcdocs/blob/usj/grammar/usj.js * * @param props.ref - Forward reference for the editor. * @param props.defaultUsj - Initial Scripture data in USJ format. * @param props.scrRef - Scripture reference that links the general cursor location of the Scripture. * @param props.onScrRefChange - Callback function when the Scripture reference changes in the editor as the cursor moves. * @param props.onSelectionChange - Callback function when the cursor selection changes. * @param props.onUsjChange - Callback function when USJ Scripture data has changed. * @param props.options - Options to configure the editor. * @param props.logger - Logger instance. * @returns the editor element. */ `
$3
`ts /* Forward reference for the editor. / export type EditorRef = { /* Focus the editor. / focus(): void; /* Get USJ Scripture data. / getUsj(): Usj | undefined; /* Set the USJ Scripture data. / setUsj(usj: Usj): void; /** * Get the selection location or range. * @returns the selection location or range, or undefined if there is no selection. The * json-path in the selection assumes no comment Milestone nodes are present in the USJ. */ getSelection(): SelectionRange | undefined; /** * Set the selection location or range. * @param selection - A selection location or range. The json-path in the selection assumes no * comment Milestone nodes are present in the USJ. */ setSelection(selection: SelectionRange): void; /** * Add an ephemeral annotation. * @param selection - An annotation range containing the start and end location. The json-path in * an annotation location assumes no comment Milestone nodes are present in the USJ. * @param type - Type of the annotation. * @param id - ID of the annotation. */ addAnnotation(selection: AnnotationRange, type: string, id: string): void; /** * Remove an ephemeral annotation. * @param type - Type of the annotation. * @param id - ID of the annotation. */ removeAnnotation(type: string, id: string): void; /* Ref to the end of the toolbar - INTERNAL USE ONLY to dynamically add controls in the toolbar. / toolbarEndRef: React.RefObject | null; }; `
$3
`ts /* Options to configure the editor. / export type EditorOptions = { /* Is the editor readonly or editable. / isReadonly?: boolean; /* Is the editor enabled for spell checking. / hasSpellCheck?: boolean; /* Text direction: "ltr" | "rtl" | "auto". / textDirection?: TextDirection; /* Key to trigger the marker menu. Defaults to '\'. / markerMenuTrigger?: string; /* View options - EXPERIMENTAL. Defaults to the formatted view mode which is currently the only functional option. / view?: ViewOptions; /** Options for each editor node: * @param nodes.ImmutableNoteCallerNode.noteCallers - Possible note callers to use when caller is * '+'. Defaults to Latin lower case letters. * @param nodes.ImmutableNoteCallerNode.onClick - Click handler method. */ nodes?: UsjNodeOptions; }; ` API
These are the same as Editorial except where noted below. See Editorial API.
`ts /* Forward reference for the editor. / export type MarginalRef = EditorRef & { /* Set the comments to accompany USJ Scripture. / setComments?(comments: Comments): void; }; `
Demo and Collaborative Web Development Environment
Thanks to CodeSandbox for the instant dev environment: https://codesandbox.io/p/github/BiblioNexus-Foundation/scripture-editors/main
This package is the third tab (
dev:platform:5175).
OR
To run the demo app locally, first follow the Developer Quick Start, but instead of running the last step, instead run:
`sh nx dev platform `
Develop in App
To develop an editor in a target application you can use yalc to link the editor in without having to publish to NPM every time something changes.
1. In this monorepo, publish the editor to
yalc, e.g.: `bash nx devpub platform ` 2. In the target application repo, link from yalc: `bash yalc link @biblionexus-foundation/platform-editor ` 3. In this monorepo, make changes and re-publish the editor (see step 1). 4. When you have finished developing in the target application repo, unlink from yalc: `bash yalc remove @biblionexus-foundation/platform-editor && npm i ``