@eventuras/scribo-chords

Songs with chords for editors built on @eventuras/scribo-core: chords above the lyrics, sections, transposing, capo, H or B, ChordPro in and out, a reader and a play mode.

It is an extension. The core knows nothing about music; an editor that wants songs adds this package, and one that does not never loads it. Lexical is pinned to exactly the version the core uses (0.50.0 today).

bash
pnpm add @eventuras/scribo-core @eventuras/scribo-chords

The entries

ImportWhat it isNeeds
@eventuras/scribo-chords/songThe song model in plain TypeScript: chords, transposing, capo, notation, ChordPro, chords over lyrics, a song as text.Nothing. No Lexical, no React, no DOM.
@eventuras/scribo-chordsThe Lexical extension: the song and chord nodes, typing [Am], commands, toolbar items, a song in Markdown.lexical, the core. Runs without a DOM.
@eventuras/scribo-chords/markdownCHORDPRO_TRANSFORMER, the Markdown transformer of a song, for a transformer list of your own.@lexical/markdown, for its type.
@eventuras/scribo-chords/reactThe chord chip an editor draws its chords with, a chord palette, section buttons, view controls, SongReader and PlayMode.react, react-dom, @lexical/react.
@eventuras/scribo-chords/style.cssThe styles for all of the above.

react, react-dom, @lexical/react and @lexical/markdown are optional peer dependencies: install them when you use the entry that needs them. A server that only reads and writes songs needs none of them, and neither do the types of the root entry: its declarations name no Markdown module, and a test in the package keeps it so.

The song model, without an editor

ts
import {
formatChordPro,
formatSongText,
parseChordPro,
viewSong,
} from "@eventuras/scribo-chords/song";
const song = parseChordPro(`{title: Morning shift}
{key: G}
{start_of_verse: Verse 1}
[G]Coffee steams on the [D]quay
{end_of_verse}`);
// Two semitones up, in Nordic notation. The song itself is not changed.
const shown = viewSong(song, { transpose: 2, capo: 0, notation: "nordic" });
formatSongText(shown); // chords on their own lines above the lyrics
formatChordPro(song); // back to ChordPro, as stored

A Song is a title, some facts (key, tempo, …) and sections of lines; a line is a list of segments, each a chord and the text that follows it.

  • Stored chords are English names at sounding pitch: B is B natural, Bb is B flat. A leading * makes an annotation (*Slowly), which is shown as text and never transposed.
  • Transpose, capo and notation are view settings. viewSong, shownChord and shownKey give what to show; nothing is written back. The shown chord is the stored chord moved by transpose − capo semitones.
  • Notation is english (B and Bb), nordic (H and B) or mixed (H and Bb, the common habit in Norway and the default).
  • ChordPro: parseChordPro and formatChordPro cover titles and facts, verse, chorus, bridge and tab environments, comments, {chorus} and inline [chords]. Directives they do not know are kept and written back. Lines outside an environment form one unlabelled section, and the blank lines between them are kept as empty lines. A tab is kept as written: brackets in it are text, in the editor too. A literal bracket in lyrics is written with a backslash before it ([sic]); ChordPro has no agreed escape, so this is the package's own. parseChordsOverLyrics reads the other common form, chords on their own lines above the lyrics.

An editor without React

ts
import { BaseExtension, buildEditorFromExtensions, defineExtension } from "@eventuras/scribo-core";
import { ChordsExtension, getSong, setSong, songOf, stateOfSong } from "@eventuras/scribo-chords";
import { parseChordPro } from "@eventuras/scribo-chords/song";
const editor = buildEditorFromExtensions(
defineExtension({ name: "songs", dependencies: [BaseExtension, ChordsExtension] }),
);
setSong(editor, parseChordPro(text));
const json = editor.getEditorState().toJSON(); // what to store
getSong(editor); // the Song again
// Or with no editor at all, straight on the stored JSON:
songOf(json); // Song | null
stateOfSong(song); // serialized editor state

