Skip to content

Commit

Permalink
Initial work on components
Browse files Browse the repository at this point in the history
  • Loading branch information
bennothommo committed Sep 8, 2023
1 parent afd151c commit 1b73551
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 3 deletions.
2 changes: 1 addition & 1 deletion packages/reactivity/src/abstracts/ReactivePluginBase.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
reactivityMount,
template,
mountTo,
} from './shared';
} from '../lib/reactivity';

/**
* Reactive plugin base abstract.
Expand Down
2 changes: 1 addition & 1 deletion packages/reactivity/src/abstracts/ReactiveSingleton.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
reactivityMount,
template,
mountTo,
} from './shared';
} from '../lib/reactivity';

/**
* Reactive singleton abstract.
Expand Down
67 changes: 67 additions & 0 deletions packages/reactivity/src/lib/components.js
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,
};
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { createApp, reactive } from 'petite-vue';
import { componentDirective, prepareComponents } from './components';

/**
* @typedef {import('../abstracts/ReactivePluginBase').default} ReactivePluginBase
Expand Down Expand Up @@ -98,7 +99,12 @@ function reactivityMount(element = null) {
}
}

this.$app = createApp(this.$data).mount(mountedElement);
// Prepare components
prepareComponents(this, mountedElement);

this.$app = createApp(this.$data)
.directive('component', (context) => componentDirective(context, this))
.mount(mountedElement);
this.$el = mountedElement;

// Import next tick into plugin
Expand Down
34 changes: 34 additions & 0 deletions packages/reactivity/src/lib/util.js
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,
};

0 comments on commit 1b73551

Please sign in to comment.