import { ImmutableOutput } from "./outputs"; import { ExecutionCount, MimeBundle, normalizeLineEndings } from "./primitives"; import { List as ImmutableList, Map as ImmutableMap, Record, RecordOf, } from "immutable"; function normalizedSourceCellRecord(recordFn: Record.Factory): Record.Factory { // Transparently wrap the factory, but overwrite the source with its normalized value function factory(this: ThisType, ...args: Parameters) { const res = recordFn.apply(this, args); return res.set("source", normalizeLineEndings(res.source)); }; factory.prototype = recordFn.prototype; factory.displayName = recordFn.displayName; return factory as Record.Factory; } /* CodeCell Record Boilerplate */ export interface CodeCellParams { cell_type: "code"; id?: string; // Sadly untyped and widely unspecced metadata: ImmutableMap; execution_count: ExecutionCount; source: string; outputs: ImmutableList; } export const makeCodeCell = normalizedSourceCellRecord(Record({ cell_type: "code", execution_count: null, metadata: ImmutableMap({ jupyter: ImmutableMap({ source_hidden: false, outputs_hidden: false, }), nteract: ImmutableMap({ transient: ImmutableMap({ deleting: false, }), }), }), source: "", outputs: ImmutableList(), })); export type ImmutableCodeCell = RecordOf; /* MarkdownCell Record Boilerplate */ export interface MarkdownCellParams { attachments?: ImmutableMap>; cell_type: "markdown"; id?: string; source: string; metadata: ImmutableMap; } export const makeMarkdownCell = normalizedSourceCellRecord(Record({ attachments: undefined, cell_type: "markdown", metadata: ImmutableMap({ nteract: ImmutableMap({ transient: ImmutableMap({ deleting: false, }), }), }), source: "", })); export type ImmutableMarkdownCell = RecordOf; /* RawCell Record Boilerplate */ export interface RawCellParams { id?: string; cell_type: "raw"; source: string; metadata: ImmutableMap; } export const makeRawCell = normalizedSourceCellRecord(Record({ cell_type: "raw", metadata: ImmutableMap({ nteract: ImmutableMap({ transient: ImmutableMap({ deleting: false, }), }), }), source: "", })); export type ImmutableRawCell = RecordOf; export type ImmutableCell = | ImmutableMarkdownCell | ImmutableCodeCell | ImmutableRawCell; export type CellType = "raw" | "markdown" | "code";