Mixins / foldkit-mixins-ui

CodeBlock

namespace · packages/mixins-ui/src/codeBlock.ts:1

export * as CodeBlock from './codeBlock.js'

In the guides

Members

CodeBlock.CodeBlockCollapsed

interface · packages/mixins-ui/src/codeBlock.ts:82

interface CodeBlockCollapsed<Message> {
  readonly lines: number
  readonly expanded?: boolean | undefined
  readonly onToggle?: Message | undefined
}

Shows the first lines lines under a fade, and a button for the rest. Every line stays in the page, clamped by style, so a find, a copy of the page's text and an anchor still reach the whole code. A block of lines lines or fewer is drawn whole, with no button.

CodeBlock.CodeBlockCopy

interface · packages/mixins-ui/src/codeBlock.ts:44

interface CodeBlockCopy<Message> {
  readonly onCopy?: ((code: string) => Message) | undefined
  readonly copied?: boolean | undefined
}

The copy button: what pressing it causes, and whether that has happened.

CodeBlock.CodeBlockSlots

const · packages/mixins-ui/src/codeBlock/slots.ts:10

const CodeBlockSlots: Slots.Contract<{ readonly root: Slot.UnnamedSlot<Capability.Capability<"Container", readonly [Capability.Capability<"Interactive", readonly [Capability.Capability<"Base", readonly []>]>]>, readonly Slot.EventName[], readonly Slot.AttributeName[], readonly Slot.RequirementName[], boolean>; readonly header: Slot.UnnamedSlot<Capability.Capability<"Container", readonly [Capability.Capability<"Interactive", readonly [Capability.Capability<"Base", readonly []>]>]>, readonly Slot.EventName[], readonly Slot.AttributeName[], readonly Slot.RequirementName[], boolean>; readonly title: Slot.UnnamedSlot<Capability.Capability<"Container", readonly [Capability.Capability<"Interactive", readonly [Capability.Capability<"Base", readonly []>]>]>, readonly Slot.EventName[], readonly Slot.AttributeName[], readonly Slot.RequirementName[], boolean>; readonly copy: Slot.UnnamedSlot<Capability.Capability<"Interactive", readonly [Capability.Capability<"Base", readonly []>]>, readonly [Event.EventToken<"click">], readonly Slot.AttributeName[], readonly Slot.RequirementName[], boolean>; readonly status: Slot.UnnamedSlot<Capability.Capability<"Container", readonly [Capability.Capability<"Interactive", readonly [Capability.Capability<"Base", readonly []>]>]>, readonly Slot.EventName[], readonly [Attr.AttrToken<"role">], readonly Slot.RequirementName[], boolean>; readonly body: Slot.UnnamedSlot<Capability.Capability<"Container", readonly [Capability.Capability<"Interactive", readonly [Capability.Capability …;

A code block publishes its regions as slots, down to each line and each highlighted token; it owns no state. Highlighting is the caller's: pass the tokens a tokenizer found (foldkit-richtext-code, foldkit-richtext-code-shiki) and each is drawn with its kind as data-kind, which the recipe colors from the theme's syntax group.

CodeBlock.CodeBlockView

interface · packages/mixins-ui/src/codeBlock.ts:100

interface CodeBlockView<Message> {
  readonly code: string
  readonly tokens?: ReadonlyArray<CodeToken> | undefined
  readonly language?: string | undefined
  readonly title?: string | undefined
  readonly lineNumbers?: boolean | undefined
  readonly startLine?: number | undefined
  readonly gutter?: ((line: number) => string) | undefined
  readonly marks?: ReadonlyArray<CodeMark> | undefined
  readonly wrap?: boolean | undefined
  readonly collapsed?: CodeBlockCollapsed<Message> | undefined
  readonly onLineSelect?: ((line: number) => Message) | undefined
  readonly lineAction?: ((line: number) => Option.Option<Html>) | undefined
  readonly copy?: CodeBlockCopy<Message> | undefined
  readonly words?: Partial<CodeBlockWords> | undefined
  readonly style?: NamedStyle<typeof CodeBlockSlots> | undefined
  readonly mixins?: MixinList<Message> | undefined
}

Code in one call: the text, the tokens that color it, a title (a file name) or a language over it, line numbers, and a copy button. No upstream component stands behind it.

CodeBlock.CodeBlockWords

interface · packages/mixins-ui/src/codeBlock.ts:56

interface CodeBlockWords {
  readonly copy: string
  readonly copied: string
  readonly selectLine: string
  readonly expand: string
  readonly collapse: string
}

The block's words: the copy button's, a gutter button's name, and the expand button's.

CodeBlock.CodeMark

interface · packages/mixins-ui/src/codeBlock.ts:37

interface CodeMark {
  readonly line: number
  readonly kind: CodeMarkKind | (string & {})
}

A whole line marked by what it is: data-mark on the line.

CodeBlock.CodeMarkKind

type · packages/mixins-ui/src/codeBlock.ts:27

type CodeMarkKind =
  'added' | 'removed' | 'highlight' | 'error' | 'warning' | 'dim' | 'selected'

The kinds the recipe colours: a diff's added and removed, a highlight, a diagnostic's error and warning, dim for the lines a walkthrough is not about, and selected, which also presses the line's gutter button under onLineSelect. A kind of your own is a recipe add.

CodeBlock.CodeToken

interface · packages/mixins-ui/src/codeBlock.ts:15

interface CodeToken {
  readonly from: number
  readonly to: number
  readonly kind: string
}

A highlighted run of the code, as offsets into it: the shape foldkit-richtext's CodeToken has, so a tokenizer's output passes as it is.

CodeBlock.follow

const · packages/mixins-ui/src/codeBlock/follow.ts:49

const follow: readonly [Mixin<never>]

Keeps a growing block's end in view while the reader is at it, as an agent writes code: pass it in mixins, and give the body a max-block-size so it scrolls. Not a default: its Mount sends no Message, which a Scene test of every page drawing a block could not resolve.

CodeBlock.linesOf

function · packages/mixins-ui/src/codeBlock.ts:143

function linesOf(
  code: string,
  tokens: ReadonlyArray<CodeToken>,
): ReadonlyArray<ReadonlyArray<Run>>

The code cut into lines of runs. A token out of order, overlapping the one before it, outside the code or covering nothing throws: it is the tokenizer's bug, and drawing it would color the wrong text.

CodeBlock.view

function · packages/mixins-ui/src/codeBlock.ts:200

function view<Message>(
  options: CodeBlockView<Message>,
  h: HtmlBuilder<Message>,
): Html

CodeBlock.words

const · packages/mixins-ui/src/codeBlock.ts:68

const words = { copy, copied, selectLine, expand, collapse }

The words a code block starts from; words overrides them one at a time.