-
Notifications
You must be signed in to change notification settings - Fork 103
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
94 changed files
with
5,819 additions
and
104 deletions.
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2022 SeungHwan-Lee <[email protected]> | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
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 |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# r-html - Tagged templates framework | ||
|
||
```ts | ||
import { | ||
css, | ||
defineCustomElement, | ||
FC, | ||
html, | ||
observable, | ||
render, | ||
} from '@dineug/r-html'; | ||
|
||
const incrementBtn = css` | ||
color: green; | ||
`; | ||
const decrementBtn = css` | ||
color: red; | ||
`; | ||
|
||
const Counter: FC = () => { | ||
const state = observable({ count: 0 }); | ||
|
||
return () => html` | ||
<div>Counter: ${state.count}</div> | ||
<button class=${incrementBtn} @click=${() => state.count++}> | ||
Increment | ||
</button> | ||
<button class=${decrementBtn} @click=${() => state.count--}> | ||
Decrement | ||
</button> | ||
`; | ||
}; | ||
|
||
const App: FC<{}, HTMLElement> = () => { | ||
return () => html`<${Counter} />`; | ||
}; | ||
|
||
defineCustomElement('my-app', { | ||
render: App, | ||
}); | ||
|
||
render(document.body, html`<my-app></my-app>`); | ||
``` |
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 |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="utf-8" /> | ||
<meta http-equiv="X-UA-Compatible" content="IE=edge" /> | ||
<meta name="viewport" content="width=device-width,initial-scale=1.0" /> | ||
<title>r-html</title> | ||
</head> | ||
<body> | ||
<script type="module" src="/src/index.dev.ts"></script> | ||
</body> | ||
</html> |
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 |
---|---|---|
@@ -0,0 +1,47 @@ | ||
{ | ||
"name": "@dineug/r-html", | ||
"version": "0.1.3", | ||
"private": true, | ||
"description": "Tagged templates framework", | ||
"type": "module", | ||
"main": "./dist/r-html.umd.cjs", | ||
"module": "./dist/r-html.js", | ||
"types": "./dist/index.d.ts", | ||
"exports": { | ||
".": { | ||
"import": { | ||
"types": "./dist/index.d.ts", | ||
"default": "./dist/r-html.js" | ||
}, | ||
"require": "./dist/r-html.umd.cjs" | ||
} | ||
}, | ||
"files": [ | ||
"dist/*.js", | ||
"dist/**/*.ts" | ||
], | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/dineug/r-html.git" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/dineug/r-html/issues" | ||
}, | ||
"homepage": "https://github.com/dineug/r-html#readme", | ||
"author": "SeungHwan-Lee <[email protected]>", | ||
"license": "MIT", | ||
"scripts": { | ||
"dev": "vite", | ||
"build": "vite build" | ||
}, | ||
"devDependencies": { | ||
"@rollup/plugin-typescript": "^11.1.2", | ||
"rollup-plugin-visualizer": "^5.9.2", | ||
"ts-patch": "^3.0.2", | ||
"tslib": "^2.6.1", | ||
"typescript": "5.1.6", | ||
"typescript-transform-paths": "^3.4.6", | ||
"vite": "^4.4.9", | ||
"vite-tsconfig-paths": "^4.2.0" | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,67 @@ | ||
export const PREFIX_ON_EVENT = 'on'; | ||
export const PREFIX_EVENT = '@'; | ||
export const PREFIX_PROPERTY = '.'; | ||
export const PREFIX_BOOLEAN = '?'; | ||
const PREFIX_SPREAD = '...'; | ||
const PREFIX_MARKER = '@@r-html'; | ||
const SUFFIX_MARKER = Math.random().toString().substring(2, 8); | ||
|
||
export const MARKER = `${PREFIX_MARKER}-${SUFFIX_MARKER}`; | ||
export const SPREAD_MARKER = `${PREFIX_SPREAD}${MARKER}`; | ||
|
||
export const markersRegexp = new RegExp(`${MARKER}_(\\d+)_`, 'g'); | ||
export const markerOnlyRegexp = new RegExp(`^${MARKER}_\\d+_$`); | ||
export const nextLineRegexp = /^\n/; | ||
|
||
export enum TAttrType { | ||
attribute = 'attribute', | ||
boolean = 'boolean', | ||
event = 'event', | ||
property = 'property', | ||
spread = 'spread', | ||
directive = 'directive', | ||
} | ||
|
||
export const BEFORE_MOUNT = Symbol.for( | ||
'https://github.com/dineug/r-html#beforeMount' | ||
); | ||
export const MOUNTED = Symbol.for('https://github.com/dineug/r-html#mounted'); | ||
export const UNMOUNTED = Symbol.for( | ||
'https://github.com/dineug/r-html#unmounted' | ||
); | ||
export const BEFORE_FIRST_UPDATE = Symbol.for( | ||
'https://github.com/dineug/r-html#beforeFirstUpdate' | ||
); | ||
export const BEFORE_UPDATE = Symbol.for( | ||
'https://github.com/dineug/r-html#beforeUpdate' | ||
); | ||
export const FIRST_UPDATED = Symbol.for( | ||
'https://github.com/dineug/r-html#firstUpdated' | ||
); | ||
export const UPDATED = Symbol.for('https://github.com/dineug/r-html#updated'); | ||
|
||
export const LIFECYCLE_NAMES: LifecycleName[] = [ | ||
BEFORE_MOUNT, | ||
MOUNTED, | ||
UNMOUNTED, | ||
BEFORE_FIRST_UPDATE, | ||
BEFORE_UPDATE, | ||
FIRST_UPDATED, | ||
UPDATED, | ||
]; | ||
|
||
export type LifecycleName = | ||
| typeof BEFORE_MOUNT | ||
| typeof MOUNTED | ||
| typeof UNMOUNTED | ||
| typeof BEFORE_FIRST_UPDATE | ||
| typeof FIRST_UPDATED | ||
| typeof BEFORE_UPDATE | ||
| typeof UPDATED; | ||
|
||
export const DIRECTIVE = Symbol.for( | ||
'https://github.com/dineug/r-html#Directive' | ||
); | ||
export const TEMPLATE_LITERALS = Symbol.for( | ||
'https://github.com/dineug/r-html#TemplateLiterals' | ||
); |
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 |
---|---|---|
@@ -0,0 +1,77 @@ | ||
export type Context<T> = { | ||
key: string | symbol; | ||
value: T; | ||
}; | ||
|
||
export function createContext<T>(value: T, key?: string | symbol): Context<T> { | ||
return Object.freeze({ key: key ?? Symbol(), value }); | ||
} | ||
|
||
export const ContextInternalEventType = { | ||
subscribe: '@@r-html/context-subscribe', | ||
unsubscribe: '@@r-html/context-unsubscribe', | ||
} as const; | ||
export type ContextInternalEventType = | ||
(typeof ContextInternalEventType)[keyof typeof ContextInternalEventType]; | ||
|
||
export type ContextInternalEventMap = { | ||
[ContextInternalEventType.subscribe]: ContextEventDetail; | ||
[ContextInternalEventType.unsubscribe]: ContextEventDetail; | ||
}; | ||
|
||
type EventOptions = { | ||
bubbles?: boolean; | ||
composed?: boolean; | ||
}; | ||
|
||
function createInternalEvent<P = void>( | ||
type: string, | ||
defaultOptions?: EventOptions | ||
) { | ||
function actionCreator(payload: P, options?: EventOptions): CustomEvent<P> { | ||
return new CustomEvent(type, { | ||
detail: payload, | ||
...defaultOptions, | ||
...options, | ||
}); | ||
} | ||
|
||
actionCreator.toString = () => `${type}`; | ||
actionCreator.type = type; | ||
return actionCreator; | ||
} | ||
|
||
export type ContextEventDetail<T = any> = { | ||
context: Context<T>; | ||
observer: (value: T) => void; | ||
}; | ||
|
||
export const contextSubscribeEvent = createInternalEvent< | ||
ContextInternalEventMap[typeof ContextInternalEventType.subscribe] | ||
>(ContextInternalEventType.subscribe, { bubbles: true, composed: true }); | ||
|
||
export const contextUnsubscribeEvent = createInternalEvent< | ||
ContextInternalEventMap[typeof ContextInternalEventType.unsubscribe] | ||
>(ContextInternalEventType.unsubscribe, { bubbles: true, composed: true }); | ||
|
||
export function fragmentContextBridge(fragment: DocumentFragment, root: Node) { | ||
const handleSubscribe = (event: Event) => { | ||
const e = event as CustomEvent<ContextEventDetail>; | ||
root.dispatchEvent(contextSubscribeEvent(e.detail)); | ||
}; | ||
|
||
const handleUnsubscribe = (event: Event) => { | ||
const e = event as CustomEvent<ContextEventDetail>; | ||
root.dispatchEvent(contextUnsubscribeEvent(e.detail)); | ||
}; | ||
|
||
fragment.addEventListener(contextSubscribeEvent.type, handleSubscribe); | ||
fragment.addEventListener(contextUnsubscribeEvent.type, handleUnsubscribe); | ||
return () => { | ||
fragment.removeEventListener(contextSubscribeEvent.type, handleSubscribe); | ||
fragment.removeEventListener( | ||
contextUnsubscribeEvent.type, | ||
handleUnsubscribe | ||
); | ||
}; | ||
} |
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 |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { observable } from '@/observable'; | ||
import { Ref } from '@/render/directives/attribute'; | ||
import { onBeforeMount, onUnmounted } from '@/render/part/node/component/hooks'; | ||
import { Context as Ctx } from '@/render/part/node/component/observableComponent'; | ||
|
||
import { | ||
Context, | ||
contextSubscribeEvent, | ||
contextUnsubscribeEvent, | ||
} from './createContext'; | ||
|
||
export function useContext<T>( | ||
ctx: Ctx<HTMLElement> | Ctx<{}>, | ||
context: Context<T> | ||
) { | ||
const ref: Ref<T> = observable({ value: context.value }, { shallow: true }); | ||
|
||
const observer = (value: T) => { | ||
ref.value = value; | ||
}; | ||
|
||
const getTarget = () => | ||
ctx instanceof HTMLElement ? ctx : ctx.parentElement ?? ctx.host; | ||
|
||
const subscribe = () => { | ||
const target = getTarget(); | ||
|
||
target.dispatchEvent( | ||
contextSubscribeEvent({ | ||
context, | ||
observer, | ||
}) | ||
); | ||
}; | ||
|
||
subscribe(); | ||
onBeforeMount(subscribe); | ||
|
||
onUnmounted(() => { | ||
const target = getTarget(); | ||
|
||
target.dispatchEvent( | ||
contextUnsubscribeEvent({ | ||
context, | ||
observer, | ||
}) | ||
); | ||
}); | ||
|
||
return ref; | ||
} |
Oops, something went wrong.