This is what a server, a migration or a test uses. Nothing here imports React or touches the DOM, and a test in the package keeps it so.

In the document a song is one song node that holds the title and the facts. Its children are song-section headings and song-line lines, and a line holds text and chord nodes.

Brackets that are text, [sic] in ChordPro, stay text in an editor. When a song is made into nodes (setSong, a Markdown import, stateOfSong), each run of brackets in the lyrics is a text node of its own with a mark in its node state, stored in the JSON as "$": { "chordsLiteral": "[sic]" }, and typing in the line does not make chords of marked text. A chord typed anywhere else in the line is a chord, also right before or after the brackets. Editing between the brackets keeps them text; deleting one of the brackets makes the text plain again, and typing it back is typing a chord. $createLiteralTextNode and $isLiteralText make and test such text. The mark lives in Lexical's JSON only: lyrics pasted as HTML or plain text are typed text, and [Am] in them becomes a chord.

With the core's Markdown extension loaded, a song is a fenced chordpro block:

md
```chordpro
{title: Morning shift}
{key: G}
[G]Coffee steams on the [D]quay
```

ChordsExtension contributes the transformer by itself; there is nothing to configure. The transformer is also exported, as CHORDPRO_TRANSFORMER from @eventuras/scribo-chords/markdown, for code that calls @lexical/markdown itself. It is not in the root entry, whose types must not need @lexical/markdown. chordProBlocks(markdown) from ./song finds such blocks in Markdown without an editor.

A song editor with React

