Skip to content

Commit

Permalink
fix: git submodule
Browse files Browse the repository at this point in the history
  • Loading branch information
dineug committed Nov 18, 2023
1 parent 2e91739 commit e61fd40
Show file tree
Hide file tree
Showing 94 changed files with 5,819 additions and 104 deletions.
1 change: 0 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,5 @@ jobs:
with:
node-version: 18
cache: 'pnpm'
- run: sh submodules/submodule-clone.sh
- run: pnpm install
- run: pnpm build
12 changes: 4 additions & 8 deletions erd-editor.code-workspace
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,12 @@
"path": "packages/shared"
},
{
"name": "@dineug/go",
"path": "submodules/go/packages/go"
"name": "r-html",
"path": "packages/r-html"
},
{
"name": "@dineug/r-html",
"path": "submodules/r-html/packages/r-html"
},
{
"name": "@dineug/vite-plugin-r-html",
"path": "submodules/r-html/packages/vite-plugin-r-html"
"name": "vite-plugin-r-html",
"path": "packages/vite-plugin-r-html"
},
{
"name": "monorepo",
Expand Down
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
"name": "@dineug/erd-editor-monorepo",
"private": true,
"workspaces": [
"packages/*",
"submodules/*/packages/*"
"packages/*"
],
"engines": {
"node": ">=18.14.0"
Expand Down
2 changes: 1 addition & 1 deletion packages/erd-editor/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
},
"devDependencies": {
"@dineug/erd-editor-schema": "workspace:*",
"@dineug/go": "workspace:*",
"@dineug/go": "^0.1.7",
"@dineug/r-html": "workspace:*",
"@dineug/shared": "workspace:*",
"@dineug/vite-plugin-r-html": "workspace:*",
Expand Down
21 changes: 21 additions & 0 deletions packages/r-html/LICENSE
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.
43 changes: 43 additions & 0 deletions packages/r-html/README.md
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>`);
```
12 changes: 12 additions & 0 deletions packages/r-html/index.html
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>
47 changes: 47 additions & 0 deletions packages/r-html/package.json
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"
}
}
67 changes: 67 additions & 0 deletions packages/r-html/src/constants.ts
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'
);
77 changes: 77 additions & 0 deletions packages/r-html/src/context/createContext.ts
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
);
};
}
51 changes: 51 additions & 0 deletions packages/r-html/src/context/useContext.ts
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;
}
Loading

0 comments on commit e61fd40

Please sign in to comment.