-
Notifications
You must be signed in to change notification settings - Fork 4.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Interactivity API: Migrate everything to TypeScript, add missing comments and remove the use of "we" #58718
Closed
cbravobernal
wants to merge
13
commits into
trunk
from
nitpick/interactivity-avoid-we-multi-line-comments
Closed
Interactivity API: Migrate everything to TypeScript, add missing comments and remove the use of "we" #58718
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
f56cbca
Nitpicks regarding multiline comments and we word
cbravobernal 837e79a
More nitpicks
cbravobernal 0ee7771
Move to typescript v1
cbravobernal 323007e
Move import type to external dependency
cbravobernal 9199959
Typing portals
cbravobernal adcec5a
Add consistency to the types, like other packages
cbravobernal 7b53b37
Add typescript to init
cbravobernal 03b742c
wip type directives
cbravobernal 3eb836c
Wip, still typing
cbravobernal 1c0c8bd
More WIP
cbravobernal bb87272
More WIP typing
cbravobernal 5a7963b
Type directives file and store
cbravobernal 071db98
Typing
cbravobernal File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ | |
*/ | ||
import { h as createElement } from 'preact'; | ||
import { useContext, useMemo, useRef } from 'preact/hooks'; | ||
import type { DeepSignal } from 'deepsignal'; | ||
import { deepSignal, peek } from 'deepsignal'; | ||
|
||
/** | ||
|
@@ -13,11 +14,44 @@ import { deepSignal, peek } from 'deepsignal'; | |
import { useWatch, useInit } from './utils'; | ||
import { directive, getScope, getEvaluate } from './hooks'; | ||
import { kebabToCamelCase } from './utils/kebab-to-camelcase'; | ||
import type { DirectiveArgs } from './types'; | ||
|
||
const isObject = ( item ) => | ||
/** | ||
* Checks if the provided item is an object and not an array. | ||
* | ||
* @param item The item to check. | ||
* | ||
* @return Whether the item is an object. | ||
* | ||
* @example | ||
* isObject({}); // returns true | ||
* isObject([]); // returns false | ||
* isObject('string'); // returns false | ||
* isObject(null); // returns false | ||
*/ | ||
const isObject = ( item: any ): boolean => | ||
item && typeof item === 'object' && ! Array.isArray( item ); | ||
|
||
const mergeDeepSignals = ( target, source, overwrite ) => { | ||
/** | ||
* Recursively merges properties from the source object into the target object. | ||
* If `overwrite` is true, existing properties in the target object are overwritten by properties in the source object with the same key. | ||
* If `overwrite` is false, existing properties in the target object are preserved. | ||
* | ||
* @param target - The target object that properties are merged into. | ||
* @param source - The source object that properties are merged from. | ||
* @param overwrite - Whether to overwrite existing properties in the target object. | ||
* | ||
* @example | ||
* const target = { $key1: { peek: () => ({ a: 1 }) }, $key2: { peek: () => ({ b: 2 }) } }; | ||
* const source = { $key1: { peek: () => ({ a: 3 }) }, $key3: { peek: () => ({ c: 3 }) } }; | ||
* mergeDeepSignals(target, source, true); | ||
* // target is now { $key1: { peek: () => ({ a: 3 }) }, $key2: { peek: () => ({ b: 2 }) }, $key3: { peek: () => ({ c: 3 }) } } | ||
*/ | ||
const mergeDeepSignals = ( | ||
target: DeepSignal< any >, | ||
source: DeepSignal< any >, | ||
overwrite?: boolean | ||
) => { | ||
for ( const k in source ) { | ||
if ( isObject( peek( target, k ) ) && isObject( peek( source, k ) ) ) { | ||
mergeDeepSignals( | ||
|
@@ -43,10 +77,10 @@ const empty = ' '; | |
* Made by Cristian Bote (@cristianbote) for Goober. | ||
* https://unpkg.com/browse/[email protected]/src/core/astish.js | ||
* | ||
* @param {string} val CSS string. | ||
* @return {Object} CSS object. | ||
* @param val CSS string. | ||
* @return CSS object. | ||
*/ | ||
const cssStringToObject = ( val ) => { | ||
const cssStringToObject = ( val: string ): Object => { | ||
const tree = [ {} ]; | ||
let block, left; | ||
|
||
|
@@ -70,22 +104,21 @@ const cssStringToObject = ( val ) => { | |
* Creates a directive that adds an event listener to the global window or | ||
* document object. | ||
* | ||
* @param {string} type 'window' or 'document' | ||
* @return {void} | ||
* @param type 'window' or 'document' | ||
*/ | ||
const getGlobalEventDirective = | ||
( type ) => | ||
( { directives, evaluate } ) => { | ||
( type: 'window' | 'document' ) => | ||
( { directives, evaluate }: DirectiveArgs ): void => { | ||
directives[ `on-${ type }` ] | ||
.filter( ( { suffix } ) => suffix !== 'default' ) | ||
.forEach( ( entry ) => { | ||
useInit( () => { | ||
const cb = ( event ) => evaluate( entry, event ); | ||
const cb = ( event: Event ) => evaluate( entry, event ); | ||
const globalVar = type === 'window' ? window : document; | ||
globalVar.addEventListener( entry.suffix, cb ); | ||
return () => | ||
globalVar.removeEventListener( entry.suffix, cb ); | ||
}, [] ); | ||
} ); | ||
} ); | ||
}; | ||
|
||
|
@@ -121,6 +154,7 @@ export default () => { | |
</Provider> | ||
); | ||
} | ||
return undefined; | ||
}, | ||
{ priority: 5 } | ||
); | ||
|
@@ -195,7 +229,7 @@ export default () => { | |
} | ||
); | ||
|
||
// data-wp-style--[style-prop] | ||
// data-wp-style--[style-key] | ||
directive( 'style', ( { directives: { style }, element, evaluate } ) => { | ||
style | ||
.filter( ( { suffix } ) => suffix !== 'default' ) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using GetEvaluate is returning this error:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll take a look, deep signal types can be complex 🙂