@hypericon/mico 0.2.0 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -6,3 +6,9 @@
6
6
  UI components and utilities built with Mithril.
7
7
 
8
8
  <a href="https://www.npmjs.com/package/@hypericon/mico">![NPM badge](https://img.shields.io/npm/v/@hypericon/mico)</a>
9
+
10
+ ## Installation
11
+
12
+ ```sh
13
+ $ npm i @hypericon/mico
14
+ ```
@@ -1,2 +1,3 @@
1
1
  export * from "./icon";
2
2
  export * from "./markdown";
3
+ export * from "./table";
@@ -26,7 +26,7 @@ __exportStar(require("./markdown"), exports);
26
26
  // export * from "./pagination";
27
27
  // export * from "./popover";
28
28
  // export * from "./reorderable-list";
29
- // export * from "./table";
29
+ __exportStar(require("./table"), exports);
30
30
  // export * from "./toast";
31
31
  // export * from "./tooltip";
32
32
  // export * from "./tree";
@@ -22,6 +22,11 @@ export declare const Markdown: m.Component<{
22
22
  unsanitised?: boolean;
23
23
  /** Highlight syntax */
24
24
  highlight?: boolean;
25
+ /**
26
+ * Wrap all the given "markdown" in triple-backticks with the given language string and enable highlighting
27
+ * @example "typescript"
28
+ */
29
+ highlightAllAs?: string;
25
30
  /** CSS classes to apply to the container div */
26
31
  class?: string;
27
32
  /** CSS styles to apply to the container div */
@@ -27,7 +27,7 @@ exports.MARKDOWN_DEFAULT_OPTIONS = {
27
27
  exports.Markdown = {
28
28
  oncreate({ attrs }) {
29
29
  var _a;
30
- if (attrs.highlight === true) {
30
+ if (attrs.highlight === true || Boolean(attrs.highlightAllAs)) {
31
31
  // console.log("highlighting...");
32
32
  (_a = window.hljs) === null || _a === void 0 ? void 0 : _a.highlightAll();
33
33
  // hljs.highlightAll();
@@ -41,7 +41,10 @@ exports.Markdown = {
41
41
  // },
42
42
  view({ attrs, children }) {
43
43
  var _a, _b, _c, _d, _e, _f;
44
- const markdown = (_c = (_b = (_a = attrs.markdown) !== null && _a !== void 0 ? _a : (typeof children === "string" ? children : undefined)) !== null && _b !== void 0 ? _b : (children instanceof Array && children.length > 0 && typeof children[0] === "string" ? children[0] : undefined)) !== null && _c !== void 0 ? _c : "";
44
+ let markdown = (_c = (_b = (_a = attrs.markdown) !== null && _a !== void 0 ? _a : (typeof children === "string" ? children : undefined)) !== null && _b !== void 0 ? _b : (children instanceof Array && children.length > 0 && typeof children[0] === "string" ? children[0] : undefined)) !== null && _c !== void 0 ? _c : "";
45
+ if (attrs.highlightAllAs) {
46
+ markdown = "```" + attrs.highlightAllAs + "\n" + markdown + "\n" + "```";
47
+ }
45
48
  const parsed = marked_1.marked.parse(markdown, (_d = attrs.markedOptions) !== null && _d !== void 0 ? _d : exports.MARKDOWN_DEFAULT_OPTIONS);
46
49
  const sanitised = attrs.unsanitised === true ? parsed : dompurify_1.default.sanitize(parsed);
47
50
  const result = sanitised;
@@ -0,0 +1,54 @@
1
+ import m from "mithril";
2
+ export type TableCellDisplayFn<T> = (item: T, index: number) => string | number | boolean | m.Vnode<any, any> | null | undefined;
3
+ export type TableColumnDefinition<T> = {
4
+ header: string | m.Vnode<any, any>;
5
+ cell: TableCellDisplayFn<T>;
6
+ };
7
+ export interface TableAttrs<T> {
8
+ /** The list of items to display as rows */
9
+ data: T[];
10
+ /** The definitions of the table columns */
11
+ columns: TableColumnDefinition<T>[];
12
+ /** Set the table to automatically size columns to fit (default), or use fixed column widths */
13
+ layout?: "auto" | "fixed";
14
+ /** Override the default message displayed when there are no data items */
15
+ emptyMessage?: string;
16
+ /** Any CSS classes to apply to the table */
17
+ class?: string;
18
+ /** Any CSS styles to apply to the table */
19
+ style?: string;
20
+ }
21
+ export interface TableState<T> {
22
+ }
23
+ /** Helper function to make it easier to define table columns */
24
+ export type TableColumnDefinitionFn<T> = (
25
+ /** The label or other element to display in the header */
26
+ header: string | m.Vnode<any, any>,
27
+ /** Function defining how to display a cell in the column using the given row item */
28
+ cell: TableCellDisplayFn<T>) => TableColumnDefinition<T>;
29
+ /**
30
+ * Build a typed Table component which includes type checking for column definitions
31
+ * @example
32
+ * ```ts
33
+ * const PersonTable = typedTable<Person>();
34
+ * m(PersonTable, {
35
+ * data: people, // `data` is type checked
36
+ * columns: [
37
+ * PersonTable.col("Name", p => p.name), // `p` is type checked
38
+ * PersonTable.col("Age", p => findAge(p.dob)), // `p` is type checked
39
+ * ],
40
+ * }),
41
+ * ```
42
+ * @returns
43
+ */
44
+ export declare function typedTable<T>(): {
45
+ col: TableColumnDefinitionFn<T>;
46
+ oninit?(this: m._NoLifecycle<m.Component<TableAttrs<T>, TableState<T>> & TableState<T>>, vnode: m.Vnode<TableAttrs<T>, m._NoLifecycle<m.Component<TableAttrs<T>, TableState<T>> & TableState<T>>>): any;
47
+ oncreate?(this: m._NoLifecycle<m.Component<TableAttrs<T>, TableState<T>> & TableState<T>>, vnode: m.VnodeDOM<TableAttrs<T>, m._NoLifecycle<m.Component<TableAttrs<T>, TableState<T>> & TableState<T>>>): any;
48
+ onbeforeremove?(this: m._NoLifecycle<m.Component<TableAttrs<T>, TableState<T>> & TableState<T>>, vnode: m.VnodeDOM<TableAttrs<T>, m._NoLifecycle<m.Component<TableAttrs<T>, TableState<T>> & TableState<T>>>): void | Promise<any>;
49
+ onremove?(this: m._NoLifecycle<m.Component<TableAttrs<T>, TableState<T>> & TableState<T>>, vnode: m.VnodeDOM<TableAttrs<T>, m._NoLifecycle<m.Component<TableAttrs<T>, TableState<T>> & TableState<T>>>): any;
50
+ onbeforeupdate?(this: m._NoLifecycle<m.Component<TableAttrs<T>, TableState<T>> & TableState<T>>, vnode: m.Vnode<TableAttrs<T>, m._NoLifecycle<m.Component<TableAttrs<T>, TableState<T>> & TableState<T>>>, old: m.VnodeDOM<TableAttrs<T>, m._NoLifecycle<m.Component<TableAttrs<T>, TableState<T>> & TableState<T>>>): boolean | void;
51
+ onupdate?(this: m._NoLifecycle<m.Component<TableAttrs<T>, TableState<T>> & TableState<T>>, vnode: m.VnodeDOM<TableAttrs<T>, m._NoLifecycle<m.Component<TableAttrs<T>, TableState<T>> & TableState<T>>>): any;
52
+ view(this: m._NoLifecycle<m.Component<TableAttrs<T>, TableState<T>> & TableState<T>>, vnode: m.Vnode<TableAttrs<T>, m._NoLifecycle<m.Component<TableAttrs<T>, TableState<T>> & TableState<T>>>): void | m.Children;
53
+ };
54
+ export declare const Table: m.Component<TableAttrs<any>, TableState<any>>;
@@ -0,0 +1,68 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.Table = exports.typedTable = void 0;
7
+ const mithril_1 = __importDefault(require("mithril"));
8
+ /**
9
+ * Build a typed Table component which includes type checking for column definitions
10
+ * @example
11
+ * ```ts
12
+ * const PersonTable = typedTable<Person>();
13
+ * m(PersonTable, {
14
+ * data: people, // `data` is type checked
15
+ * columns: [
16
+ * PersonTable.col("Name", p => p.name), // `p` is type checked
17
+ * PersonTable.col("Age", p => findAge(p.dob)), // `p` is type checked
18
+ * ],
19
+ * }),
20
+ * ```
21
+ * @returns
22
+ */
23
+ function typedTable() {
24
+ const typedTable = exports.Table;
25
+ const col = (header, cell) => {
26
+ return { header, cell };
27
+ };
28
+ return Object.assign(Object.assign({}, typedTable), { col });
29
+ }
30
+ exports.typedTable = typedTable;
31
+ exports.Table = {
32
+ view({ attrs }) {
33
+ var _a, _b, _c, _d;
34
+ return (0, mithril_1.default)("table", {
35
+ class: attrs.class,
36
+ style: `${(_a = attrs.style) !== null && _a !== void 0 ? _a : ""} table-layout: ${((_b = attrs.layout) !== null && _b !== void 0 ? _b : "auto")};`,
37
+ }, [
38
+ (0, mithril_1.default)("thead", [
39
+ (0, mithril_1.default)("tr", [
40
+ attrs.columns.map(col => {
41
+ return (0, mithril_1.default)("th", [
42
+ col.header,
43
+ ]);
44
+ }),
45
+ ]),
46
+ ]),
47
+ (0, mithril_1.default)("tbody", [
48
+ attrs.data.map(item => {
49
+ return (0, mithril_1.default)("tr", [
50
+ attrs.columns.map((col, index) => {
51
+ var _a;
52
+ return (0, mithril_1.default)("td", [
53
+ (_a = col.cell) === null || _a === void 0 ? void 0 : _a.call(col, item, index),
54
+ ]);
55
+ }),
56
+ ]);
57
+ }),
58
+ ((_c = attrs.data) === null || _c === void 0 ? void 0 : _c.length) === 0 ? (0, mithril_1.default)("tr", [
59
+ (0, mithril_1.default)("td.text-center.text-medium", {
60
+ colspan: attrs.columns.length,
61
+ }, [
62
+ (_d = attrs.emptyMessage) !== null && _d !== void 0 ? _d : "No items",
63
+ ]),
64
+ ]) : [],
65
+ ]),
66
+ ]);
67
+ }
68
+ };
@@ -0,0 +1 @@
1
+ export * from "./Table";
@@ -1 +1,17 @@
1
1
  "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./Table"), exports);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hypericon/mico",
3
- "version": "0.2.0",
3
+ "version": "0.2.1",
4
4
  "description": "UI components and utilities with Mithril",
5
5
  "author": "Ed Johnson, Hypericon Ltd",
6
6
  "license": "MIT",