UNPKG

@bokeh/bokehjs

Version:

Interactive, novel data visualization

402 lines 14.1 kB
import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "preact/jsx-runtime"; import { to_string } from "../../core/util/pretty"; import { HasProps } from "../../core/has_props"; import { isBoolean, isNumber, isString, isSymbol, isArray, isIterable, isObject, isPlainObject } from "../../core/util/types"; import { entries } from "../../core/util/object"; import { interleave } from "../../core/util/array"; import { css4_parse } from "../../core/util/color"; import { Kinds, Kind } from "../../core/kinds"; import * as p from "../../core/properties"; import * as pretty from "../../styles/pretty.css"; export class BasePrinter { static __name__ = "BasePrinter"; null() { return _jsx("span", { class: pretty.nullish, children: "null" }); } token(val) { return _jsx("span", { class: pretty.token, children: val }); } boolean(val) { return _jsx("span", { class: pretty.boolean, children: `${val}` }); } number(val) { return _jsx("span", { class: pretty.number, children: `${val}` }); } string(val) { const sq = val.includes("'"); const dq = val.includes('"'); const str = (() => { if (sq && dq) { return `\`${val.replace(/`/g, "\\`")}\``; } else if (dq) { return `'${val}'`; } else { return `"${val}"`; } })(); const rep = _jsx("span", { class: pretty.string, children: str }); const color = css4_parse(val); if (color == null) { return rep; } else { return (_jsxs("span", { class: pretty.color, children: [_jsx("span", { class: pretty.swatch, style: { backgroundColor: val } }), rep] })); } } symbol(val) { return _jsx("span", { class: pretty.symbol, children: val.toString() }); } } export class OpaqueKindPrinter extends BasePrinter { static __name__ = "OpaqueKindPrinter"; to_html(obj) { if (obj == null) { return this.null(); } else if (isBoolean(obj)) { return this.boolean(obj); } else if (isNumber(obj)) { return this.number(obj); } else if (isString(obj)) { return this.string(obj); } else if (isSymbol(obj)) { return this.symbol(obj); } else if (obj instanceof Kinds.Ref) { return this.ref(obj); } else if (obj instanceof Kinds.Struct) { return this.struct(obj); } else if (obj instanceof Kinds.PartialStruct) { return this.partial_struct(obj); } else if (obj instanceof Kinds.Func) { return this.func(obj); } else if (obj instanceof Kind) { return this.kind(obj); } else { return _jsx("span", { children: obj.toString() }); } } ref(obj) { const T = this.token; return _jsxs("span", { children: [obj.kind_name, T("("), _jsx("span", { class: pretty.type, children: obj.type_name }), T(")")] }); } struct(obj) { const T = this.token; const args = entries(obj.struct_type).map(([key, val]) => _jsxs(_Fragment, { children: [this.to_html(key), T(": "), this.to_html(val)] })); return _jsxs("span", { children: [obj.kind_name, T("({"), args, T("})")] }); } partial_struct(obj) { const T = this.token; const args = entries(obj.struct_type).map(([key, val]) => _jsxs(_Fragment, { children: [this.to_html(key), T("?: "), this.to_html(val)] })); return _jsxs("span", { children: [obj.kind_name, T("({"), args, T("})")] }); } func(obj) { const T = this.token; const args = obj.args_types?.map((arg) => this.to_html(arg)) ?? []; const ret = obj.ret_type === undefined ? _jsx("span", { children: "Void" }) : this.to_html(obj.ret_type); return _jsxs("span", { children: [obj.kind_name, T("("), T("("), interleave(args, () => T(", ")), T(")"), T(", "), ret, T(")")] }); } kind(obj) { const T = this.token; const { kind_name, kind_args } = obj; if (kind_args.length == 0) { return _jsx("span", { children: kind_name }); } else { const args = kind_args.map((arg) => this.to_html(arg)); return _jsxs("span", { children: [kind_name, T("("), interleave(args, () => T(", ")), T(")")] }); } } } export class KindPrinter extends BasePrinter { static __name__ = "KindPrinter"; _precedence_queue = []; to_html(obj) { const p_prev = this._precedence_queue.at(-1); const p = this.precedence(obj); this._precedence_queue.push(p); try { const rep = this._to_html(obj); if (p_prev === undefined || p >= p_prev) { return rep; } else { const T = this.token; return _jsxs(_Fragment, { children: [T("("), rep, T(")")] }); } } finally { this._precedence_queue.pop(); } } _to_html(obj) { if (obj == null) { return this.null(); } else if (isBoolean(obj)) { return this.boolean(obj); } else if (isNumber(obj)) { return this.number(obj); } else if (isString(obj)) { return this.string(obj); } else if (isSymbol(obj)) { return this.symbol(obj); } else if (obj instanceof Kinds.Ref) { return this.ref(obj); } else if (obj instanceof Kinds.Nullable) { return this.nullable(obj); } else if (obj instanceof Kinds.Opt) { return this.opt(obj); } else if (obj instanceof Kinds.List) { return this.list(obj); } else if (obj instanceof Kinds.Set) { return this.set(obj); } else if (obj instanceof Kinds.Dict) { return this.dict(obj); } else if (obj instanceof Kinds.Mapping) { return this.mapping(obj); } else if (obj instanceof Kinds.Tuple) { return this.tuple(obj); } else if (obj instanceof Kinds.Or) { return this.or(obj); } else if (obj instanceof Kinds.And) { return this.and(obj); } else if (obj instanceof Kinds.Enum) { return this.enum(obj); } else if (obj instanceof Kinds.Struct) { return this.struct(obj); } else if (obj instanceof Kinds.PartialStruct) { return this.partial_struct(obj); } else if (obj instanceof Kinds.Func) { return this.func(obj); } else if (obj instanceof Kinds.Regex) { return this.regex(obj); } else if (obj instanceof Kinds.PrefixedStr) { return this.prefixed_str(obj); } else if (obj instanceof Kinds.Primitive) { return this.primitive(obj.toString().toLocaleLowerCase()); } else { return _jsx("span", { children: obj.toString() }); } } precedence(kind) { if (kind instanceof Kinds.Or || kind instanceof Kinds.Enum || kind instanceof Kinds.Tuple) { return 0; } else if (kind instanceof Kinds.And) { return 1; } else { return 2; } } primitive(obj) { return _jsx("span", { class: pretty.primitive, children: obj }); } ref(obj) { return _jsx("span", { class: pretty.type, children: obj.type_name }); } nullable(obj) { const T = this.token; return _jsxs("span", { children: [this.to_html(obj.base_type), T("?")] }); } opt(obj) { return this.nullable(obj); } list(obj) { const T = this.token; return _jsxs("span", { children: [this.to_html(obj.item_type), T("["), T("]")] }); } set(obj) { const T = this.token; return _jsxs("span", { children: [T("{"), this.to_html(obj.item_type), T("}")] }); } dict(obj) { const T = this.token; return _jsxs("span", { children: [T("{"), this.primitive("str"), T(": "), this.to_html(obj.item_type), T("}")] }); } mapping(obj) { const T = this.token; return _jsxs("span", { children: [T("{"), this.to_html(obj.key_type), T(" => "), this.to_html(obj.item_type), T("}")] }); } tuple(obj) { const T = this.token; const types = obj.types.map((tp) => this.to_html(tp)); return _jsxs("span", { children: [T("["), interleave(types, () => T(", ")), T("]")] }); } or(obj) { const T = this.token; const types = obj.types.map((tp) => this.to_html(tp)); return _jsx("span", { children: interleave(types, () => T(" | ")) }); } and(obj) { const T = this.token; const types = obj.types.map((tp) => this.to_html(tp)); return _jsx("span", { children: interleave(types, () => T(" & ")) }); } enum(obj) { const T = this.token; const types = [...obj.values].map((val) => this.to_html(val)); return _jsx("span", { children: interleave(types, () => T(" | ")) }); } struct(obj) { const T = this.token; const fields = entries(obj.struct_type).map(([name, kind]) => { return _jsxs("span", { children: [name, T(": "), this.to_html(kind)] }); }); return _jsxs("span", { children: [T("{"), interleave(fields, () => T(", ")), T("}")] }); } partial_struct(obj) { const T = this.token; const fields = entries(obj.struct_type).map(([name, kind]) => { return _jsxs("span", { children: [name, T("?: "), this.to_html(kind)] }); }); return _jsxs("span", { children: [T("{"), interleave(fields, () => T(", ")), T("}")] }); } func(obj) { const T = this.token; const args = obj.args_types?.map((arg) => this.to_html(arg)) ?? []; const ret = obj.ret_type === undefined ? this.primitive("void") : this.to_html(obj.ret_type); return _jsxs("span", { children: [T("("), interleave(args, () => T(", ")), T(")"), T(" => "), ret] }); } regex(obj) { const T = this.token; const { source, flags } = obj.regex; return _jsxs("span", { children: [T("/"), source, T("/"), flags] }); } prefixed_str(obj) { const T = this.token; return _jsxs("span", { children: ["PrefixedStr", T("("), this.to_html(obj.prefix), T(")")] }); } } export class ValuePrinter extends BasePrinter { click; max_items; max_depth; static __name__ = "ValuePrinter"; visited = new WeakSet(); depth = 0; constructor(click, max_items = 5, max_depth = 3) { super(); this.click = click; this.max_items = max_items; this.max_depth = max_depth; } to_html(obj) { if (isObject(obj)) { if (this.visited.has(obj)) { return _jsx("span", { children: "circular" }); } else { this.visited.add(obj); } } if (obj == null) { return this.null(); } else if (isBoolean(obj)) { return this.boolean(obj); } else if (isNumber(obj)) { return this.number(obj); } else if (isString(obj)) { return this.string(obj); } else if (isSymbol(obj)) { return this.symbol(obj); } else if (obj instanceof HasProps) { return this.model(obj); } else if (obj instanceof p.Property) { return this.property(obj); } else if (isPlainObject(obj)) { return this.object(obj); } else if (isArray(obj)) { return this.array(obj); } else if (isIterable(obj)) { return this.iterable(obj); } else { return _jsx("span", { children: to_string(obj) }); } } array(obj) { const T = this.token; const items = []; let i = 0; for (const entry of obj) { items.push(this.to_html(entry)); if (i++ > this.max_items) { items.push(_jsx("span", { children: "\\u2026" })); break; } } return _jsxs("span", { class: pretty.array, children: [T("["), interleave(items, () => T(", ")), T("]")] }); } iterable(obj) { const T = this.token; const tag = Object(obj)[Symbol.toStringTag] ?? "Object"; const items = this.array([...obj]); return _jsxs("span", { class: pretty.iterable, children: [`${tag}`, T("("), items, T(")")] }); } object(obj) { const T = this.token; const items = []; let i = 0; for (const [key, val] of entries(obj)) { items.push(_jsxs("span", { children: ["$", `${key}`, T(": "), this.to_html(val)] })); if (i++ > this.max_items) { items.push(_jsx("span", { children: "\\u2026" })); break; } } return _jsxs("span", { class: pretty.object, children: [T("{"), interleave(items, () => T(", ")), T("}")] }); } model(obj) { const T = this.token; const { click } = this; return (_jsxs("span", { class: `${pretty.model} ${click != null ? "ref" : ""}`, onClick: () => click?.(obj), children: [obj.constructor.__qualified__, T("("), this.to_html(obj.id), T(")")] })); } property(obj) { const model = this.model(obj.obj); const attr = _jsx("span", { class: pretty.attr, children: obj.attr }); return _jsxs("span", { children: [model, this.token("."), attr] }); } } //# sourceMappingURL=printers.js.map