native-document 1.0.94 → 1.0.98
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/{src/devtools/hrm → devtools}/ComponentRegistry.js +2 -2
- package/devtools/index.js +8 -0
- package/{src/devtools/plugin.js → devtools/plugin/dev-tools-plugin.js} +2 -2
- package/{src/devtools/hrm/nd-vite-hot-reload.js → devtools/transformers/nd-vite-devtools.js} +16 -6
- package/devtools/transformers/src/transformComponentForHrm.js +74 -0
- package/devtools/transformers/src/transformJsFile.js +9 -0
- package/devtools/transformers/src/utils.js +79 -0
- package/devtools/widget/Widget.js +48 -0
- package/devtools/widget/widget.css +81 -0
- package/devtools/widget.js +23 -0
- package/dist/native-document.components.min.js +2441 -1191
- package/dist/native-document.dev.js +2710 -1359
- package/dist/native-document.dev.js.map +1 -1
- package/dist/native-document.devtools.min.js +1 -1
- package/dist/native-document.min.js +1 -1
- package/docs/cache.md +1 -1
- package/docs/core-concepts.md +1 -1
- package/docs/native-document-element.md +51 -15
- package/docs/observables.md +310 -306
- package/docs/state-management.md +198 -193
- package/index.def.js +762 -26
- package/package.json +1 -1
- package/readme.md +1 -1
- package/src/core/data/ObservableChecker.js +2 -0
- package/src/core/data/ObservableItem.js +97 -0
- package/src/core/data/ObservableObject.js +182 -0
- package/src/core/data/Store.js +364 -34
- package/src/core/data/observable-helpers/object.js +2 -166
- package/src/core/elements/anchor.js +28 -20
- package/src/core/elements/control/for-each.js +1 -1
- package/src/core/utils/formatters.js +91 -0
- package/src/core/utils/localstorage.js +57 -0
- package/src/core/utils/validator.js +0 -2
- package/src/core/wrappers/DocumentObserver.js +102 -31
- package/src/core/wrappers/ElementCreator.js +5 -0
- package/src/core/wrappers/NDElement.js +32 -1
- package/src/core/wrappers/prototypes/nd-element.transition.extensions.js +83 -0
- package/src/devtools.js +9 -0
- package/src/fetch/NativeFetch.js +5 -2
- package/types/elements.d.ts +580 -2
- package/types/nd-element.d.ts +6 -0
- package/types/observable.d.ts +71 -15
- package/types/plugins-manager.d.ts +1 -1
- package/types/store.d.ts +33 -6
- package/hrm.js +0 -7
- package/src/devtools/app/App.js +0 -66
- package/src/devtools/app/app.css +0 -0
- package/src/devtools/hrm/transformComponent.js +0 -129
- package/src/devtools/index.js +0 -18
- package/src/devtools/widget/DevToolsWidget.js +0 -26
- /package/{src/devtools/hrm → devtools/transformers/templates}/hrm.hook.template.js +0 -0
- /package/{src/devtools/hrm → devtools/transformers/templates}/hrm.orbservable.hook.template.js +0 -0
|
@@ -6,10 +6,10 @@ export const DevToolService = {
|
|
|
6
6
|
|
|
7
7
|
|
|
8
8
|
export const DevToolsPlugin = {
|
|
9
|
+
$name: 'DevTools',
|
|
9
10
|
name: 'DevTools',
|
|
10
11
|
onCreateObservable(observable) {
|
|
11
12
|
DevToolService.createdObservable.set((last) => ++last);
|
|
12
|
-
console.log('Création Capturé', observable);
|
|
13
|
+
console.log('Création Capturé', observable.$value);
|
|
13
14
|
}
|
|
14
|
-
|
|
15
15
|
};
|
package/{src/devtools/hrm/nd-vite-hot-reload.js → devtools/transformers/nd-vite-devtools.js}
RENAMED
|
@@ -1,19 +1,24 @@
|
|
|
1
|
-
|
|
2
|
-
import
|
|
1
|
+
import Fs from 'node:fs'
|
|
2
|
+
import transformComponentForHrm from "./src/transformComponentForHrm.js";
|
|
3
|
+
import {isFileMustBeModified} from "./src/utils.js";
|
|
4
|
+
import transformJsFile from "./src/transformJsFile.js";
|
|
3
5
|
|
|
4
|
-
export default function
|
|
6
|
+
export default function NdViteDevtools(options) {
|
|
5
7
|
const {
|
|
6
8
|
include = /\.nd\.js$/,
|
|
7
9
|
preserveState = true
|
|
8
10
|
} = options;
|
|
9
11
|
|
|
10
12
|
return {
|
|
11
|
-
name: '
|
|
13
|
+
name: 'nd-devtools',
|
|
12
14
|
apply: 'serve',
|
|
13
15
|
enforce: 'post',
|
|
14
16
|
|
|
15
17
|
handleHotUpdate({ file, server, modules }) {
|
|
16
18
|
if (!include.test(file)) {
|
|
19
|
+
if(isFileMustBeModified(file)) {
|
|
20
|
+
return modules.filter(Boolean);
|
|
21
|
+
}
|
|
17
22
|
return;
|
|
18
23
|
}
|
|
19
24
|
|
|
@@ -28,11 +33,16 @@ export default function NdViteHotReload(options) {
|
|
|
28
33
|
return modules.filter(Boolean);
|
|
29
34
|
},
|
|
30
35
|
transform(code, id) {
|
|
31
|
-
if (!include.test(id))
|
|
36
|
+
if (!include.test(id)) {
|
|
37
|
+
if(isFileMustBeModified(id)) {
|
|
38
|
+
return transformJsFile(id, code, { preserveState });
|
|
39
|
+
}
|
|
40
|
+
return null;
|
|
41
|
+
}
|
|
32
42
|
if (id.includes('node_modules')) return null;
|
|
33
43
|
|
|
34
44
|
try {
|
|
35
|
-
return
|
|
45
|
+
return transformComponentForHrm(id, code, { preserveState });
|
|
36
46
|
} catch (error) {
|
|
37
47
|
console.error(`[NativeDocument] Transform error in ${id}:`, error);
|
|
38
48
|
return null;
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
// import { parse } from '@babel/parser';
|
|
4
|
+
import MagicString from 'magic-string';
|
|
5
|
+
|
|
6
|
+
import { fileURLToPath } from 'node:url';
|
|
7
|
+
import { parse } from '@babel/parser';
|
|
8
|
+
import { default as traverse } from '@babel/traverse';
|
|
9
|
+
|
|
10
|
+
import { transformObservableImports, transformObservableDeclarations, template } from './utils.js';
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
const __filename = fileURLToPath(import.meta.url);
|
|
14
|
+
const __dirname = path.dirname(__filename);
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
export default function transformComponentForHrm(id, code, options) {
|
|
18
|
+
let hasDefaultExport = false;
|
|
19
|
+
let componentName = null;
|
|
20
|
+
let exportStart = 0;
|
|
21
|
+
let exportEnd = 0;
|
|
22
|
+
// TODO: move this line outside the function
|
|
23
|
+
const hrmHookTemplate = fs.readFileSync(__dirname + '/../templates/hrm.hook.template.js', 'utf8');
|
|
24
|
+
const formattedCode = transformObservableDeclarations(
|
|
25
|
+
transformObservableImports(code, { id, ...options }),
|
|
26
|
+
);
|
|
27
|
+
const s = new MagicString(formattedCode);
|
|
28
|
+
const codeParsed = parse(formattedCode, {
|
|
29
|
+
sourceType: 'module',
|
|
30
|
+
plugins: []
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
traverse.default(codeParsed, {
|
|
34
|
+
ExportDefaultDeclaration(path) {
|
|
35
|
+
hasDefaultExport = true;
|
|
36
|
+
const declaration = path.node.declaration;
|
|
37
|
+
|
|
38
|
+
if (declaration.id) {
|
|
39
|
+
componentName = declaration.id.name;
|
|
40
|
+
} else if (declaration.type === 'Identifier') {
|
|
41
|
+
componentName = declaration.name;
|
|
42
|
+
} else {
|
|
43
|
+
componentName = 'AnonymousComponent';
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
exportStart = path.node.start;
|
|
47
|
+
exportEnd = path.node.end;
|
|
48
|
+
},
|
|
49
|
+
});
|
|
50
|
+
if (!hasDefaultExport) {
|
|
51
|
+
return null;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const originalExport = formattedCode.slice(exportStart, exportEnd);
|
|
55
|
+
const hrmComponentName = `__HRM_${componentName}__`;
|
|
56
|
+
const newExport = originalExport.replace(
|
|
57
|
+
'export default',
|
|
58
|
+
`const ${hrmComponentName} =`
|
|
59
|
+
);
|
|
60
|
+
s.overwrite(exportStart, exportEnd, newExport);
|
|
61
|
+
|
|
62
|
+
const hrmHookTemplateFormatted = template(hrmHookTemplate, {
|
|
63
|
+
id
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
s.prepend('import ComponentRegistry from "native-document/devtools/ComponentRegistry";');
|
|
67
|
+
s.append(`export default ComponentRegistry.register('${id}', ${hrmComponentName}, { preserveState: ${options.preserveState} });`);
|
|
68
|
+
s.append(hrmHookTemplateFormatted);
|
|
69
|
+
|
|
70
|
+
return {
|
|
71
|
+
code: s.toString(),
|
|
72
|
+
map: s.generateMap({ source: id, hires: true })
|
|
73
|
+
};
|
|
74
|
+
}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import MagicString from 'magic-string';
|
|
4
|
+
|
|
5
|
+
import { fileURLToPath } from 'node:url';
|
|
6
|
+
|
|
7
|
+
const __filename = fileURLToPath(import.meta.url);
|
|
8
|
+
const __dirname = path.dirname(__filename);
|
|
9
|
+
|
|
10
|
+
export function template(code, params) {
|
|
11
|
+
let codeFormatted = code;
|
|
12
|
+
for(const key in params) {
|
|
13
|
+
codeFormatted = codeFormatted.replace(new RegExp("\\$\{"+key+"}", 'ig'), params[key]);
|
|
14
|
+
}
|
|
15
|
+
return codeFormatted;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export function renameImportedObservables(content, match) {
|
|
19
|
+
let isObservableFound = false;
|
|
20
|
+
let isObservableShortFound = false;
|
|
21
|
+
const transformedContentArray = match.split(',').map(item => {
|
|
22
|
+
let trimmed = item.trim();
|
|
23
|
+
|
|
24
|
+
if (trimmed === 'Observable') {
|
|
25
|
+
isObservableFound = true;
|
|
26
|
+
return 'Observable as __OriginalObservable__';
|
|
27
|
+
}
|
|
28
|
+
if (trimmed === '$') {
|
|
29
|
+
isObservableShortFound = true;
|
|
30
|
+
return '$ as __$__';
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
return ` ${trimmed}`;
|
|
34
|
+
});
|
|
35
|
+
if(!isObservableFound && isObservableShortFound) {
|
|
36
|
+
transformedContentArray.push('Observable as __OriginalObservable__')
|
|
37
|
+
}
|
|
38
|
+
const transformedContent = transformedContentArray.join(', ');
|
|
39
|
+
|
|
40
|
+
return `import {${transformedContent} } from 'native-document'`;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export function transformObservableImports(code, params) {
|
|
44
|
+
const renameImportationCode = code.replace(/import\s+\{([^}]+?)\}\s+from\s+['"]native-document['"]/g, renameImportedObservables);
|
|
45
|
+
const isRenamed = renameImportationCode !== code;
|
|
46
|
+
|
|
47
|
+
const s = new MagicString(renameImportationCode);
|
|
48
|
+
if(isRenamed) {
|
|
49
|
+
//Todo: move this code outside the function
|
|
50
|
+
const hrmObservableHookTemplate = fs.readFileSync(__dirname + '/../templates/hrm.orbservable.hook.template.js', 'utf8');
|
|
51
|
+
s.append(template(
|
|
52
|
+
hrmObservableHookTemplate,
|
|
53
|
+
params ?? {}
|
|
54
|
+
));
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
return s.toString();
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export function transformObservableDeclarations(code) {
|
|
61
|
+
const regex = /(const|let|var)\s+([\w$]+)\s*=\s*(\$|Observable)(\.(?:init|array|json))?\s*\(/g;
|
|
62
|
+
|
|
63
|
+
return code.replace(regex, (match, varType, varName, caller, method) => {
|
|
64
|
+
const obsName = method ? `${caller}${method}` : 'Observable';
|
|
65
|
+
return `${varType} ${varName} = ${obsName}('${varName}', arguments[arguments.length - 1], `;
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export function isFileMustBeModified(file) {
|
|
70
|
+
if(!/labs\/live\/src/.test(file)) {
|
|
71
|
+
return false;
|
|
72
|
+
}
|
|
73
|
+
const content = fs.readFileSync(file, 'utf-8');
|
|
74
|
+
const isContainObservableImport = /import\s+\{([^}]+?)\}\s+from\s+['"]native-document['"]/g.test(content);
|
|
75
|
+
if(isContainObservableImport) {
|
|
76
|
+
return true;
|
|
77
|
+
}
|
|
78
|
+
return /(const|let|var)\s+([\w$]+)\s*=\s*(\$|Observable)(\.(?:init|array|json))?\s*\(/g.test(content);
|
|
79
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import {Observable} from "../../index";
|
|
2
|
+
import {Button, Div, ShowIf, Switch} from "../../elements";
|
|
3
|
+
import './widget.css';
|
|
4
|
+
|
|
5
|
+
export default function Widget() {
|
|
6
|
+
|
|
7
|
+
const $isNotMinimize = Observable(false);
|
|
8
|
+
const $isMaximize = Observable(false);
|
|
9
|
+
|
|
10
|
+
const toggleMinimize = () => {
|
|
11
|
+
$isNotMinimize.toggle();
|
|
12
|
+
}
|
|
13
|
+
const toggleMaximize = () => {
|
|
14
|
+
$isNotMinimize.set(true);
|
|
15
|
+
$isMaximize.toggle();
|
|
16
|
+
}
|
|
17
|
+
const close = () => {}
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
return Div({
|
|
21
|
+
class: {
|
|
22
|
+
'nd-dev-tools-widget': true,
|
|
23
|
+
'nd-dev-tools-widget-maximize': $isMaximize,
|
|
24
|
+
'nd-dev-tools-widget-not-minimize': $isNotMinimize,
|
|
25
|
+
}
|
|
26
|
+
},
|
|
27
|
+
[
|
|
28
|
+
Div({ class: 'nd-dev-tools-widget-header'},[
|
|
29
|
+
Switch(
|
|
30
|
+
$isNotMinimize,
|
|
31
|
+
Div({ class: 'nd-dev-tools-widget-header-title'}, 'DevTools'),
|
|
32
|
+
Div({ class: 'nd-dev-notification-badge' }, 100)
|
|
33
|
+
),
|
|
34
|
+
Div({ class: 'nd-dev-tools-widget-header-options'}, [
|
|
35
|
+
Button({ class: 'nd-dev-tools-widget-header-option btn-close'}).nd.onClick(close),
|
|
36
|
+
Button({ class: 'nd-dev-tools-widget-header-option btn-maximize'}).nd.onClick(toggleMaximize),
|
|
37
|
+
Button({ class: 'nd-dev-tools-widget-header-option btn-minimize'}).nd.onClick(toggleMinimize),
|
|
38
|
+
])
|
|
39
|
+
]),
|
|
40
|
+
ShowIf(
|
|
41
|
+
$isNotMinimize,
|
|
42
|
+
Div({ class: 'nd-dev-tools-widget-content'}, [
|
|
43
|
+
'Show me sometghings'
|
|
44
|
+
])
|
|
45
|
+
)
|
|
46
|
+
]
|
|
47
|
+
);
|
|
48
|
+
}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
:root {
|
|
2
|
+
--nd-dev-background: #16181f;
|
|
3
|
+
--nd-dev-text-color: #e2e8f0;
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
.nd-dev-tools-widget {
|
|
8
|
+
position: fixed;
|
|
9
|
+
top: 10px;
|
|
10
|
+
right: 10px;
|
|
11
|
+
background: var(--nd-dev-background);
|
|
12
|
+
color: var(--nd-dev-text-color);
|
|
13
|
+
border-radius: 5px;
|
|
14
|
+
box-shadow: 0 0 10px rgba(0, 0, 0, 0.5);
|
|
15
|
+
min-width: 100px;
|
|
16
|
+
min-height: unset;
|
|
17
|
+
z-index: 2147483647;
|
|
18
|
+
&.nd-dev-tools-widget-not-minimize {
|
|
19
|
+
min-width: 300px;
|
|
20
|
+
min-height: 200px;
|
|
21
|
+
&.nd-dev-tools-widget-maximize {
|
|
22
|
+
width: calc(100dvw - 10px*2);
|
|
23
|
+
height: calc(100dvh - 10px*2);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
.nd-dev-tools-widget-header {
|
|
28
|
+
display: flex;
|
|
29
|
+
justify-content: space-between;
|
|
30
|
+
align-items: center;
|
|
31
|
+
padding: .5rem 1.5rem;
|
|
32
|
+
column-gap: 3rem;
|
|
33
|
+
cursor: move;
|
|
34
|
+
}
|
|
35
|
+
.nd-dev-notification-badge {
|
|
36
|
+
display: flex;
|
|
37
|
+
justify-content: center;
|
|
38
|
+
align-items: center;
|
|
39
|
+
align-content: center;
|
|
40
|
+
width: 1.5rem;
|
|
41
|
+
height: 1.5rem;
|
|
42
|
+
border-radius: 50%;
|
|
43
|
+
background-color: #ff5f57;
|
|
44
|
+
font-size: .75rem;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
.nd-dev-tools-widget-header-title {
|
|
48
|
+
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
.nd-dev-tools-widget-header-options {
|
|
52
|
+
display: flex;
|
|
53
|
+
justify-content: flex-end;
|
|
54
|
+
align-items: center;
|
|
55
|
+
column-gap: 5px;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
.nd-dev-tools-widget-header-option {
|
|
59
|
+
display: inline-block;
|
|
60
|
+
width: 10px;
|
|
61
|
+
height: 10px;
|
|
62
|
+
border-radius: 50%;
|
|
63
|
+
border: none;
|
|
64
|
+
cursor: pointer;
|
|
65
|
+
&.btn-close {
|
|
66
|
+
background-color: #ff5f57
|
|
67
|
+
}
|
|
68
|
+
&.btn-minimize {
|
|
69
|
+
background-color: #febc2e;
|
|
70
|
+
}
|
|
71
|
+
&.btn-maximize {
|
|
72
|
+
background-color: #28c840;
|
|
73
|
+
}
|
|
74
|
+
&:hover {
|
|
75
|
+
box-shadow: 0 0 0 2px #fff;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
.nd-dev-tools-widget-content {
|
|
80
|
+
padding: .5rem;
|
|
81
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import Widget from './widget/Widget';
|
|
2
|
+
import {PluginsManager} from "../index";
|
|
3
|
+
import { DevToolsPlugin } from "./plugin/dev-tools-plugin";
|
|
4
|
+
|
|
5
|
+
PluginsManager.add(DevToolsPlugin, 'Devtools');
|
|
6
|
+
|
|
7
|
+
const Devtools = (function () {
|
|
8
|
+
|
|
9
|
+
return {
|
|
10
|
+
config() {
|
|
11
|
+
console.log('devtool init configuratzion');
|
|
12
|
+
},
|
|
13
|
+
rootApp() {
|
|
14
|
+
|
|
15
|
+
},
|
|
16
|
+
init() {
|
|
17
|
+
const app = Widget();
|
|
18
|
+
document.body.parentNode.appendChild(app);
|
|
19
|
+
},
|
|
20
|
+
}
|
|
21
|
+
}());
|
|
22
|
+
|
|
23
|
+
export default Devtools;
|