@davidsouther/jiffies 1.0.0-beta.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 +60 -0
- package/build/assert.d.ts +23 -0
- package/build/assert.js +33 -0
- package/build/case.d.ts +1 -0
- package/build/case.js +5 -0
- package/build/components/button_bar.d.ts +8 -0
- package/build/components/button_bar.js +16 -0
- package/build/components/index.d.ts +1 -0
- package/build/components/index.js +1 -0
- package/build/components/inline_edit.d.ts +12 -0
- package/build/components/inline_edit.js +48 -0
- package/build/components/logger.d.ts +7 -0
- package/build/components/logger.js +22 -0
- package/build/components/select.d.ts +13 -0
- package/build/components/select.js +3 -0
- package/build/components/test.d.ts +1 -0
- package/build/components/test.js +2 -0
- package/build/components/virtual_scroll.d.ts +41 -0
- package/build/components/virtual_scroll.js +94 -0
- package/build/components/virtual_scroll.test.d.ts +1 -0
- package/build/components/virtual_scroll.test.js +21 -0
- package/build/context.d.ts +15 -0
- package/build/context.js +43 -0
- package/build/context.test.d.ts +1 -0
- package/build/context.test.js +46 -0
- package/build/debounce.d.ts +1 -0
- package/build/debounce.js +7 -0
- package/build/display.d.ts +5 -0
- package/build/display.js +3 -0
- package/build/dom/css/border.d.ts +11 -0
- package/build/dom/css/border.js +27 -0
- package/build/dom/css/constants.d.ts +31 -0
- package/build/dom/css/constants.js +28 -0
- package/build/dom/css/core.d.ts +5 -0
- package/build/dom/css/core.js +24 -0
- package/build/dom/css/fstyle.d.ts +5 -0
- package/build/dom/css/fstyle.js +32 -0
- package/build/dom/css/sizing.d.ts +5 -0
- package/build/dom/css/sizing.js +10 -0
- package/build/dom/dom.d.ts +27 -0
- package/build/dom/dom.js +94 -0
- package/build/dom/fc.d.ts +14 -0
- package/build/dom/fc.js +35 -0
- package/build/dom/fc.test.d.ts +1 -0
- package/build/dom/fc.test.js +21 -0
- package/build/dom/form/form.app.d.ts +1 -0
- package/build/dom/form/form.app.js +23 -0
- package/build/dom/form/form.d.ts +25 -0
- package/build/dom/form/form.js +25 -0
- package/build/dom/form/form.test.d.ts +0 -0
- package/build/dom/form/form.test.js +1 -0
- package/build/dom/html.d.ts +117 -0
- package/build/dom/html.js +114 -0
- package/build/dom/html.test.d.ts +1 -0
- package/build/dom/html.test.js +58 -0
- package/build/dom/router/link.d.ts +6 -0
- package/build/dom/router/link.js +3 -0
- package/build/dom/router/router.d.ts +12 -0
- package/build/dom/router/router.js +49 -0
- package/build/dom/svg.d.ts +64 -0
- package/build/dom/svg.js +65 -0
- package/build/dom/test.d.ts +1 -0
- package/build/dom/test.js +2 -0
- package/build/dom/types/css.d.ts +6612 -0
- package/build/dom/types/css.js +23 -0
- package/build/dom/types/dom.d.ts +0 -0
- package/build/dom/types/dom.js +1 -0
- package/build/dom/types/html.d.ts +616 -0
- package/build/dom/types/html.js +1 -0
- package/build/dom/xml.d.ts +1 -0
- package/build/dom/xml.js +5 -0
- package/build/equal.d.ts +4 -0
- package/build/equal.js +22 -0
- package/build/equal.test.d.ts +1 -0
- package/build/equal.test.js +20 -0
- package/build/flags.d.ts +7 -0
- package/build/flags.js +48 -0
- package/build/flags.test.d.ts +1 -0
- package/build/flags.test.js +35 -0
- package/build/generator.d.ts +1 -0
- package/build/generator.js +10 -0
- package/build/generator.test.d.ts +1 -0
- package/build/generator.test.js +24 -0
- package/build/index.d.ts +13 -0
- package/build/index.js +13 -0
- package/build/is_browser.d.ts +1 -0
- package/build/is_browser.js +1 -0
- package/build/loader.d.mts +22 -0
- package/build/loader.mjs +35 -0
- package/build/lock.d.ts +1 -0
- package/build/lock.js +23 -0
- package/build/lock.test.d.ts +1 -0
- package/build/lock.test.js +16 -0
- package/build/log.d.ts +26 -0
- package/build/log.js +34 -0
- package/build/parcel_resolver.d.ts +3 -0
- package/build/parcel_resolver.js +19 -0
- package/build/range.d.ts +1 -0
- package/build/range.js +7 -0
- package/build/result.d.ts +31 -0
- package/build/result.js +65 -0
- package/build/result.test.d.ts +1 -0
- package/build/result.test.js +71 -0
- package/build/safe.d.ts +1 -0
- package/build/safe.js +10 -0
- package/build/scope/describe.d.ts +14 -0
- package/build/scope/describe.js +52 -0
- package/build/scope/display/console.d.ts +2 -0
- package/build/scope/display/console.js +21 -0
- package/build/scope/display/dom.d.ts +3 -0
- package/build/scope/display/dom.js +26 -0
- package/build/scope/display/junit.d.ts +2 -0
- package/build/scope/display/junit.js +17 -0
- package/build/scope/execute.d.ts +12 -0
- package/build/scope/execute.js +85 -0
- package/build/scope/expect.d.ts +23 -0
- package/build/scope/expect.js +107 -0
- package/build/scope/fix.d.ts +4 -0
- package/build/scope/fix.js +22 -0
- package/build/scope/index.d.ts +3 -0
- package/build/scope/index.js +3 -0
- package/build/scope/scope.d.ts +17 -0
- package/build/scope/scope.js +1 -0
- package/build/server/http/apps.d.ts +5 -0
- package/build/server/http/apps.js +23 -0
- package/build/server/http/index.d.ts +21 -0
- package/build/server/http/index.js +71 -0
- package/build/server/http/response.d.ts +4 -0
- package/build/server/http/response.js +37 -0
- package/build/server/http/sitemap.d.ts +2 -0
- package/build/server/http/sitemap.js +42 -0
- package/build/server/http/static.d.ts +2 -0
- package/build/server/http/static.js +21 -0
- package/build/server/http/typescript.d.ts +5 -0
- package/build/server/http/typescript.js +40 -0
- package/build/server/main.d.ts +2 -0
- package/build/server/main.js +9 -0
- package/build/test.d.mts +2 -0
- package/build/test.mjs +23 -0
- package/build/test_all.d.ts +1 -0
- package/build/test_all.js +19 -0
- package/build/transpile.d.mts +3 -0
- package/build/transpile.mjs +18 -0
- package/package.json +36 -0
- package/src/404.html +14 -0
- package/src/assert.ts +50 -0
- package/src/case.ts +5 -0
- package/src/components/_notes +33 -0
- package/src/components/button_bar.ts +38 -0
- package/src/components/inline_edit.ts +77 -0
- package/src/components/logger.ts +36 -0
- package/src/components/select.ts +22 -0
- package/src/components/test.js +2 -0
- package/src/components/virtual_scroll.test.ts +27 -0
- package/src/components/virtual_scroll.ts +194 -0
- package/src/context.test.ts +58 -0
- package/src/context.ts +62 -0
- package/src/debounce.ts +7 -0
- package/src/display.ts +12 -0
- package/src/dom/README.md +102 -0
- package/src/dom/css/border.ts +47 -0
- package/src/dom/css/constants.ts +34 -0
- package/src/dom/css/core.ts +28 -0
- package/src/dom/css/fstyle.ts +42 -0
- package/src/dom/css/sizing.ts +11 -0
- package/src/dom/dom.ts +153 -0
- package/src/dom/fc.test.ts +43 -0
- package/src/dom/fc.ts +79 -0
- package/src/dom/form/form.app.ts +50 -0
- package/src/dom/form/form.test.ts +0 -0
- package/src/dom/form/form.ts +53 -0
- package/src/dom/form/index.html +14 -0
- package/src/dom/html.test.ts +72 -0
- package/src/dom/html.ts +129 -0
- package/src/dom/router/link.ts +14 -0
- package/src/dom/router/router.ts +69 -0
- package/src/dom/svg.ts +77 -0
- package/src/dom/test.ts +2 -0
- package/src/dom/types/css.ts +10106 -0
- package/src/dom/types/dom.ts +0 -0
- package/src/dom/types/html.ts +631 -0
- package/src/dom/xml.ts +12 -0
- package/src/equal.test.ts +23 -0
- package/src/equal.ts +32 -0
- package/src/favicon.ico +0 -0
- package/src/flags.test.ts +43 -0
- package/src/flags.ts +53 -0
- package/src/generator.test.ts +26 -0
- package/src/generator.ts +12 -0
- package/src/hooks/_notes +3 -0
- package/src/index.html +79 -0
- package/src/is_browser.js +1 -0
- package/src/loader.mjs +45 -0
- package/src/lock.test.ts +17 -0
- package/src/lock.ts +22 -0
- package/src/log.ts +61 -0
- package/src/observable/_notes +13 -0
- package/src/observable/observable._js +175 -0
- package/src/range.ts +7 -0
- package/src/result.test.ts +98 -0
- package/src/result.ts +107 -0
- package/src/safe.ts +12 -0
- package/src/scope/describe.ts +70 -0
- package/src/scope/display/console.ts +26 -0
- package/src/scope/display/dom.ts +36 -0
- package/src/scope/display/junit.ts +67 -0
- package/src/scope/execute.ts +108 -0
- package/src/scope/expect.ts +170 -0
- package/src/scope/fix.ts +29 -0
- package/src/scope/index.ts +11 -0
- package/src/scope/scope.ts +21 -0
- package/src/server/http/apps.ts +26 -0
- package/src/server/http/index.ts +119 -0
- package/src/server/http/response.ts +47 -0
- package/src/server/http/sitemap.ts +48 -0
- package/src/server/http/static.ts +27 -0
- package/src/server/http/typescript.ts +46 -0
- package/src/server/main.ts +13 -0
- package/src/test.mjs +29 -0
- package/src/test_all.ts +22 -0
- package/src/transpile.mjs +29 -0
package/README.md
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
# Jiffies
|
|
2
|
+
|
|
3
|
+
JEFRi Jiffies are a number of "common" utilities for JavaScript/TypeScript pulled from and inspired by other programming languages, and common base tools missing or inconsistent between DOM and Node runtimes.
|
|
4
|
+
|
|
5
|
+
- `assert` - common runtime assertions.
|
|
6
|
+
- `context` - JavaScript implementation of the Python [`with`][pywith] statement.
|
|
7
|
+
- `display` - TypeScript implementation of the Rust [Display][rustdisplay] trait.
|
|
8
|
+
- `equal` - JavaScript deep equality checkers, including TS type checking.
|
|
9
|
+
- `flags` - JavaScript flag, environment, and configuration loader.
|
|
10
|
+
- `log` - JavaScript implementation of a [log4j][log4j]-alike logger.
|
|
11
|
+
- `result` - JavaScript implementation of Rust's [Option][rustoption] and [Result][rustresult] types.
|
|
12
|
+
- `loader.mjs` - Node 16.x typescript-transpiling module loader.
|
|
13
|
+
|
|
14
|
+
Jiffies also includes several microframeworks.
|
|
15
|
+
|
|
16
|
+
- `dom` - a tiny DOM functional library.
|
|
17
|
+
- `pico` - a copy of [PicoCSS](pico.css).
|
|
18
|
+
- `scope` - JavaScript testing microframework.
|
|
19
|
+
- `server` - Node HTTP Server & middleware.
|
|
20
|
+
|
|
21
|
+
## Development
|
|
22
|
+
|
|
23
|
+
1. Clone jiffies
|
|
24
|
+
1. cd jiffies
|
|
25
|
+
1. npm install
|
|
26
|
+
1. npm link
|
|
27
|
+
|
|
28
|
+
After any edit
|
|
29
|
+
|
|
30
|
+
1. npm run build
|
|
31
|
+
|
|
32
|
+
Alternatively, in a background terminal
|
|
33
|
+
|
|
34
|
+
1. npm run watch &
|
|
35
|
+
|
|
36
|
+
## Apps
|
|
37
|
+
|
|
38
|
+
### Starting
|
|
39
|
+
|
|
40
|
+
1. Create app folder
|
|
41
|
+
1. npm init
|
|
42
|
+
1. npm install -D prettier typescript@next
|
|
43
|
+
1. npm link @jefri/jiffies
|
|
44
|
+
|
|
45
|
+
<!--
|
|
46
|
+
TODO(Make @jefri/create)
|
|
47
|
+
|
|
48
|
+
1. npm init @jefri
|
|
49
|
+
|
|
50
|
+
* .gitignore
|
|
51
|
+
* package.json
|
|
52
|
+
* tsconfig.json
|
|
53
|
+
* src/main.ts
|
|
54
|
+
* src/index.html
|
|
55
|
+
* src/app.ts
|
|
56
|
+
-->
|
|
57
|
+
|
|
58
|
+
### Running
|
|
59
|
+
|
|
60
|
+
1. npm start
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
declare type AssertMessage = string | (() => string);
|
|
2
|
+
/**
|
|
3
|
+
* Throw an error when a condition is not met.
|
|
4
|
+
*/
|
|
5
|
+
export declare function assert(condition: boolean, message?: AssertMessage): void | never;
|
|
6
|
+
/**
|
|
7
|
+
* Given a value, return it if it is not null nor undefined. Otherwise throw an
|
|
8
|
+
* error.
|
|
9
|
+
*
|
|
10
|
+
* @template T
|
|
11
|
+
* @returns {NonNullable<T>}
|
|
12
|
+
*/
|
|
13
|
+
export declare function assertExists<T>(t: T, message?: AssertMessage): NonNullable<T>;
|
|
14
|
+
/**
|
|
15
|
+
* @param {*} n
|
|
16
|
+
* @returns string
|
|
17
|
+
*/
|
|
18
|
+
export declare function assertString(n: unknown, message?: AssertMessage): string;
|
|
19
|
+
/**
|
|
20
|
+
* Compile time assertion that no value will used at this point in control flow.
|
|
21
|
+
*/
|
|
22
|
+
export declare function checkExhaustive(value: never, message?: AssertMessage): never;
|
|
23
|
+
export {};
|
package/build/assert.js
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Throw an error when a condition is not met.
|
|
3
|
+
*/
|
|
4
|
+
export function assert(condition, message = "Assertion failed") {
|
|
5
|
+
if (!condition) {
|
|
6
|
+
throw new Error(message instanceof Function ? message() : message);
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Given a value, return it if it is not null nor undefined. Otherwise throw an
|
|
11
|
+
* error.
|
|
12
|
+
*
|
|
13
|
+
* @template T
|
|
14
|
+
* @returns {NonNullable<T>}
|
|
15
|
+
*/
|
|
16
|
+
export function assertExists(t, message = "Assertion failed: value does not exist") {
|
|
17
|
+
assert(t != null, message);
|
|
18
|
+
return t;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* @param {*} n
|
|
22
|
+
* @returns string
|
|
23
|
+
*/
|
|
24
|
+
export function assertString(n, message = () => `Assertion failed: ${n} is not a string`) {
|
|
25
|
+
assert(typeof n == "string", message);
|
|
26
|
+
return n;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Compile time assertion that no value will used at this point in control flow.
|
|
30
|
+
*/
|
|
31
|
+
export function checkExhaustive(value, message = `Unexpected value ${value}`) {
|
|
32
|
+
throw new Error(message instanceof Function ? message() : message);
|
|
33
|
+
}
|
package/build/case.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function dashCase(identifier: string): string;
|
package/build/case.js
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { display } from "../display.js";
|
|
2
|
+
import { FC } from "../dom/fc.js";
|
|
3
|
+
import { a, li, ul } from "../dom/html.js";
|
|
4
|
+
const ButtonBar = FC("button-bar", (el, { value, values, events }) => ul({ class: "ButtonBar__wrapper" }, ...values.map((option) => li(a({
|
|
5
|
+
href: "#",
|
|
6
|
+
class: `ButtonBar__${`${option}`.replace(/\s+/g, "_").toLowerCase()}
|
|
7
|
+
${option === value ? "" : "secondary"}
|
|
8
|
+
`.replace(/[\n\s]+/, " "),
|
|
9
|
+
events: {
|
|
10
|
+
click: (e) => {
|
|
11
|
+
e.preventDefault();
|
|
12
|
+
events.onSelect(option);
|
|
13
|
+
},
|
|
14
|
+
},
|
|
15
|
+
}, display(option))))));
|
|
16
|
+
export default ButtonBar;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export interface InlineEditState {
|
|
2
|
+
mode: number;
|
|
3
|
+
value: string;
|
|
4
|
+
}
|
|
5
|
+
export declare const InlineEdit: import("../dom/fc.js").FCComponentCtor<{
|
|
6
|
+
mode?: number | undefined;
|
|
7
|
+
value: string;
|
|
8
|
+
events: {
|
|
9
|
+
change: (value: string) => void;
|
|
10
|
+
};
|
|
11
|
+
}, InlineEditState>;
|
|
12
|
+
export default InlineEdit;
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { width } from "../dom/css/sizing.js";
|
|
2
|
+
import { FC, State } from "../dom/fc.js";
|
|
3
|
+
import { input, span } from "../dom/html.js";
|
|
4
|
+
const Mode = { VIEW: 0, EDIT: 1 };
|
|
5
|
+
export const InlineEdit = FC("inline-edit", (el, { mode = Mode.VIEW, value, events }) => {
|
|
6
|
+
const state = (el[State] ??= { mode, value });
|
|
7
|
+
const render = () => {
|
|
8
|
+
switch (state.mode) {
|
|
9
|
+
case Mode.EDIT:
|
|
10
|
+
return edit();
|
|
11
|
+
case Mode.VIEW:
|
|
12
|
+
return view();
|
|
13
|
+
default:
|
|
14
|
+
return span();
|
|
15
|
+
}
|
|
16
|
+
};
|
|
17
|
+
const view = () => span({
|
|
18
|
+
style: { cursor: "text", ...width("full", "inline") },
|
|
19
|
+
events: {
|
|
20
|
+
click: () => {
|
|
21
|
+
state.mode = Mode.EDIT;
|
|
22
|
+
el.update(render());
|
|
23
|
+
},
|
|
24
|
+
},
|
|
25
|
+
}, state.value ?? "");
|
|
26
|
+
const edit = () => {
|
|
27
|
+
const edit = span({ style: { display: "block", position: "relative" } }, input({
|
|
28
|
+
style: {
|
|
29
|
+
zIndex: "10",
|
|
30
|
+
position: "absolute",
|
|
31
|
+
left: "0",
|
|
32
|
+
marginTop: "-0.375rem",
|
|
33
|
+
},
|
|
34
|
+
events: {
|
|
35
|
+
blur: ({ target }) => events.change(target?.value ?? ""),
|
|
36
|
+
},
|
|
37
|
+
type: "text",
|
|
38
|
+
value: state.value,
|
|
39
|
+
}), "\u00a0" // Hack to get the span to take up space
|
|
40
|
+
);
|
|
41
|
+
setTimeout(() => {
|
|
42
|
+
edit.dispatchEvent(new Event("focus"));
|
|
43
|
+
});
|
|
44
|
+
return edit;
|
|
45
|
+
};
|
|
46
|
+
return render();
|
|
47
|
+
});
|
|
48
|
+
export default InlineEdit;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { DOMElement, Updatable } from "../dom/dom.js";
|
|
2
|
+
import { Logger } from "../log.js";
|
|
3
|
+
export interface HTMLLogger extends Logger {
|
|
4
|
+
root: Updatable<DOMElement>;
|
|
5
|
+
}
|
|
6
|
+
export declare function isHTMLLogger(logger: Logger): logger is HTMLLogger;
|
|
7
|
+
export declare function makeHTMLLogger(name: string): HTMLLogger;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { display } from "../display.js";
|
|
2
|
+
import { div, span, ul, li, pre, code } from "../dom/html.js";
|
|
3
|
+
import { LEVEL } from "../log.js";
|
|
4
|
+
export function isHTMLLogger(logger) {
|
|
5
|
+
return logger.root != undefined;
|
|
6
|
+
}
|
|
7
|
+
export function makeHTMLLogger(name) {
|
|
8
|
+
let log;
|
|
9
|
+
const root = div(div(span(name)), (log = ul()));
|
|
10
|
+
const logger = { level: LEVEL.INFO, root };
|
|
11
|
+
function append(message) {
|
|
12
|
+
log.appendChild(li(pre(code(message))));
|
|
13
|
+
}
|
|
14
|
+
const logAt = (level) => (message) => level >= (logger.level ?? LEVEL.ERROR)
|
|
15
|
+
? append(display(message))
|
|
16
|
+
: undefined;
|
|
17
|
+
logger.debug = logAt(LEVEL.VERBOSE);
|
|
18
|
+
logger.info = logAt(LEVEL.INFO);
|
|
19
|
+
logger.warn = logAt(LEVEL.WARN);
|
|
20
|
+
logger.error = logAt(LEVEL.ERROR);
|
|
21
|
+
return logger;
|
|
22
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { EventHandler } from "../dom/dom.js";
|
|
2
|
+
export declare const Select: import("../dom/fc.js").FCComponentCtor<{
|
|
3
|
+
name: string;
|
|
4
|
+
value: string;
|
|
5
|
+
events: {
|
|
6
|
+
change: EventHandler;
|
|
7
|
+
};
|
|
8
|
+
disabled: boolean;
|
|
9
|
+
options: [
|
|
10
|
+
string,
|
|
11
|
+
string
|
|
12
|
+
][];
|
|
13
|
+
}, {}>;
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
import { FC } from "../dom/fc.js";
|
|
2
|
+
import { option, select } from "../dom/html.js";
|
|
3
|
+
export const Select = FC("jiffies-select", (el, { name, events: { change }, disabled, value, options }) => select({ name, events: { change }, disabled }, ...options.map(([v, name]) => option({ value: v, selected: value === v }, `${name}`))));
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { UHTMLElement } from "../dom/html.js";
|
|
2
|
+
export interface VirtualScrollSettings {
|
|
3
|
+
minIndex: number;
|
|
4
|
+
maxIndex: number;
|
|
5
|
+
startIndex: number;
|
|
6
|
+
itemHeight: number;
|
|
7
|
+
count: number;
|
|
8
|
+
tolerance: number;
|
|
9
|
+
}
|
|
10
|
+
export interface VirtualScrollDataAdapter<T> {
|
|
11
|
+
(offset: number, limit: number): Iterable<T>;
|
|
12
|
+
}
|
|
13
|
+
export declare function arrayAdapter<T>(data: T[]): VirtualScrollDataAdapter<T>;
|
|
14
|
+
export interface VirtualScrollProps<T, U extends HTMLElement> {
|
|
15
|
+
settings: Partial<VirtualScrollSettings>;
|
|
16
|
+
get: VirtualScrollDataAdapter<T>;
|
|
17
|
+
row: (t: T) => UHTMLElement<U>;
|
|
18
|
+
}
|
|
19
|
+
export declare function fillVirtualScrollSettings(settings: Partial<VirtualScrollSettings>): VirtualScrollSettings;
|
|
20
|
+
export declare function initialState<T>(settings: VirtualScrollSettings): VirtualScrollState<T>;
|
|
21
|
+
export declare function getData<T>(minIndex: number, maxIndex: number, offset: number, limit: number, get: VirtualScrollDataAdapter<T>): T[];
|
|
22
|
+
export declare function doScroll<T>(scrollTop: number, state: VirtualScrollState<T>, get: VirtualScrollDataAdapter<T>): {
|
|
23
|
+
scrollTop: number;
|
|
24
|
+
topPaddingHeight: number;
|
|
25
|
+
bottomPaddingHeight: number;
|
|
26
|
+
data: T[];
|
|
27
|
+
};
|
|
28
|
+
interface VirtualScrollState<T, U extends HTMLElement = HTMLElement> {
|
|
29
|
+
settings: VirtualScrollSettings;
|
|
30
|
+
scrollTop: number;
|
|
31
|
+
bufferedItems: number;
|
|
32
|
+
totalHeight: number;
|
|
33
|
+
viewportHeight: number;
|
|
34
|
+
topPaddingHeight: number;
|
|
35
|
+
bottomPaddingHeight: number;
|
|
36
|
+
toleranceHeight: number;
|
|
37
|
+
data: T[];
|
|
38
|
+
rows: UHTMLElement<U>[];
|
|
39
|
+
}
|
|
40
|
+
export declare const VirtualScroll: import("../dom/fc.js").FCComponentCtor<VirtualScrollProps<unknown, HTMLElement>, VirtualScrollState<unknown, HTMLElement>>;
|
|
41
|
+
export default VirtualScroll;
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { debounce } from "../debounce.js";
|
|
2
|
+
import { FC, State } from "../dom/fc.js";
|
|
3
|
+
import { div } from "../dom/html.js";
|
|
4
|
+
export function arrayAdapter(data) {
|
|
5
|
+
return (offset, limit) => data.slice(offset, offset + limit);
|
|
6
|
+
}
|
|
7
|
+
export function fillVirtualScrollSettings(settings) {
|
|
8
|
+
const { minIndex = 0, maxIndex = 1, startIndex = 0, itemHeight = 20, count = maxIndex - minIndex + 1, tolerance = count, } = settings;
|
|
9
|
+
return { minIndex, maxIndex, startIndex, itemHeight, count, tolerance };
|
|
10
|
+
}
|
|
11
|
+
export function initialState(settings) {
|
|
12
|
+
// From Denis Hilt, https://blog.logrocket.com/virtual-scrolling-core-principles-and-basic-implementation-in-react/
|
|
13
|
+
const { minIndex, maxIndex, startIndex, itemHeight, count, tolerance } = settings;
|
|
14
|
+
const bufferedItems = count + 2 * tolerance;
|
|
15
|
+
const itemsAbove = Math.max(0, startIndex - tolerance - minIndex);
|
|
16
|
+
const viewportHeight = count * itemHeight;
|
|
17
|
+
const totalHeight = (maxIndex - minIndex + 1) * itemHeight;
|
|
18
|
+
const toleranceHeight = tolerance * itemHeight;
|
|
19
|
+
const bufferHeight = viewportHeight + 2 * toleranceHeight;
|
|
20
|
+
const topPaddingHeight = itemsAbove * itemHeight;
|
|
21
|
+
const bottomPaddingHeight = totalHeight - (topPaddingHeight + bufferHeight);
|
|
22
|
+
return {
|
|
23
|
+
scrollTop: 0,
|
|
24
|
+
settings,
|
|
25
|
+
viewportHeight,
|
|
26
|
+
totalHeight,
|
|
27
|
+
toleranceHeight,
|
|
28
|
+
bufferedItems,
|
|
29
|
+
topPaddingHeight,
|
|
30
|
+
bottomPaddingHeight,
|
|
31
|
+
data: [],
|
|
32
|
+
rows: [],
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
export function getData(minIndex, maxIndex, offset, limit, get) {
|
|
36
|
+
const start = Math.max(0, minIndex, offset);
|
|
37
|
+
const end = Math.min(maxIndex, offset + limit - 1);
|
|
38
|
+
const data = get(start, end - start);
|
|
39
|
+
return [...data];
|
|
40
|
+
}
|
|
41
|
+
export function doScroll(scrollTop, state, get) {
|
|
42
|
+
const { totalHeight, toleranceHeight, bufferedItems, settings: { itemHeight, minIndex, maxIndex }, } = state;
|
|
43
|
+
const index = minIndex + Math.floor((scrollTop - toleranceHeight) / itemHeight);
|
|
44
|
+
const data = getData(minIndex, maxIndex, index, bufferedItems, get);
|
|
45
|
+
const topPaddingHeight = Math.max((index - minIndex) * itemHeight, 0);
|
|
46
|
+
const bottomPaddingHeight = Math.max(totalHeight - (topPaddingHeight + data.length * itemHeight), 0);
|
|
47
|
+
return { scrollTop, topPaddingHeight, bottomPaddingHeight, data };
|
|
48
|
+
}
|
|
49
|
+
// export interface VirtualScroll<T, U extends HTMLElement> {
|
|
50
|
+
// state: VirtualScrollState<T>;
|
|
51
|
+
// rows: UHTMLElement<U>[];
|
|
52
|
+
// }
|
|
53
|
+
export const VirtualScroll = FC("virtual-scroll", (element, props) => {
|
|
54
|
+
const settings = fillVirtualScrollSettings(props.settings);
|
|
55
|
+
const state = (element[State] = {
|
|
56
|
+
...initialState(settings),
|
|
57
|
+
...element[State],
|
|
58
|
+
});
|
|
59
|
+
const scrollTo = ({ target } = { target: state }) => {
|
|
60
|
+
const scrollTop = target?.scrollTop ?? state.topPaddingHeight;
|
|
61
|
+
const updatedSate = {
|
|
62
|
+
...state,
|
|
63
|
+
...doScroll(scrollTop, state, props.get),
|
|
64
|
+
};
|
|
65
|
+
setState(updatedSate);
|
|
66
|
+
};
|
|
67
|
+
const viewportElement = div({
|
|
68
|
+
style: { height: `${state.viewportHeight}px`, overflowY: "scroll" },
|
|
69
|
+
events: { scroll: debounce(scrollTo, 0) },
|
|
70
|
+
});
|
|
71
|
+
setTimeout(() => {
|
|
72
|
+
viewportElement.scroll({ top: state.scrollTop });
|
|
73
|
+
});
|
|
74
|
+
const setState = (newState) => {
|
|
75
|
+
state.scrollTop = newState.scrollTop;
|
|
76
|
+
state.topPaddingHeight = newState.topPaddingHeight;
|
|
77
|
+
state.bottomPaddingHeight = newState.bottomPaddingHeight;
|
|
78
|
+
state.data = newState.data;
|
|
79
|
+
element[State].rows = state.data.map(props.row);
|
|
80
|
+
viewportElement.update(div({
|
|
81
|
+
class: "VirtualScroll__topPadding",
|
|
82
|
+
style: { height: `${state.topPaddingHeight}px` },
|
|
83
|
+
}), ...(element[State].rows ?? []).map((row, i) => div({
|
|
84
|
+
class: `VirtualScroll__item_${i}`,
|
|
85
|
+
style: { height: `${settings.itemHeight}px` },
|
|
86
|
+
}, row)), div({
|
|
87
|
+
class: "VirtualScroll__bottomPadding",
|
|
88
|
+
style: { height: `${state.bottomPaddingHeight}px` },
|
|
89
|
+
}));
|
|
90
|
+
};
|
|
91
|
+
scrollTo();
|
|
92
|
+
return viewportElement;
|
|
93
|
+
});
|
|
94
|
+
export default VirtualScroll;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { State } from "../dom/fc.js";
|
|
2
|
+
import { div } from "../dom/html.js";
|
|
3
|
+
import { describe, it, expect } from "../scope/index.js";
|
|
4
|
+
import VirtualScroll, { arrayAdapter, } from "./virtual_scroll.js";
|
|
5
|
+
describe("VirtualScroll", () => {
|
|
6
|
+
it("tracks scroll position", () => {
|
|
7
|
+
const data = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
|
|
8
|
+
const props = {
|
|
9
|
+
settings: { count: 3, startIndex: 2 },
|
|
10
|
+
get: arrayAdapter(data),
|
|
11
|
+
row: (i) => div(`${i}`),
|
|
12
|
+
};
|
|
13
|
+
// @ts-ignore TODO(TFC)
|
|
14
|
+
const scroll = VirtualScroll(props);
|
|
15
|
+
expect(scroll[State].bufferedItems).toBe(9);
|
|
16
|
+
expect(scroll[State].data).toEqual([0, 1, 2, 3, 4, 5, 6, 7, 8]);
|
|
17
|
+
//expect(scroll.state.topPaddingHeight).toBe(0);
|
|
18
|
+
expect(scroll[State].viewportHeight).toBe(60);
|
|
19
|
+
//expect(scroll.state.totalHeight).toBe(200);
|
|
20
|
+
});
|
|
21
|
+
});
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { Err, Result } from "./result.js";
|
|
2
|
+
export declare const Enter: unique symbol;
|
|
3
|
+
export declare const Exit: unique symbol;
|
|
4
|
+
export interface Context {
|
|
5
|
+
[Enter]: () => void;
|
|
6
|
+
[Exit]: () => void;
|
|
7
|
+
}
|
|
8
|
+
export interface Operation<T, E extends Error, C extends Context> {
|
|
9
|
+
(c: C): T | Result<T, E>;
|
|
10
|
+
}
|
|
11
|
+
export interface AsyncOperation<T, E extends Error, C extends Context> {
|
|
12
|
+
(c: C): Promise<T | Result<T, E>>;
|
|
13
|
+
}
|
|
14
|
+
export declare function using<T, E extends Error, C extends Context>(context: C | (() => C) | Operation<T, E, C>, operation?: Operation<T, E, C>, normalizeError?: (e: Error | unknown | any) => Err<E>): Result<T, E>;
|
|
15
|
+
export declare function asyncUsing<T, E extends Error, C extends Context>(context: C | (() => Promise<C>), operation: AsyncOperation<T, E, C>, normalizeError?: (e: Error | unknown | any) => Err<E>): Promise<Result<T, E>>;
|
package/build/context.js
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { Ok, Err, isResult } from "./result.js";
|
|
2
|
+
export const Enter = Symbol("Context Enter");
|
|
3
|
+
export const Exit = Symbol("Context Exit");
|
|
4
|
+
export function using(context, operation, normalizeError = (e) => Err(e)) {
|
|
5
|
+
if (typeof context == "function") {
|
|
6
|
+
if (context.length == 1) {
|
|
7
|
+
operation = context;
|
|
8
|
+
context = {};
|
|
9
|
+
}
|
|
10
|
+
else {
|
|
11
|
+
context = context();
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
let result;
|
|
15
|
+
try {
|
|
16
|
+
context[Enter]();
|
|
17
|
+
const op = operation(context);
|
|
18
|
+
result = isResult(op) ? op : Ok(op);
|
|
19
|
+
}
|
|
20
|
+
catch (e) {
|
|
21
|
+
result = normalizeError(e);
|
|
22
|
+
}
|
|
23
|
+
finally {
|
|
24
|
+
context[Exit]();
|
|
25
|
+
}
|
|
26
|
+
return result;
|
|
27
|
+
}
|
|
28
|
+
export async function asyncUsing(context, operation, normalizeError = (e) => Err(e)) {
|
|
29
|
+
context = typeof context == "function" ? await context() : context;
|
|
30
|
+
let result;
|
|
31
|
+
try {
|
|
32
|
+
context[Enter]();
|
|
33
|
+
const op = await operation(context);
|
|
34
|
+
result = isResult(op) ? op : Ok(op);
|
|
35
|
+
}
|
|
36
|
+
catch (e) {
|
|
37
|
+
result = normalizeError(e);
|
|
38
|
+
}
|
|
39
|
+
finally {
|
|
40
|
+
context[Exit]();
|
|
41
|
+
}
|
|
42
|
+
return result;
|
|
43
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { Enter, Exit, using } from "./context.js";
|
|
2
|
+
import { Err, isErr, isOk, Ok, unwrap } from "./result.js";
|
|
3
|
+
import { describe, it } from "./scope/describe.js";
|
|
4
|
+
import { expect } from "./scope/expect.js";
|
|
5
|
+
describe("Context", () => {
|
|
6
|
+
it("performs an operation using a context", () => {
|
|
7
|
+
const context = TestContext();
|
|
8
|
+
const result = using(context, () => Ok(5));
|
|
9
|
+
expect(unwrap(result)).toBe(5);
|
|
10
|
+
expect(context.initialized).toBe(true);
|
|
11
|
+
expect(context.completed).toBe(true);
|
|
12
|
+
});
|
|
13
|
+
it("reports the result of a thrown error", () => {
|
|
14
|
+
const context = TestContext();
|
|
15
|
+
const result = using(context, () => {
|
|
16
|
+
throw new Error("Failed");
|
|
17
|
+
});
|
|
18
|
+
expect(isErr(result)).toBe(true);
|
|
19
|
+
expect(Err(result)).toMatchObject({
|
|
20
|
+
message: "Failed",
|
|
21
|
+
});
|
|
22
|
+
});
|
|
23
|
+
it("passes the context to the operation", () => {
|
|
24
|
+
const op = using(TestContext, ({ initialized, completed }) => ({
|
|
25
|
+
initialized,
|
|
26
|
+
completed,
|
|
27
|
+
}));
|
|
28
|
+
expect(isOk(op)).toBe(true);
|
|
29
|
+
const { completed, initialized } = unwrap(op);
|
|
30
|
+
expect(initialized).toBe(true);
|
|
31
|
+
expect(completed).toBe(false);
|
|
32
|
+
});
|
|
33
|
+
});
|
|
34
|
+
function TestContext() {
|
|
35
|
+
const context = {
|
|
36
|
+
[Enter]: () => {
|
|
37
|
+
context.initialized = true;
|
|
38
|
+
},
|
|
39
|
+
[Exit]: () => {
|
|
40
|
+
context.completed = true;
|
|
41
|
+
},
|
|
42
|
+
initialized: false,
|
|
43
|
+
completed: false,
|
|
44
|
+
};
|
|
45
|
+
return context;
|
|
46
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function debounce(fn: (...args: any[]) => any, ms?: number): (...args: Parameters<typeof fn>) => ReturnType<typeof fn>;
|
package/build/display.js
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { Properties } from "../types/css.js";
|
|
2
|
+
import { Side, Size } from "./constants.js";
|
|
3
|
+
export declare function rounded(size?: Size, side?: Side): Properties<0 | (string & {}), string & {}>;
|
|
4
|
+
export declare function border({ side, style, radius, width, color, }: {
|
|
5
|
+
side?: Side;
|
|
6
|
+
style?: "solid" | "dotted" | "dashed" | "double" | "none";
|
|
7
|
+
radius?: Size;
|
|
8
|
+
width?: 0 | 1 | 2 | 4 | 8;
|
|
9
|
+
color?: string;
|
|
10
|
+
}): {};
|
|
11
|
+
export declare function inset(width: 0 | 1 | 2 | 4 | 8, color1?: string, color2?: string): {};
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { isSide, getSize, getSide } from "./core.js";
|
|
2
|
+
export function rounded(size = "", side = "") {
|
|
3
|
+
if (isSide(size)) {
|
|
4
|
+
side = size;
|
|
5
|
+
size = "";
|
|
6
|
+
}
|
|
7
|
+
const sized = getSize(size);
|
|
8
|
+
return getSide(side).reduce((prev, curr) => {
|
|
9
|
+
if (curr === "") {
|
|
10
|
+
prev.borderRadius = sized;
|
|
11
|
+
}
|
|
12
|
+
else {
|
|
13
|
+
// @ts-ignore
|
|
14
|
+
prev[`border${curr}Radius`] = sized;
|
|
15
|
+
}
|
|
16
|
+
return prev;
|
|
17
|
+
}, {});
|
|
18
|
+
}
|
|
19
|
+
export function border({ side = "", style = "solid", radius = "", width = 1, color = "black", }) {
|
|
20
|
+
return {};
|
|
21
|
+
}
|
|
22
|
+
export function inset(width, color1 = "gray", color2 = "lightgray") {
|
|
23
|
+
return {
|
|
24
|
+
...border({ side: "tl", width, color: color1, radius: "none" }),
|
|
25
|
+
...border({ side: "br", width, color: color2, radius: "none" }),
|
|
26
|
+
};
|
|
27
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
export declare const Sizes: {
|
|
2
|
+
none: string;
|
|
3
|
+
sm: string;
|
|
4
|
+
"": string;
|
|
5
|
+
md: string;
|
|
6
|
+
lg: string;
|
|
7
|
+
xl: string;
|
|
8
|
+
"2xl": string;
|
|
9
|
+
"3xl": string;
|
|
10
|
+
full: string;
|
|
11
|
+
};
|
|
12
|
+
export declare const Sides: {
|
|
13
|
+
"": string;
|
|
14
|
+
t: string;
|
|
15
|
+
r: string;
|
|
16
|
+
l: string;
|
|
17
|
+
b: string;
|
|
18
|
+
tl: string;
|
|
19
|
+
tr: string;
|
|
20
|
+
bl: string;
|
|
21
|
+
br: string;
|
|
22
|
+
};
|
|
23
|
+
export declare const Widths: {
|
|
24
|
+
"1/4": string;
|
|
25
|
+
"1/2": string;
|
|
26
|
+
"3/4": string;
|
|
27
|
+
full: string;
|
|
28
|
+
};
|
|
29
|
+
export declare type Size = keyof typeof Sizes;
|
|
30
|
+
export declare type Side = keyof typeof Sides;
|
|
31
|
+
export declare type Width = keyof typeof Widths;
|