playhtml 0.1.5 → 0.1.7

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,7 +5,7 @@ _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).
@@ -14,11 +14,19 @@ https://github.com/spencerc99/playhtml/assets/14796580/00e84e15-2c1c-4b4b-8e15-2
14
14
 
15
15
  ## Usage
16
16
 
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.
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 and ensure that the HTML elements you are "magic-ifying" have an `id` set.
18
18
 
19
19
  ```html
20
20
  <body>
21
21
  <!-- ... html elements here -->
22
+ <!-- valid example -->
23
+ <img
24
+ src="https://media2.giphy.com/media/lL7A3Li0YtFHq/giphy.gif?cid=ecf05e47ah89o71gzz7ke7inrgb1ai1xcbrjnqdf7o890118&ep=v1_stickers_search&rid=giphy.gif"
25
+ can-move
26
+ />
27
+ id="openSign"
28
+ <!-- INVALID EXAMPLE <img src="https://media2.giphy.com/media/lL7A3Li0YtFHq/giphy.gif?cid=ecf05e47ah89o71gzz7ke7inrgb1ai1xcbrjnqdf7o890118&ep=v1_stickers_search&rid=giphy.gif" can-move /> -->
29
+ <!-- import the script -->
22
30
  <script type="module" src="https://unpkg.com/playhtml"></script>
23
31
  <link rel="stylesheet" href="https://unpkg.com/playhtml/dist/style.css" />
24
32
  </body>
@@ -46,4 +54,4 @@ Outside of contributing new capabilities, feel free to submit any issues or PRs
46
54
 
47
55
  ## Support & Maintenance
48
56
 
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.
57
+ If you enjoy the idea of this library or like using it, please consider [sponsoring the project](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