slicejs-web-framework 1.0.7 → 1.0.9
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/Slice/Components/Structural/Router/Router.js +59 -33
- package/api/index.js +1 -1
- package/package.json +1 -1
- package/src/Components/AppComponents/CodeVisualizer/CodeVisualizer.css +126 -10
- package/src/Components/AppComponents/CodeVisualizer/CodeVisualizer.html +2 -1
- package/src/Components/AppComponents/CodeVisualizer/CodeVisualizer.js +224 -6
- package/src/Components/AppComponents/DocumentationPage/DocumentationPage.js +207 -165
- package/src/Components/AppComponents/RoutingDocumentation/RoutingDocumentation.css +89 -0
- package/src/Components/AppComponents/RoutingDocumentation/RoutingDocumentation.html +108 -0
- package/src/Components/AppComponents/RoutingDocumentation/RoutingDocumentation.js +177 -0
- package/src/Components/AppComponents/SliceTeamCard/SliceTeamCard.js +74 -74
- package/src/Components/AppComponents/TheBuildMethod/TheBuildMethod.css +122 -0
- package/src/Components/AppComponents/TheBuildMethod/TheBuildMethod.html +70 -0
- package/src/Components/AppComponents/TheBuildMethod/TheBuildMethod.js +86 -0
- package/src/Components/components.js +3 -0
- package/src/routes.js +59 -18
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
|
|
2
1
|
export default class Router {
|
|
3
2
|
constructor(routes) {
|
|
4
3
|
this.routes = routes;
|
|
@@ -11,16 +10,30 @@ export default class Router {
|
|
|
11
10
|
window.addEventListener('popstate', this.onRouteChange.bind(this));
|
|
12
11
|
}
|
|
13
12
|
|
|
14
|
-
createPathToRouteMap(routes, basePath = '') {
|
|
13
|
+
createPathToRouteMap(routes, basePath = '', parentRoute = null) {
|
|
15
14
|
const pathToRouteMap = new Map();
|
|
16
15
|
|
|
17
16
|
for (const route of routes) {
|
|
18
|
-
const fullPath = `${basePath}${route.path}`.replace(/\/+/g, '/');
|
|
19
|
-
|
|
17
|
+
const fullPath = `${basePath}${route.path}`.replace(/\/+/g, '/');
|
|
18
|
+
|
|
19
|
+
// Guardar la referencia a la ruta padre
|
|
20
|
+
const routeWithParent = {
|
|
21
|
+
...route,
|
|
22
|
+
fullPath,
|
|
23
|
+
parentPath: parentRoute ? parentRoute.fullPath : null,
|
|
24
|
+
parentRoute: parentRoute
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
pathToRouteMap.set(fullPath, routeWithParent);
|
|
20
28
|
|
|
21
29
|
// Si tiene rutas secundarias, también agregarlas al mapa
|
|
22
30
|
if (route.children) {
|
|
23
|
-
const childPathToRouteMap = this.createPathToRouteMap(
|
|
31
|
+
const childPathToRouteMap = this.createPathToRouteMap(
|
|
32
|
+
route.children,
|
|
33
|
+
fullPath,
|
|
34
|
+
routeWithParent // Pasar la ruta actual como padre
|
|
35
|
+
);
|
|
36
|
+
|
|
24
37
|
for (const [childPath, childRoute] of childPathToRouteMap.entries()) {
|
|
25
38
|
pathToRouteMap.set(childPath, childRoute);
|
|
26
39
|
}
|
|
@@ -28,7 +41,7 @@ export default class Router {
|
|
|
28
41
|
}
|
|
29
42
|
|
|
30
43
|
return pathToRouteMap;
|
|
31
|
-
|
|
44
|
+
}
|
|
32
45
|
|
|
33
46
|
async renderRoutesComponentsInPage() {
|
|
34
47
|
const routeContainers = document.querySelectorAll('slice-route, slice-multi-route');
|
|
@@ -41,7 +54,6 @@ export default class Router {
|
|
|
41
54
|
routerContainersFlag = true;
|
|
42
55
|
}
|
|
43
56
|
}
|
|
44
|
-
|
|
45
57
|
return routerContainersFlag;
|
|
46
58
|
}
|
|
47
59
|
|
|
@@ -66,7 +78,12 @@ export default class Router {
|
|
|
66
78
|
|
|
67
79
|
async handleRoute(route, params) {
|
|
68
80
|
const targetElement = document.querySelector('#app');
|
|
69
|
-
|
|
81
|
+
|
|
82
|
+
// Si tenemos una ruta con parentRoute, usamos el componente del padre
|
|
83
|
+
const componentName = route.parentRoute ? route.parentRoute.component : route.component;
|
|
84
|
+
const sliceId = `route-${componentName}`;
|
|
85
|
+
|
|
86
|
+
const existingComponent = slice.controller.getComponent(sliceId);
|
|
70
87
|
|
|
71
88
|
if (slice.loading) {
|
|
72
89
|
slice.loading.start();
|
|
@@ -80,9 +97,9 @@ export default class Router {
|
|
|
80
97
|
}
|
|
81
98
|
targetElement.appendChild(existingComponent);
|
|
82
99
|
} else {
|
|
83
|
-
const component = await slice.build(
|
|
100
|
+
const component = await slice.build(componentName, {
|
|
84
101
|
params,
|
|
85
|
-
sliceId:
|
|
102
|
+
sliceId: sliceId,
|
|
86
103
|
});
|
|
87
104
|
targetElement.innerHTML = '';
|
|
88
105
|
targetElement.appendChild(component);
|
|
@@ -100,7 +117,6 @@ export default class Router {
|
|
|
100
117
|
const { route, params } = this.matchRoute(path);
|
|
101
118
|
|
|
102
119
|
await this.handleRoute(route, params);
|
|
103
|
-
|
|
104
120
|
await this.renderRoutesComponentsInPage();
|
|
105
121
|
}
|
|
106
122
|
|
|
@@ -108,10 +124,19 @@ export default class Router {
|
|
|
108
124
|
// 1. Buscar coincidencia exacta en el mapa
|
|
109
125
|
const exactMatch = this.pathToRouteMap.get(path);
|
|
110
126
|
if (exactMatch) {
|
|
127
|
+
// Si es una ruta hija y tiene un padre definido, devolvemos el padre en su lugar
|
|
128
|
+
if (exactMatch.parentRoute) {
|
|
129
|
+
// Mantenemos la información de parámetros que viene de la ruta actual
|
|
130
|
+
return {
|
|
131
|
+
route: exactMatch.parentRoute,
|
|
132
|
+
params: {},
|
|
133
|
+
childRoute: exactMatch // Guardamos la referencia a la ruta hija original
|
|
134
|
+
};
|
|
135
|
+
}
|
|
111
136
|
return { route: exactMatch, params: {} };
|
|
112
137
|
}
|
|
113
138
|
|
|
114
|
-
// 2. Buscar coincidencias en rutas con parámetros
|
|
139
|
+
// 2. Buscar coincidencias en rutas con parámetros
|
|
115
140
|
for (const [routePattern, route] of this.pathToRouteMap.entries()) {
|
|
116
141
|
if (routePattern.includes('${')) {
|
|
117
142
|
const { regex, paramNames } = this.compilePathPattern(routePattern);
|
|
@@ -119,34 +144,35 @@ export default class Router {
|
|
|
119
144
|
if (match) {
|
|
120
145
|
const params = {};
|
|
121
146
|
paramNames.forEach((name, i) => {
|
|
122
|
-
params[name] = match[i + 1];
|
|
147
|
+
params[name] = match[i + 1];
|
|
123
148
|
});
|
|
149
|
+
|
|
150
|
+
// Si es una ruta hija, devolvemos el padre con los parámetros
|
|
151
|
+
if (route.parentRoute) {
|
|
152
|
+
return {
|
|
153
|
+
route: route.parentRoute,
|
|
154
|
+
params: params,
|
|
155
|
+
childRoute: route
|
|
156
|
+
};
|
|
157
|
+
}
|
|
158
|
+
|
|
124
159
|
return { route, params };
|
|
125
160
|
}
|
|
126
161
|
}
|
|
127
162
|
}
|
|
128
163
|
|
|
129
|
-
// 3. Si no hay coincidencias, retornar la ruta 404
|
|
164
|
+
// 3. Si no hay coincidencias, retornar la ruta 404
|
|
130
165
|
const notFoundRoute = this.pathToRouteMap.get('/404');
|
|
131
166
|
return { route: notFoundRoute, params: {} };
|
|
132
167
|
}
|
|
133
168
|
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
const regexPattern = '^' + pattern.replace(/\$\{([^}]+)\}/g, (_, paramName) => {
|
|
145
|
-
paramNames.push(paramName);
|
|
146
|
-
return '([^/]+)';
|
|
147
|
-
}) + '$';
|
|
148
|
-
|
|
149
|
-
return { regex: new RegExp(regexPattern), paramNames };
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
}
|
|
169
|
+
compilePathPattern(pattern) {
|
|
170
|
+
const paramNames = [];
|
|
171
|
+
const regexPattern = '^' + pattern.replace(/\$\{([^}]+)\}/g, (_, paramName) => {
|
|
172
|
+
paramNames.push(paramName);
|
|
173
|
+
return '([^/]+)';
|
|
174
|
+
}) + '$';
|
|
175
|
+
|
|
176
|
+
return { regex: new RegExp(regexPattern), paramNames };
|
|
177
|
+
}
|
|
178
|
+
}
|
package/api/index.js
CHANGED
|
@@ -6,7 +6,7 @@ import inquirer from 'inquirer';
|
|
|
6
6
|
|
|
7
7
|
const __filename = fileURLToPath(import.meta.url);
|
|
8
8
|
const __dirname = dirname(__filename);
|
|
9
|
-
import sliceConfig from '../src/sliceConfig.json'
|
|
9
|
+
import sliceConfig from '../src/sliceConfig.json' with { type: 'json' };
|
|
10
10
|
|
|
11
11
|
let server;
|
|
12
12
|
|
package/package.json
CHANGED
|
@@ -1,14 +1,130 @@
|
|
|
1
|
+
slice-codevisualizer {
|
|
2
|
+
display: block;
|
|
3
|
+
width: 100%;
|
|
4
|
+
}
|
|
5
|
+
|
|
1
6
|
.codevisualizer_container {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
7
|
+
position: relative;
|
|
8
|
+
font-size: smaller;
|
|
9
|
+
max-width: 100%;
|
|
10
|
+
background-color: var(--secondary-background-color);
|
|
11
|
+
border: 1px solid var(--primary-color);
|
|
12
|
+
padding: 15px 10px 10px;
|
|
13
|
+
border-radius: var(--border-radius-slice, 10px);
|
|
14
|
+
overflow-x: auto;
|
|
15
|
+
margin: 15px 0;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
.codevisualizer pre {
|
|
19
|
+
margin: 0;
|
|
20
|
+
padding: 0;
|
|
21
|
+
font-family: 'Courier New', Courier, monospace;
|
|
22
|
+
line-height: 1.5;
|
|
23
|
+
white-space: pre;
|
|
24
|
+
tab-size: 2;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
.codevisualizer code {
|
|
28
|
+
display: block;
|
|
29
|
+
color: var(--font-primary-color);
|
|
30
|
+
overflow-wrap: normal;
|
|
31
|
+
word-break: normal;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/* Botón de copiar */
|
|
35
|
+
.copy-button {
|
|
36
|
+
position: absolute;
|
|
37
|
+
top: 5px;
|
|
38
|
+
right: 10px;
|
|
39
|
+
padding: 4px 8px;
|
|
40
|
+
font-size: 12px;
|
|
41
|
+
background-color: var(--primary-color);
|
|
42
|
+
color: var(--primary-color-contrast);
|
|
43
|
+
border: none;
|
|
44
|
+
border-radius: var(--border-radius-slice, 4px);
|
|
45
|
+
cursor: pointer;
|
|
46
|
+
opacity: 0.7;
|
|
47
|
+
transition: opacity 0.2s ease, background-color 0.2s ease;
|
|
48
|
+
z-index: 1;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
.copy-button:hover {
|
|
52
|
+
opacity: 1;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
.copy-button.copied {
|
|
56
|
+
background-color: var(--success-color);
|
|
57
|
+
color: var(--success-contrast);
|
|
58
|
+
opacity: 1;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/* Temas para resaltado de sintaxis adaptados a las variables de Slice */
|
|
62
|
+
.codevisualizer .code-keyword {
|
|
63
|
+
color: var(--primary-color); /* Palabras clave en color primario */
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
.codevisualizer .code-string {
|
|
67
|
+
color: var(--success-color); /* Strings en color de éxito */
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
.codevisualizer .code-number {
|
|
71
|
+
color: var(--warning-color); /* Números en color de advertencia */
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
.codevisualizer .code-comment {
|
|
75
|
+
color: var(--medium-color); /* Comentarios en color medio */
|
|
76
|
+
font-style: italic;
|
|
9
77
|
}
|
|
10
78
|
|
|
11
|
-
.codevisualizer
|
|
12
|
-
|
|
13
|
-
color: var(--font-primary-color);
|
|
79
|
+
.codevisualizer .code-function {
|
|
80
|
+
color: var(--secondary-color); /* Funciones en color secundario */
|
|
14
81
|
}
|
|
82
|
+
|
|
83
|
+
.codevisualizer .code-method {
|
|
84
|
+
color: var(--secondary-color); /* Métodos en color secundario */
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
.codevisualizer .code-builtin {
|
|
88
|
+
color: var(--secondary-color); /* Objetos integrados en color secundario */
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
.codevisualizer .code-tag {
|
|
92
|
+
color: var(--primary-color); /* Tags HTML en color primario */
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
.codevisualizer .code-attribute {
|
|
96
|
+
color: var(--warning-color); /* Atributos HTML en color de advertencia */
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
.codevisualizer .code-selector {
|
|
100
|
+
color: var(--primary-color); /* Selectores CSS en color primario */
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
.codevisualizer .code-property {
|
|
104
|
+
color: var(--secondary-color); /* Propiedades CSS en color secundario */
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
.codevisualizer .code-value {
|
|
108
|
+
color: var(--success-color); /* Valores CSS en color de éxito */
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
.codevisualizer .code-unit {
|
|
112
|
+
color: var(--warning-color); /* Unidades CSS en color de advertencia */
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
.codevisualizer .code-color {
|
|
116
|
+
color: var(--font-primary-color); /* Colores CSS en color primario */
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/* Media query para diseño responsivo */
|
|
120
|
+
@media screen and (max-width: 768px) {
|
|
121
|
+
.codevisualizer_container {
|
|
122
|
+
font-size: 12px;
|
|
123
|
+
padding: 25px 8px 8px;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
.copy-button {
|
|
127
|
+
font-size: 10px;
|
|
128
|
+
padding: 3px 6px;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
@@ -5,10 +5,13 @@ export default class CodeVisualizer extends HTMLElement {
|
|
|
5
5
|
|
|
6
6
|
this.$container = this.querySelector('.codevisualizer_container');
|
|
7
7
|
this.$code = this.querySelector('.codevisualizer');
|
|
8
|
+
this.$copyButton = this.querySelector('.copy-button');
|
|
9
|
+
|
|
10
|
+
// Configurar el botón de copiado
|
|
11
|
+
this.$copyButton.addEventListener('click', () => this.copyCodeToClipboard());
|
|
8
12
|
|
|
9
13
|
slice.controller.setComponentProps(this, props);
|
|
10
14
|
this.debuggerProps = ['language', 'value'];
|
|
11
|
-
this.editor = null;
|
|
12
15
|
}
|
|
13
16
|
|
|
14
17
|
set value(value) {
|
|
@@ -33,13 +36,228 @@ export default class CodeVisualizer extends HTMLElement {
|
|
|
33
36
|
|
|
34
37
|
visualizeCode() {
|
|
35
38
|
if (this._value && this._language) {
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
39
|
+
const highlightedCode = this.highlightCode(this._value, this._language);
|
|
40
|
+
this.$code.innerHTML = `<pre><code class="language-${this._language}">${highlightedCode}</code></pre>`;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
copyCodeToClipboard() {
|
|
45
|
+
// Obtenemos el texto sin formato (sin las etiquetas HTML de resaltado)
|
|
46
|
+
const textToCopy = this._value;
|
|
47
|
+
|
|
48
|
+
// Utilizar el API de clipboard
|
|
49
|
+
navigator.clipboard.writeText(textToCopy)
|
|
50
|
+
.then(() => {
|
|
51
|
+
// Cambiar el texto del botón temporalmente para indicar éxito
|
|
52
|
+
this.$copyButton.textContent = '✓ Copiado!';
|
|
53
|
+
this.$copyButton.classList.add('copied');
|
|
54
|
+
|
|
55
|
+
// Restaurar el texto original después de 1.5 segundos
|
|
56
|
+
setTimeout(() => {
|
|
57
|
+
this.$copyButton.textContent = 'Copiar';
|
|
58
|
+
this.$copyButton.classList.remove('copied');
|
|
59
|
+
}, 1500);
|
|
60
|
+
})
|
|
61
|
+
.catch(err => {
|
|
62
|
+
console.error('Error al copiar al portapapeles: ', err);
|
|
63
|
+
this.$copyButton.textContent = '❌ Error!';
|
|
64
|
+
|
|
65
|
+
setTimeout(() => {
|
|
66
|
+
this.$copyButton.textContent = 'Copiar';
|
|
67
|
+
}, 1500);
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
highlightCode(code, language) {
|
|
72
|
+
// Escape HTML to prevent XSS
|
|
73
|
+
const escapedCode = this.escapeHtml(code);
|
|
74
|
+
|
|
75
|
+
switch (language.toLowerCase()) {
|
|
76
|
+
case 'javascript':
|
|
77
|
+
case 'js':
|
|
78
|
+
return this.highlightJavaScript(escapedCode);
|
|
79
|
+
case 'html':
|
|
80
|
+
return this.highlightHtml(escapedCode);
|
|
81
|
+
case 'css':
|
|
82
|
+
return this.highlightCss(escapedCode);
|
|
83
|
+
default:
|
|
84
|
+
return escapedCode;
|
|
41
85
|
}
|
|
42
86
|
}
|
|
87
|
+
|
|
88
|
+
escapeHtml(text) {
|
|
89
|
+
return text
|
|
90
|
+
.replace(/&/g, '&')
|
|
91
|
+
.replace(/</g, '<')
|
|
92
|
+
.replace(/>/g, '>')
|
|
93
|
+
.replace(/"/g, '"')
|
|
94
|
+
.replace(/'/g, ''');
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
highlightJavaScript(code) {
|
|
98
|
+
// Este método fue rediseñado por completo para evitar problemas de superposición
|
|
99
|
+
let tokenizedCode = code;
|
|
100
|
+
|
|
101
|
+
// Creamos una estructura para almacenar todos los tokens y luego reemplazarlos de una sola vez
|
|
102
|
+
const tokens = [];
|
|
103
|
+
|
|
104
|
+
// Función para generar un ID único para cada token
|
|
105
|
+
const generateTokenId = (index) => `__TOKEN_${index}__`;
|
|
106
|
+
|
|
107
|
+
// Función para extraer tokens y reemplazarlos con marcadores
|
|
108
|
+
const extractTokens = (regex, className) => {
|
|
109
|
+
tokenizedCode = tokenizedCode.replace(regex, (match) => {
|
|
110
|
+
const tokenId = generateTokenId(tokens.length);
|
|
111
|
+
tokens.push({ id: tokenId, content: match, className });
|
|
112
|
+
return tokenId;
|
|
113
|
+
});
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
// Extraer los tokens en orden específico para evitar interferencias
|
|
117
|
+
|
|
118
|
+
// 1. Primero los comentarios
|
|
119
|
+
extractTokens(/\/\/.*$/gm, 'code-comment');
|
|
120
|
+
extractTokens(/\/\*[\s\S]*?\*\//g, 'code-comment');
|
|
121
|
+
|
|
122
|
+
// 2. Luego las cadenas de texto
|
|
123
|
+
extractTokens(/(['"`])(?:\\.|[^\\])*?\1/g, 'code-string');
|
|
124
|
+
|
|
125
|
+
// 3. Luego los números
|
|
126
|
+
extractTokens(/\b(\d+(?:\.\d+)?)\b/g, 'code-number');
|
|
127
|
+
|
|
128
|
+
// 4. Palabras clave
|
|
129
|
+
const keywords = [
|
|
130
|
+
'await', 'async', 'break', 'case', 'catch', 'class', 'const', 'continue',
|
|
131
|
+
'debugger', 'default', 'delete', 'do', 'else', 'export', 'extends', 'false',
|
|
132
|
+
'finally', 'for', 'function', 'if', 'import', 'in', 'instanceof', 'new',
|
|
133
|
+
'null', 'return', 'super', 'switch', 'this', 'throw', 'true', 'try',
|
|
134
|
+
'typeof', 'var', 'void', 'while', 'with', 'yield', 'let'
|
|
135
|
+
];
|
|
136
|
+
extractTokens(new RegExp(`\\b(${keywords.join('|')})\\b`, 'g'), 'code-keyword');
|
|
137
|
+
|
|
138
|
+
// 5. Objetos y métodos integrados
|
|
139
|
+
const builtins = [
|
|
140
|
+
'Array', 'Boolean', 'Date', 'Error', 'Function', 'JSON', 'Math',
|
|
141
|
+
'Number', 'Object', 'Promise', 'RegExp', 'String', 'console', 'document',
|
|
142
|
+
'window', 'slice', 'Map', 'Set', 'Symbol', 'setTimeout', 'setInterval'
|
|
143
|
+
];
|
|
144
|
+
extractTokens(new RegExp(`\\b(${builtins.join('|')})\\b`, 'g'), 'code-builtin');
|
|
145
|
+
|
|
146
|
+
// 6. Métodos y llamadas a funciones (esto debe ir después de extraer las palabras clave)
|
|
147
|
+
extractTokens(/(\.)([a-zA-Z_$][\w$]*)\s*(?=\()/g, 'code-method');
|
|
148
|
+
|
|
149
|
+
// Reemplazar los tokens con el HTML resaltado
|
|
150
|
+
tokens.forEach(token => {
|
|
151
|
+
tokenizedCode = tokenizedCode.replace(
|
|
152
|
+
token.id,
|
|
153
|
+
`<span class="${token.className}">${token.content}</span>`
|
|
154
|
+
);
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
return tokenizedCode;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
highlightHtml(code) {
|
|
161
|
+
// Utilizamos un enfoque similar al de JavaScript para HTML
|
|
162
|
+
let tokenizedCode = code;
|
|
163
|
+
const tokens = [];
|
|
164
|
+
const generateTokenId = (index) => `__TOKEN_${index}__`;
|
|
165
|
+
|
|
166
|
+
const extractTokens = (regex, className) => {
|
|
167
|
+
tokenizedCode = tokenizedCode.replace(regex, (match) => {
|
|
168
|
+
const tokenId = generateTokenId(tokens.length);
|
|
169
|
+
tokens.push({ id: tokenId, content: match, className });
|
|
170
|
+
return tokenId;
|
|
171
|
+
});
|
|
172
|
+
};
|
|
173
|
+
|
|
174
|
+
// Extraer tokens HTML en orden
|
|
175
|
+
|
|
176
|
+
// 1. Comentarios HTML
|
|
177
|
+
extractTokens(/<!--[\s\S]*?-->/g, 'code-comment');
|
|
178
|
+
|
|
179
|
+
// 2. Etiquetas HTML
|
|
180
|
+
extractTokens(/(<\/?[a-zA-Z0-9-]+)(\s|>)/g, (match, tag, end) => {
|
|
181
|
+
const tokenId = generateTokenId(tokens.length);
|
|
182
|
+
tokens.push({
|
|
183
|
+
id: tokenId,
|
|
184
|
+
content: `<span class="code-tag">${tag}</span>${end}`,
|
|
185
|
+
className: 'no-class' // Ya incluye su propio elemento span
|
|
186
|
+
});
|
|
187
|
+
return tokenId;
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
// 3. Atributos HTML
|
|
191
|
+
extractTokens(/\s([a-zA-Z0-9-]+)=("|')/g, (match, attr, quote) => {
|
|
192
|
+
const tokenId = generateTokenId(tokens.length);
|
|
193
|
+
tokens.push({
|
|
194
|
+
id: tokenId,
|
|
195
|
+
content: ` <span class="code-attribute">${attr}</span>=${quote}`,
|
|
196
|
+
className: 'no-class' // Ya incluye su propio elemento span
|
|
197
|
+
});
|
|
198
|
+
return tokenId;
|
|
199
|
+
});
|
|
200
|
+
|
|
201
|
+
// Reemplazar los tokens con el HTML resaltado
|
|
202
|
+
tokens.forEach(token => {
|
|
203
|
+
tokenizedCode = tokenizedCode.replace(
|
|
204
|
+
token.id,
|
|
205
|
+
token.className === 'no-class' ? token.content : `<span class="${token.className}">${token.content}</span>`
|
|
206
|
+
);
|
|
207
|
+
});
|
|
208
|
+
|
|
209
|
+
return tokenizedCode;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
highlightCss(code) {
|
|
213
|
+
// Utilizamos el mismo enfoque para CSS
|
|
214
|
+
let tokenizedCode = code;
|
|
215
|
+
const tokens = [];
|
|
216
|
+
const generateTokenId = (index) => `__TOKEN_${index}__`;
|
|
217
|
+
|
|
218
|
+
const extractTokens = (regex, className, processor = null) => {
|
|
219
|
+
tokenizedCode = tokenizedCode.replace(regex, (match, ...groups) => {
|
|
220
|
+
const tokenId = generateTokenId(tokens.length);
|
|
221
|
+
const content = processor ? processor(match, ...groups) : match;
|
|
222
|
+
tokens.push({ id: tokenId, content, className });
|
|
223
|
+
return tokenId;
|
|
224
|
+
});
|
|
225
|
+
};
|
|
226
|
+
|
|
227
|
+
// Comentarios CSS
|
|
228
|
+
extractTokens(/\/\*[\s\S]*?\*\//g, 'code-comment');
|
|
229
|
+
|
|
230
|
+
// Selectores CSS
|
|
231
|
+
extractTokens(/([^\{\}]+)(?=\{)/g, 'code-selector');
|
|
232
|
+
|
|
233
|
+
// Propiedad y valor CSS (manipulando la coincidencia para preservar la estructura)
|
|
234
|
+
tokenizedCode = tokenizedCode.replace(/(\s*)([a-zA-Z-]+)(\s*):(\s*)([^;\{\}]+)(?=;)/g, (match, space1, prop, space2, space3, value) => {
|
|
235
|
+
const propTokenId = generateTokenId(tokens.length);
|
|
236
|
+
tokens.push({ id: propTokenId, content: prop, className: 'code-property' });
|
|
237
|
+
|
|
238
|
+
const valueTokenId = generateTokenId(tokens.length);
|
|
239
|
+
tokens.push({ id: valueTokenId, content: value, className: 'code-value' });
|
|
240
|
+
|
|
241
|
+
return `${space1}<span class="code-property">${prop}</span>${space2}:${space3}<span class="code-value">${value}</span>`;
|
|
242
|
+
});
|
|
243
|
+
|
|
244
|
+
// Colores CSS
|
|
245
|
+
extractTokens(/#([a-fA-F0-9]{3,6})\b/g, 'code-color');
|
|
246
|
+
|
|
247
|
+
// Reemplazar los tokens restantes
|
|
248
|
+
tokens.forEach(token => {
|
|
249
|
+
if (token.className !== 'no-replace') {
|
|
250
|
+
tokenizedCode = tokenizedCode.replace(
|
|
251
|
+
token.id,
|
|
252
|
+
`<span class="${token.className}">${token.content}</span>`
|
|
253
|
+
);
|
|
254
|
+
}
|
|
255
|
+
});
|
|
256
|
+
|
|
257
|
+
return tokenizedCode;
|
|
258
|
+
}
|
|
43
259
|
}
|
|
44
260
|
|
|
45
261
|
customElements.define('slice-codevisualizer', CodeVisualizer);
|
|
262
|
+
|
|
263
|
+
|