Skip to content

Commit

Permalink
feature: load renderers with options
Browse files Browse the repository at this point in the history
  • Loading branch information
jankapunkt committed Jul 4, 2023
1 parent eccfa9b commit 25d1783
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 29 deletions.
38 changes: 30 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,36 @@ required in many of the lea.online applications.
#### Renderers (Templates)

All lea.online applications resolve around certain interactions, mostly them being part of several units.
In order to dynamically display (and edit) these interactions, we use a set of dynamic renderers:

- Factory - A factory to initialize a renderer by given name with given data
- Text - renders plain text
- Image - renders a lazy-loaded image
- Item - render specific item types
- Markdown - renders markdown
- Page - renders a page with mixed and variable content
In order to dynamically display (and edit) these interactions, we use a set of dynamic renderers:

| Name | key | Description |
|----------|------------|---------------------------------------------------------------------------------------------------|
| Factory | `factory` | A Blaze factory template to dynamically load and execute a renderer by given name with given data |
| Page | `page` | renders a page with mixed and variable content
| Text | `text` | renders plain text |
| Image | `image` | renders a lazy-loaded image |
| Item | `item` | render-factory for specific item types |
| Markdown | `markdown` | renders markdown, requires a custom renderer function that uses the host app's markdown parser|

**Init task renderers**

Initializing renderer allows to pass options by using their key
and pass options as Object:

```js
import { TaskRenderers } from '../../renderers/TaskRenderers'

TaskRenderers.init({
markdown: {
renderer: async txt => {
const mdOptions = { input: txt, renderer: defaultMarkdownRendererName }
return LeaMarkdown.parse(mdOptions)
}
}
})
.catch(console.error)
.then(() => { /* core renderers loaded */ })
```

## Run tests

Expand Down
4 changes: 0 additions & 4 deletions components/text/text.css
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@
text-decoration: underline;
}

.lea-text-token-active {
font-weight: bold;
}

.text-wrapper:hover {
cursor: pointer;
}
Expand Down
2 changes: 1 addition & 1 deletion components/text/text.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ Template.text.helpers({
const fill = (data.fill && 'flex-fill') || ''
const customTokenClass = data.tokenClass || ''
const playingClass = instance.isPlaying(currentIndex)
? 'lea-text-token-active text-primary'
? 'lea-text-bold text-primary'
: ''
return { class: `lea-text-token ${fill} ${playingClass} ${customTokenClass}` }
}
Expand Down
9 changes: 2 additions & 7 deletions package.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-env meteor */
Package.describe({
name: 'leaonline:ui',
version: '1.0.0',
version: '1.1.0',
// Brief, one-line summary of the package.
summary: 'Common Blaze ui-components for .lea apps',
// URL to the Git repository containing the source code for this package.
Expand All @@ -25,13 +25,8 @@ Package.onUse(function (api) {

Package.onTest(function (api) {
api.use('ecmascript')
api.use('mongo')
api.use('random')
api.use('templating')
api.use('reactive-dict')
api.use('reactive-var')
api.use('dynamic-import')
api.use(['lmieulet:meteor-legacy-coverage', 'lmieulet:meteor-coverage', 'meteortesting:mocha'])
api.use('leaonline:corelib')
api.mainModule('ui-tests.js', 'client')
api.mainModule('ui-tests.js', ['server','client'])

Check failure on line 31 in package.js

View workflow job for this annotation

GitHub Actions / Javascript lint

A space is required after ','
})
23 changes: 16 additions & 7 deletions renderers/Renderers.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,9 @@ const defaults = {
label: 'taskRenderers.layout.markdown.title',
icon: 'hashtag',
template: 'markdownRenderer',
async load () {
return import('./markdown/markdownRenderer')
async load (options) {
const { Markdown } = await import('./markdown/markdownRenderer')
Markdown.init(options)
}
},
image: {
Expand Down Expand Up @@ -219,20 +220,28 @@ export const TaskRenderers = {
// should we do caching here or on a component level?
return Array.from(rendererMap.values()).filter(el => el.group === group)
},
init: async function () {
init: async function (options) {
if (_initialized) return true

// load the factory
const factory = TaskRenderers.get(defaults.factory.name)
await factory.load()
await factory.load(factory.__initOptions)

const pageRender = TaskRenderers.get(defaults.page.name)
await pageRender.load()
await pageRender.load(pageRender.__initOptions)

// register the default item renderers
const { CoreRenderers } = await import('./CoreRenderers')
CoreRenderers.forEach(rendererContext => {
rendererMap.set(rendererContext.name, rendererContext)
CoreRenderers.forEach(rendererCtx => {
rendererMap.set(rendererCtx.name, rendererCtx)
})

// once we have all renderer added to the map
// we can assign them init options
rendererMap.forEach(rendererCtx => {
if (rendererCtx.name in options) {
rendererCtx.__initOptions = options[rendererCtx.name]
}
})

_initialized = true
Expand Down
2 changes: 1 addition & 1 deletion renderers/factory/TaskRendererFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ Template.TaskRendererFactory.onCreated(function () {
return
}

rendererContext.load()
rendererContext.load(rendererContext.__initOptions)
.then(() => {
loaded.set(content.subtype, rendererContext.template)
if (content.onLoadComplete) content.onLoadComplete(content.subtype)
Expand Down
2 changes: 1 addition & 1 deletion renderers/markdown/markdownRenderer.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
<div class="w-100 lea-text {{#if lineHeight}}line-height-{{lineHeight}}{{/if}} {{#if
padding}}p-{{padding}}{{/if}} {{#if background}}bg-{{background}}{{/if}} {{#if
textColor}}text-{{textColor}}{{/if}}">
{{#markdown}}{{value}}{{/markdown}}
{{{render}}}
</div>
</template>
30 changes: 30 additions & 0 deletions renderers/markdown/markdownRenderer.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,32 @@
import { Template } from 'meteor/templating'
import { ReactiveVar } from 'meteor/reactive-var'
import './markdownRenderer.css'
import './markdownRenderer.html'

Template.markdownRenderer.onCreated(function () {
const instance = this
instance.markdown = new ReactiveVar()

instance.autorun(async () => {
const { value } = Template.currentData()

if (typeof value === 'string') {
const rendered = await Markdown.renderer(value)
instance.markdown.set(rendered)
}
})
})

Template.markdownRenderer.helpers({
render () {
return Template.instance().markdown.get()
}
})

export const Markdown = {}

Markdown.renderer = async md => md

Markdown.init = (options) => {
Markdown.renderer = options.renderer
}

0 comments on commit 25d1783

Please sign in to comment.