-
-
Notifications
You must be signed in to change notification settings - Fork 0
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
1 parent
afd151c
commit 1b73551
Showing
5 changed files
with
110 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/** | ||
* @typedef {import('petite-vue/dist/types/context').Context} Context | ||
* @typedef {import('../abstracts/ReactivePluginBase').default} ReactivePluginBase | ||
* @typedef {import('../abstracts/ReactiveSingleton').default} ReactiveSingleton | ||
*/ | ||
|
||
/** | ||
* Prepares components in a mounted element or template. | ||
* | ||
* @param {ReactivePluginBase|ReactiveSingleton} parent | ||
* @param {HTMLElement} mountElement | ||
*/ | ||
function prepareComponents(parent, mountElement) { | ||
if (!parent.components || !parent.components()) { | ||
return; | ||
} | ||
|
||
const components = parent.components(); | ||
|
||
Object.keys(components).forEach((componentName) => { | ||
const componentElements = mountElement.querySelectorAll(componentName); | ||
|
||
// Replace each component holder with a template tag using the correct directive | ||
componentElements.forEach((componentElement) => { | ||
const templateElement = document.createElement('ins'); | ||
templateElement.setAttribute('v-component', componentName); | ||
|
||
if (componentElement.hasAttributes()) { | ||
const { attributes } = componentElement; | ||
|
||
for (let i = 0; i < attributes.length; i += 1) { | ||
const { name, value } = attributes[i]; | ||
templateElement.setAttribute(name, value); | ||
} | ||
} | ||
|
||
componentElement.replaceWith(templateElement); | ||
}); | ||
}); | ||
} | ||
|
||
/** | ||
* Cleanup/unmount component | ||
* | ||
* @param {Context} context | ||
*/ | ||
function componentCleanup(/* context */) { | ||
} | ||
|
||
/** | ||
* Directive to link components to a parent. | ||
* | ||
* @param {Context} context | ||
* @param {ReactivePluginBase|ReactiveSingleton} parent | ||
*/ | ||
function componentDirective(context /* , parent */) { | ||
// const scopeKeys = Object.getOwnPropertyNames(context.ctx.scope); | ||
|
||
return () => { | ||
componentCleanup(context); | ||
}; | ||
} | ||
|
||
export { | ||
prepareComponents, | ||
componentDirective, | ||
}; |
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,34 @@ | ||
/** | ||
* Decimal to hexidecimal | ||
* | ||
* @param {number} dec | ||
* @returns {string} | ||
*/ | ||
function dec2hex(dec) { | ||
return dec.toString(16).padStart(2, '0'); | ||
} | ||
|
||
const usedIds = []; | ||
|
||
/** | ||
* Generates a random ID. | ||
* | ||
* @param {Number} length | ||
* @return {string} | ||
*/ | ||
function generateId(length = 32) { | ||
const arr = new Uint8Array(length / 2); | ||
let value = ''; | ||
|
||
do { | ||
window.crypto.getRandomValues(arr); | ||
value = Array.from(arr, dec2hex).join(''); | ||
} while (usedIds.includes(value)); | ||
|
||
return value; | ||
} | ||
|
||
export { | ||
generateId, | ||
dec2hex, | ||
}; |