Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
Machy8 committed Oct 21, 2023
1 parent 0f1023c commit be90424
Show file tree
Hide file tree
Showing 30 changed files with 204 additions and 256 deletions.
38 changes: 38 additions & 0 deletions first.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<!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">
<meta snippet="spa-cache-control" name="spa-cache-control" content="cache">
<title snippet="title">First page</title>
<script type="module" src="http://localhost:5173/@vite/client"></script>
</head>

<body>
<style>
[cloak] {
visibility: hidden;
}
</style>

<div snippet="content">
<a href="/second.html">Second</a>
</div>

<script type="module">
import Signalize from 'signalizejs';
import ajax from 'signalizejs/ajax';
import snippets from 'signalizejs/snippets';
import spa from 'signalizejs/spa';

const signalize = new Signalize();
signalize.use(ajax);
signalize.use(snippets);
signalize.use(spa);
</script>

</body>

</html>
38 changes: 15 additions & 23 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta data-snippet="spa-cache-control" name="spa-cache-control" content="cache">
<title data-snippet="title">First page</title>
<meta snippet="spa-cache-control" name="spa-cache-control" content="cache">
<title snippet="title">First page</title>
<script type="module" src="http://localhost:5173/@vite/client"></script>
</head>

Expand Down Expand Up @@ -117,30 +117,22 @@
</template>
</div> -->

<div id="app"></div>
<div>

</div>

<script type="module">
// Import signalize
import Signalize from 'signalizejs';
// Import Hyperscript Plugin
import HyperscriptPlugin from 'signalizejs/h'

// Init signalize
const signalize = new Signalize();
// Use Hyperscript Plugin
signalize.use(HyperscriptPlugin);
const { signal, select, h } = signalize;
// Create text signal
const text = signal(0);

select('#app').replaceWith(
// Create elements
h('div', {id: app},
// Bind text signal to element attributes
h('input', {value: text}),
h('div', text)
)
);

const { Signal } = new Signalize();
class Number extends Signal {
increment() {
this.set(this.get() + 1);
}
}
const number = new Number(0);
number.increment();
console.log(number())
</script>

</body>
Expand Down
2 changes: 1 addition & 1 deletion packages/signalizejs/asset-loader/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,5 +138,5 @@ export default (signalize: Signalize): void => {
})
});

signalize.load = load;
signalize.loadAsset = load;
}
8 changes: 0 additions & 8 deletions packages/signalizejs/core/package.json

This file was deleted.

88 changes: 0 additions & 88 deletions packages/signalizejs/core/src/Signalize.ts

This file was deleted.

11 changes: 0 additions & 11 deletions packages/signalizejs/core/src/index.global.ts

This file was deleted.

4 changes: 0 additions & 4 deletions packages/signalizejs/core/src/index.ts

This file was deleted.

6 changes: 0 additions & 6 deletions packages/signalizejs/core/tests/bind.spec.ts

This file was deleted.

13 changes: 0 additions & 13 deletions packages/signalizejs/core/tests/pages/bind.html

This file was deleted.

8 changes: 3 additions & 5 deletions packages/signalizejs/directives/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type Signalize from 'signalizejs/core'
import type { CustomEventListener, Scope } from 'signalizejs/core';
import type Signalize from 'signalizejs'
import type { CustomEventListener, Scope } from 'signalizejs';

declare module '..' {
interface Signalize {
Expand Down Expand Up @@ -190,11 +190,10 @@ export default (signalize: Signalize): void => {
if (!(cacheKey in createFunctionCache)) {
const functionDataKeys = Object.keys({...globals, ...context});
createFunctionCache[cacheKey] = async function (data) {
let functionData = {...globals, ...data}
let functionData = {signalize, ...globals, ...data}
try {
return new AsyncFunction('_context', '_element', `
try {
console.log('he');
let { ${functionDataKeys.join(',')} } = _context;
${functionString}
} catch(e) {
Expand Down Expand Up @@ -653,5 +652,4 @@ export default (signalize: Signalize): void => {
})

signalize.directive = directive;

}
39 changes: 33 additions & 6 deletions packages/signalizejs/spa/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,15 @@ type StateAction = 'push' | 'replace';

interface VisitData {
url: string | URL
scrollX?: number
scrollY?: number
stateAction?: StateAction
}

interface SpaHistoryState extends Partial<VisitData> {
spa?: true
}

interface SpaDispatchEventData extends VisitData {
success?: boolean
}
Expand Down Expand Up @@ -123,10 +129,20 @@ export default (signalize: Signalize): void => {
redraw(responseData);
}

console.log(data);
if (stateAction === 'replace') {
window.history.replaceState(window.history.state, '', urlString);
} else if (stateAction === 'push') {
window.history.pushState({ spa: true }, '', urlString);
window.history.pushState(
{
url: data.url,
spa: true,
scrollX: data.scrollX ?? window.scrollX,
scrollY: data.scrollY ?? window.scrollY
},
'',
urlString
);
}

if (shouldCacheResponse === null) {
Expand Down Expand Up @@ -168,7 +184,7 @@ export default (signalize: Signalize): void => {
});
}
} else {
window.scrollTo(0, 0)
window.scrollTo(data.scrollX ?? 0, data.scrollY ?? 0)
}
}

Expand All @@ -182,18 +198,23 @@ export default (signalize: Signalize): void => {
}

const onPopState = (): void => {
if (typeof window.history.state?.spa !== 'boolean') {
const state = window.history.state as SpaHistoryState;
console.log(state?.spa ?? false);
if (!(state?.spa ?? false)) {
return;
}

console.log(state);
const location = new URL(window.location.href);

if (location === currentLocation || (location.pathname === currentLocation.pathname && location.hash !== currentLocation.hash)) {
return;
}

const visitConfig = {
url: location
const visitConfig: VisitData = {
url: location,
scrollX: state.scrollX,
scrollY: state.scrollY
};

dispatch('spa:popstate', visitConfig);
Expand Down Expand Up @@ -230,7 +251,13 @@ export default (signalize: Signalize): void => {
}

if (window.history.state === null) {
window.history.replaceState({ spa: true }, '', window.location.href);
window.history.replaceState(
{
spa: true, scrollX: window.scrollX, scrollY: window.scrollY
},
'',
window.location.href
);
}

event.preventDefault();
Expand Down
Loading

0 comments on commit be90424

Please sign in to comment.