Skip to content

Instantly share code, notes, and snippets.

@bigopon
Forked from MaximBalaganskiy/index.html
Last active December 19, 2020 05:59
Show Gist options
  • Save bigopon/996a176e979c4c8dd84857ca50f99f37 to your computer and use it in GitHub Desktop.
Save bigopon/996a176e979c4c8dd84857ca50f99f37 to your computer and use it in GitHub Desktop.
process-content
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Dumber Gist</title>
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1.0, user-scalable=no">
<base href="/">
</head>
<!--
Dumber gist uses dumber bundler, the default bundle file
is /dist/entry-bundle.js.
The starting module is pointed to aurelia-bootstrapper
(data-main attribute on script) for Aurelia,
The aurelia bootstrapper then loads up user module "main"
(aurelia-app attribute on <body>) which is your src/main.ts.
-->
<body aurelia-app="main">
<script src="/dist/entry-bundle.js" data-main="aurelia-bootstrapper"></script>
</body>
</html>
{
"dependencies": {
"aurelia-bootstrapper": "^2.3.3"
}
}
<template>
<!-- Try to create a css/scss/sass/less file then require it here -->
<h1>${message}</h1>
<slotted-element>
<troubled><span>Works in root!</span></troubled>
<parent></parent>
</slotted-element>
</template>
export class App {
public message: string = 'Hello Aurelia!';
}
import {Aurelia, FrameworkConfiguration} from 'aurelia-framework';
export function configure(aurelia: Aurelia) {
aurelia.use
.standardConfiguration()
.developmentLogging('info')
.globalResources(['troubled','slotted-element']);
aurelia.use.postTask(() => {
const config = new FrameworkConfiguration(aurelia);
config.globalResources(['parent']);
return config.apply();
});
aurelia.start().then(() => aurelia.setRoot());
}
<template>
<!--<require from="./troubled"></require>-->
<troubled repeat.for="i of items"><span>Works in parent!</span></troubled>
</template>
import {
customElement, processContent, ViewCompiler, ViewResources, BehaviorInstruction,
ViewCompileInstruction, useView, PLATFORM, containerless
} from 'aurelia-framework';
@customElement('parent')
@containerless
export class Parent {
items = [1,2];
}
<template>
Slotted
<div>
<slot></slot>
</div>
</template>
export class SlottedElement {
}
<template>The real template is in the processContent function</template>
import {
customElement, processContent, ViewCompiler, ViewResources, BehaviorInstruction,
ViewCompileInstruction, useView, PLATFORM
} from 'aurelia-framework';
@customElement('troubled')
@processContent(Troubled.processContent)
export class Troubled {
static processContent(viewCompiler: ViewCompiler, _resources: ViewResources, element: Element, instruction: BehaviorInstruction) {
const elementResource = instruction.type;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const viewResources = (elementResource as any).viewFactory.resources;
const shouldCreateFactory = element.children.length > 0;
// inside this if, we are going to create a new factory to supply
// to the creation of the custom element
if (shouldCreateFactory) {
const compileInstruction = new ViewCompileInstruction(/* shadow DOM? */false, true);
const template = `
<template class="troubled">
<div class="troubled__container">
${element.innerHTML}
</div>
</template>`;
const viewFactory = viewCompiler.compile(template, viewResources, compileInstruction);
// override default view factory
// Aurelia fallbacks to instruction view factory if there's any
instruction.viewFactory = viewFactory;
// only use the content as template
// remove all after use to avoid undesirable visual
element.innerHTML = '';
}
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment