playhtml 0.1.6 → 0.1.8

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023 Spencer Chang
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -5,20 +5,30 @@ _interactive, collaborative html elements with a single data attribute_
5
5
  playhtml is a library for magically creating collaborative interactive HTML elements that persist their state across sessions. For example, you can create a movable piece of HTML "furniture" by adding the `can-move` attribute:
6
6
 
7
7
  ```html
8
- <div can-move style="font-size: 80px">🛋</div>
8
+ <div id="couch" can-move style="font-size: 80px">🛋</div>
9
9
  ```
10
10
 
11
11
  This is designed as an open library for anyone to add on new interactions and capabilities. To get started, see the [New Capabilities](#new-capabilities).
12
12
 
13
13
  https://github.com/spencerc99/playhtml/assets/14796580/00e84e15-2c1c-4b4b-8e15-2af22f39db7a
14
14
 
15
+ If you enjoy this, please consider [sponsoring the project or sending a small donation](https://github.com/sponsors/spencerc99). This helps ensure that the library is maintained and improved over time and funds the hosting costs for the syncing and persistence services.
16
+
15
17
  ## Usage
16
18
 
17
- To use this library, you can import the library and the associated styles from a CDN (in this case we will use [unpkg.com](https://unpkg.com)). Please make sure to do this after all the HTML elements on the page.
19
+ To use this library, you can import the library and the associated styles from a CDN (in this case we will use [unpkg.com](https://unpkg.com)). Please make sure to do this after all the HTML elements on the page and ensure that the HTML elements you are "magic-ifying" have an `id` set.
18
20
 
19
21
  ```html
20
22
  <body>
21
23
  <!-- ... html elements here -->
24
+ <!-- valid example -->
25
+ <img
26
+ src="https://media2.giphy.com/media/lL7A3Li0YtFHq/giphy.gif?cid=ecf05e47ah89o71gzz7ke7inrgb1ai1xcbrjnqdf7o890118&ep=v1_stickers_search&rid=giphy.gif"
27
+ can-move
28
+ />
29
+ id="openSign"
30
+ <!-- INVALID EXAMPLE <img src="https://media2.giphy.com/media/lL7A3Li0YtFHq/giphy.gif?cid=ecf05e47ah89o71gzz7ke7inrgb1ai1xcbrjnqdf7o890118&ep=v1_stickers_search&rid=giphy.gif" can-move /> -->
31
+ <!-- import the script -->
22
32
  <script type="module" src="https://unpkg.com/playhtml"></script>
23
33
  <link rel="stylesheet" href="https://unpkg.com/playhtml/dist/style.css" />
24
34
  </body>
@@ -32,18 +42,51 @@ If you have dynamic elements that are hydrated after the initial load, you can c
32
42
  </script>
33
43
  ```
34
44
 
35
- TODO: add import for React.
45
+ ## Capabilities
46
+
47
+ A full list can be found in `types.ts` under `TagType`.
48
+
49
+ ### `can-move`
50
+
51
+ Creates a movable object using 2D `translate` on the element. Dragging the element around will move it
52
+
53
+ **troubleshooting**
54
+
55
+ - This currently doesn't work on `inline` display elements.
56
+ - This currently doesn't work on touch screens.
57
+
58
+ ### `can-spin`
59
+
60
+ Creates a rotatable object using a `rotate` `transform` on the element. Dragging the element to the left or right will rotate it counter-clockwise and clockwise respectively.
61
+
62
+ ### `can-toggle`
63
+
64
+ Creates an object that can be switched on and off. Clicking the element will toggle the `clicked` class on the element.
36
65
 
37
- ## New Capabilities
66
+ ### `can-grow`
67
+
68
+ Creates an object that can be resized using a `scale` `transform`. Clicking the object will grow it, clicking with <kbd>ALT</kbd> will shrink it. Currently, the max size is 2x the original size and the min size is 1/2 the original size.
69
+
70
+ ### `can-post`
71
+
72
+ Creates a communal forum from a `form` element. The form will sync any new submissions including all the `input` elements in the form, using their `name` property as the key and their value as the value. New messages will be currently prepended to the element with the `guestbookMessages` ID. TODO: make this generic and take user input
73
+
74
+ ---
75
+
76
+ To add new ones, please find contributor guidelines in [New Capabilities](#new-capabilities).
77
+
78
+ ## Contributing
79
+
80
+ ### New Capabilities
38
81
 
39
82
  `playhtml` is designed to be a collective library of magical capabilities that anyone can attach to arbitrary HTML elements. If you have an idea for a new capability, please first ensure that there is not a duplicate existing one in the current library (see `TagType` in `types.ts`). Please make a proposal for the capability you would like to add by opening an issue with the `new-capability` label.
40
83
 
41
84
  To contribute your capability, see sample PR (TODO: LINK).
42
85
 
43
- ## Contributing
86
+ ---
44
87
 
45
88
  Outside of contributing new capabilities, feel free to submit any issues or PRs for bugs or improvements to the core of the library.
46
89
 
47
90
  ## Support & Maintenance
48
91
 
49
- If you enjoy the idea of this library or like using it, please consider [sponsoring the project](https://github.com/sponsors/spencerc99). This will help ensure that the library is maintained and improved over time.
92
+ Thank you for considering reading this little README and browing this project! I'd love to see you share the library and what you've made with it to me and with your friends. And if you enjoy using this, please consider [sponsoring the project or sending a small donation](https://github.com/sponsors/spencerc99). This helps ensure that the library is maintained and improved over time and funds the hosting costs for the syncing and persistence services.
@@ -1,34 +1,46 @@
1
1
  /// <reference lib="dom" />
2
2
  import { TagType } from "./types";
3
3
  type ModifierKey = "ctrlKey" | "altKey" | "shiftKey" | "metaKey";
4
- interface ElementInitializer<T = any> {
4
+ interface ElementInitializer<T = any, U = any> {
5
5
  defaultData: T;
6
- updateElement: (element: HTMLElement, data: T) => void;
7
- onClick?: (e: MouseEvent, data: T, element: HTMLElement) => T;
8
- onDrag?: (e: MouseEvent, data: T, element: HTMLElement) => T;
9
- onDragStart?: (e: MouseEvent, data: T, element: HTMLElement) => T;
10
- onMouseEnter?: (e: MouseEvent, data: T, element: HTMLElement) => T;
11
- onKeyDown?: (e: KeyboardEvent, data: T, element: HTMLElement) => T;
12
- onKeyUp?: (e: KeyboardEvent, data: T, element: HTMLElement) => T;
6
+ defaultLocalData?: U;
7
+ updateElement: (data: ElementEventHandlerData<T, U>) => void;
8
+ onClick?: (e: MouseEvent, eventData: ElementEventHandlerData<T, U>) => void;
9
+ onDrag?: (e: MouseEvent, eventData: ElementEventHandlerData<T, U>) => void;
10
+ onDragStart?: (e: MouseEvent, eventData: ElementEventHandlerData<T, U>) => void;
11
+ onMouseEnter?: (e: MouseEvent, eventData: ElementEventHandlerData<T, U>) => void;
12
+ onKeyDown?: (e: KeyboardEvent, eventData: ElementEventHandlerData<T, U>) => void;
13
+ onKeyUp?: (e: KeyboardEvent, eventData: ElementEventHandlerData<T, U>) => void;
14
+ onSubmit?: (e: SubmitEvent, eventData: ElementEventHandlerData<T, U>) => void;
13
15
  resetShortcut?: ModifierKey;
14
16
  debounceMs?: number;
15
17
  }
16
- export interface ElementData<T = any> extends ElementInitializer<T> {
18
+ export interface ElementData<T = any, U = any> extends ElementInitializer<T> {
17
19
  data?: T;
20
+ localData?: U;
18
21
  element: HTMLElement;
19
22
  onChange: (data: T) => void;
20
23
  }
24
+ interface ElementEventHandlerData<T = any, U = any> {
25
+ data: T;
26
+ localData: U;
27
+ element: HTMLElement;
28
+ setData: (data: T) => void;
29
+ setLocalData: (data: U) => void;
30
+ }
21
31
  export declare const TagTypeToElement: Record<TagType, ElementInitializer>;
22
- export declare class ElementHandler<T = any> {
32
+ export declare class ElementHandler<T = any, U = any> {
23
33
  defaultData: T;
34
+ localData: U;
24
35
  element: HTMLElement;
25
36
  _data: T;
26
37
  onChange: (data: T) => void;
27
38
  debouncedOnChange: (data: T) => void;
28
39
  resetShortcut?: ModifierKey;
29
- updateElement: (element: HTMLElement, data: T) => void;
30
- constructor({ element, onChange, defaultData, data, updateElement, onClick, onDrag, onDragStart, onMouseEnter, onKeyDown, onKeyUp, resetShortcut, debounceMs, }: ElementData<T>);
40
+ updateElement: (data: ElementEventHandlerData<T, U>) => void;
41
+ constructor({ element, onChange, defaultData, defaultLocalData, data, updateElement, onClick, onDrag, onDragStart, onMouseEnter, onKeyDown, onKeyUp, onSubmit, resetShortcut, debounceMs, }: ElementData<T>);
31
42
  get data(): T;
43
+ setLocalData(localData: U): void;
32
44
  /**
33
45
  * // PRIVATE USE ONLY \\
34
46
  *
@@ -37,10 +49,11 @@ export declare class ElementHandler<T = any> {
37
49
  * (e.g. calling `updateElement` and `onChange`)
38
50
  */
39
51
  set __data(data: T);
52
+ getEventHandlerData(): ElementEventHandlerData<T, U>;
40
53
  /**
41
54
  * Public-use setter for data that makes the change to all clients.
42
55
  */
43
- set data(data: T);
56
+ setData(data: T): void;
44
57
  setDataDebounced(data: T): void;
45
58
  /**
46
59
  * Resets the element to its default state.
package/dist/main.d.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  /// <reference lib="dom" />
2
2
  import * as Y from "yjs";
3
3
  export declare const globalData: Y.Map<Y.Map<any>>;
4
+ export declare function getElementFromId(id: string): HTMLElement | null;
4
5
  /**
5
6
  * Sets up any playhtml elements that are currently on the page.
6
7
  * Can be repeatedly called to set up new elements without affecting existing ones.
Binary file