sliftutils 0.40.0 → 0.41.0

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "sliftutils",
3
- "version": "0.40.0",
3
+ "version": "0.41.0",
4
4
  "main": "index.js",
5
5
  "license": "MIT",
6
6
  "files": [
@@ -0,0 +1,48 @@
1
+ import { observable } from "mobx";
2
+ import { list } from "socket-function/src/misc";
3
+ import { css } from "typesafecss";
4
+ import { URLParam } from "../render-utils/URLParam";
5
+ import { observer } from "../render-utils/observer";
6
+ import * as preact from "preact";
7
+
8
+ const exampleUrlParam = new URLParam("example", "");
9
+
10
+ @observer
11
+ export class ExamplePage extends preact.Component {
12
+ synced = observable({
13
+ count: 0,
14
+ });
15
+
16
+ onKeyDown = (e: KeyboardEvent) => {
17
+ // Skip if the current target is an ipnut
18
+ if (e.target instanceof HTMLInputElement || e.target instanceof HTMLTextAreaElement) {
19
+ return;
20
+ }
21
+ let hotkeySelector = `[data-hotkey="${e.code}"]`;
22
+ let elements = document.querySelectorAll(hotkeySelector);
23
+ for (let element of elements) {
24
+ (element as HTMLElement).click();
25
+ }
26
+ };
27
+ componentDidMount(): void {
28
+ document.addEventListener("keydown", this.onKeyDown);
29
+ }
30
+ componentWillUnmount(): void {
31
+ document.removeEventListener("keydown", this.onKeyDown);
32
+ }
33
+
34
+ render() {
35
+ return (
36
+ <div className={css.pad2(20)}>
37
+ <h1>Hello from Web! 3</h1>
38
+ <p>Count: {this.synced.count}</p>
39
+ <button onClick={() => this.synced.count++}>
40
+ Increment
41
+ </button>
42
+ <div>
43
+ {list(1000).map(x => <div key={x}>{x}</div>)}
44
+ </div>
45
+ </div>
46
+ );
47
+ }
48
+ }
package/web/Page.tsx ADDED
@@ -0,0 +1,59 @@
1
+ import preact from "preact";
2
+ import { css } from "typesafecss";
3
+ import { Anchor } from "../render-utils/Anchor";
4
+ import { URLParam } from "../render-utils/URLParam";
5
+ import { observer } from "../render-utils/observer";
6
+ import { ExamplePage } from "./ExamplePage";
7
+
8
+ export const pageURL = new URLParam("page");
9
+
10
+ @observer
11
+ export class Page extends preact.Component {
12
+ onKeyDown = (e: KeyboardEvent) => {
13
+ // Ignore if it is for an input, text area, etc
14
+ let ignore = (
15
+ e.target instanceof HTMLInputElement ||
16
+ e.target instanceof HTMLTextAreaElement ||
17
+ e.target instanceof HTMLSelectElement
18
+ );
19
+ if (ignore) return;
20
+
21
+ console.log("Checking hotkey", e.key, e);
22
+ let key = e.key;
23
+ if (e.ctrlKey) key = "Ctrl+" + key;
24
+ if (e.shiftKey) key = "Shift+" + key;
25
+ let hotkeyDataAttribute = `[data-hotkey="${key}"]`;
26
+ let el = document.querySelector<HTMLElement>(hotkeyDataAttribute);
27
+ if (el) {
28
+ e.stopPropagation();
29
+ e.preventDefault();
30
+ console.log("Found hotkey", e.key, el);
31
+ el.click();
32
+ }
33
+ };
34
+ render() {
35
+ let pages = [
36
+ {
37
+ key: "example",
38
+ content: <ExamplePage />
39
+ },
40
+ ];
41
+
42
+ let page = pages.find(p => p.key === pageURL.value) || pages[0];
43
+
44
+ return (
45
+ <div className={css.size("100vw", "100vh").vbox(0)}>
46
+ <div className={css.hbox(12).pad2(20)}>
47
+ {pages.map(p => (
48
+ <Anchor key={p.key} params={[[pageURL, p.key]]}>
49
+ {p.key}
50
+ </Anchor>
51
+ ))}
52
+ </div>
53
+ <div className={css.overflowAuto.fillBoth}>
54
+ {page.content}
55
+ </div>
56
+ </div>
57
+ );
58
+ }
59
+ }
package/web/browser.tsx CHANGED
@@ -5,53 +5,13 @@ import { css, isNode } from "typesafecss";
5
5
  import { list } from "socket-function/src/misc";
6
6
  import { enableHotReloading } from "../builders/hotReload";
7
7
  import { URLParam } from "../render-utils/URLParam";
8
+ import { Page } from "./Page";
8
9
 
9
- const exampleUrlParam = new URLParam("example", "");
10
-
11
- @observer
12
- class App extends preact.Component {
13
- synced = observable({
14
- count: 0,
15
- });
16
-
17
- onKeyDown = (e: KeyboardEvent) => {
18
- // Skip if the current target is an ipnut
19
- if (e.target instanceof HTMLInputElement || e.target instanceof HTMLTextAreaElement) {
20
- return;
21
- }
22
- let hotkeySelector = `[data-hotkey="${e.code}"]`;
23
- let elements = document.querySelectorAll(hotkeySelector);
24
- for (let element of elements) {
25
- (element as HTMLElement).click();
26
- }
27
- };
28
- componentDidMount(): void {
29
- document.addEventListener("keydown", this.onKeyDown);
30
- }
31
- componentWillUnmount(): void {
32
- document.removeEventListener("keydown", this.onKeyDown);
33
- }
34
-
35
- render() {
36
- return (
37
- <div className={css.pad2(20)}>
38
- <h1>Hello from Web! 3</h1>
39
- <p>Count: {this.synced.count}</p>
40
- <button onClick={() => this.synced.count++}>
41
- Increment
42
- </button>
43
- <div>
44
- {list(1000).map(x => <div key={x}>{x}</div>)}
45
- </div>
46
- </div>
47
- );
48
- }
49
- }
50
10
 
51
11
  async function main() {
52
12
  if (isNode()) return;
53
13
  await enableHotReloading({ port: 9877 });
54
- preact.render(<App />, document.getElementById("app")!);
14
+ preact.render(<Page />, document.getElementById("app")!);
55
15
  }
56
16
 
57
17
  main().catch(console.error);