tsx
import "@eventuras/scribo-chords/style.css";
import { BaseExtension, ToolbarExtension, defineExtension } from "@eventuras/scribo-core";
import { SongDocumentExtension } from "@eventuras/scribo-chords";
import {
ChordPalette,
ChordsReactExtension,
ChordsViewControls,
SectionButtons,
} from "@eventuras/scribo-chords/react";
import { Toolbar } from "@eventuras/scribo-react";
import { ContentEditable } from "@lexical/react/LexicalContentEditable";
import { LexicalExtensionComposer } from "@lexical/react/LexicalExtensionComposer";
const songEditor = defineExtension({
name: "my-song-editor",
dependencies: [BaseExtension, SongDocumentExtension, ChordsReactExtension, ToolbarExtension],
});
<LexicalExtensionComposer extension={songEditor} contentEditable={null}>
<Toolbar />
<SectionButtons />
<ChordPalette />
<ChordsViewControls />
<ContentEditable />
</LexicalExtensionComposer>;
  • ChordsExtension adds songs to any editor: a song is a block among the paragraphs, inserted from the toolbar or by typing ```chordpro and a space.
  • SongDocumentExtension makes the whole document one song, for a song editor.
  • ChordsReactExtension draws the chords as chord chips.
  • Typing [Am] in a line makes a chord. A click on a chord edits it, and Backspace removes it whole.
  • The toolbar items (chord, verse, chorus, bridge, and song to insert one) reach the core's ToolbarExtension on their own. @eventuras/scribo-react's Toolbar renders them, or build your own from useToolbar().
  • The view is changed with setChordsView(editor, view), or useChordsView() in React. It never changes the document: the stored JSON is the same byte for byte before and after.

Words are English by default. Set the headings of new sections with configExtension(ChordsExtension, { sectionLabels: { verse: "Vers", chorus: "Refreng" } }), and the words of the React components with their labels prop or ChordsLabelsContext.

A reader and play mode

tsx
import "@eventuras/scribo-chords/style.css";
import { PlayMode, SongReader, ViewControls } from "@eventuras/scribo-chords/react";
import { songOf } from "@eventuras/scribo-chords";
const song = songOf(storedJson); // null when the document has no song
<ViewControls view={view} onChange={setView} />
<SongReader song={song} view={view} />
{playing && <PlayMode song={song} onClose={() => setPlaying(false)} />}

Both are drawn from the Song model and mount no editor. SongReader puts each chord above its word, in lines that wrap with their chords. PlayMode fills the screen with large text that scrolls by itself, started and stopped with a tap or the space bar, with the speed, the text size and the key at hand; it keeps the screen on where the browser allows it.

How a chord gets its React component

The chord is a Lexical DecoratorNode, and its class lives in the entry without React. It does not import the component that draws it. Instead:

  1. ChordsExtension has a setting, decorateChord, which is null by default.
  2. ChordNode.decorate(editor) asks the chords extension of its editor for that setting and calls it with the editor, the node key and the stored symbol. With no setting it returns null, and the chord draws nothing, which is right for an editor on a server.
  3. ChordsReactExtension is one line: it depends on ChordsExtension with decorateChord set to a function that returns a <ChordChip />. LexicalExtensionComposer renders the decorations.

So there is one node class and one stored chord type, whether or not React is loaded. To draw chords with your own component, set decorateChord yourself with configExtension.

The alternative, replacing the node with a React subclass, was not used: in Lexical 0.50 a replacing class must have its own type, so the stored JSON would differ between an editor with React and one without.

Styling

Every class starts with scribo-chords-. Colours and fonts are CSS variables, such as --scribo-chords-chord, --scribo-chords-primary and --scribo-chords-font, set on :root with no specificity, so set them anywhere:

css
:root {
--scribo-chords-chord: #8a2b0b;
--scribo-chords-chord-font: "Space Mono", monospace;
}

Play mode is dark whatever the page is, and has its own variables (--scribo-chords-play-…). See style.css for the full list.

Open decisions

These are choices the package had to make and may change before 1.0. Each is also a question for whoever builds on it.

  • A chord typed while the song is transposed is read as it is shown. With the view two semitones up, typing [A] stores G, and H is read by the view's notation. It keeps what is seen and what is typed the same, but a writer who thinks in the stored key may expect the opposite. The palette and a click on a chord follow the same rule.
  • {capo} in ChordPro. In ChordPro, {capo: 2} usually means that the chords written are the shapes to play with a capo on the second fret, so they sound two semitones higher. Here stored chords are sounding pitch and the capo is a view setting. On import {capo} is kept as a fact of the song (meta.capo) and written back on export; the chords are not moved. A song written the ChordPro way therefore comes in a capo's worth too low until that is decided.
  • A bare B. A song with a B chord and neither H nor Bb does not tell whether B is B natural (English) or B flat (Nordic). detectNotation returns null for it and hasBareB says so; the app has to ask or decide, and then call importNotation. In the default mixed notation a typed B is read as B flat.
  • The title and the facts are stored on the song node. That is what lets a song round-trip through Markdown and lets a document hold several songs. An app that also keeps a title of its own beside the document has two places for it and has to choose which one wins.
  • Chords cannot be reached with the arrow keys as objects (isKeyboardSelectable() is false): the caret passes a chord in one key press, and Backspace or Delete removes it. Measured in a browser, this is what makes typing around chords behave; the cost is that a chord is edited with a click and not from the keyboard alone.
  • Collaboration is not part of this package. The nodes keep their stored properties as own properties, as the Yjs binding needs, but nothing here has been tried with it.

Develop

bash
pnpm --filter @eventuras/scribo-chords dev # the demo, from source
pnpm --filter @eventuras/scribo-chords test
pnpm --filter @eventuras/scribo-chords build
pnpm --filter @eventuras/scribo-chords build:site # the demo, built to dist-site
pnpm --filter @eventuras/scribo-chords e2e # browser checks of the built demo

The browser checks run the demo in Chromium through Playwright, which is not a dependency of this package: the script takes it from wherever Node finds it. They are a local check and not part of CI.

The demo and the sample song are made up for this package. Do not put the lyrics of real songs in this repository, in tests or anywhere else.