Skip to content

Commit ae1fe86

Browse files
authored
Merge pull request #513 from com-pas/fix/pass-compas-api-to-plugins
fix: pass compasApi to plugins
2 parents f414a6d + f015e8f commit ae1fe86

2 files changed

Lines changed: 76 additions & 5 deletions

File tree

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/addons/CompasLayout.ts

Lines changed: 74 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,54 @@
1-
import { customElement, property, TemplateResult } from 'lit-element';
1+
import {
2+
customElement,
3+
html,
4+
property,
5+
TemplateResult,
6+
} from 'lit-element';
27
import { get } from 'lit-translate';
8+
import { classMap } from 'lit-html/directives/class-map.js';
9+
import { OscdApi } from '@compas-oscd/core';
310

4-
import type { UserInfoEvent } from '../compas/foundation';
11+
import type { UserInfoEvent } from '../compas/foundation.js';
12+
import type { CompasApi } from '../open-scd.js';
513

614
import { OscdLayout } from '@compas-oscd/open-scd/dist/addons/Layout.js';
715

16+
interface RenderAblePlugin {
17+
src?: string;
18+
kind: string;
19+
content?: { tag?: string };
20+
}
21+
22+
function staticTagHtml(
23+
oldStrings: ReadonlyArray<string>,
24+
...oldArgs: unknown[]
25+
): TemplateResult {
26+
const args = [...oldArgs];
27+
const firstArg = args.shift();
28+
const lastArg = args.pop();
29+
30+
if (firstArg !== lastArg)
31+
throw new Error(
32+
`Opening tag <${firstArg}> does not match closing tag </${lastArg}>.`
33+
);
34+
35+
const strings = [...oldStrings] as string[] & { raw: string[] };
36+
const firstString = strings.shift();
37+
const secondString = strings.shift();
38+
39+
const lastString = strings.pop();
40+
const penultimateString = strings.pop();
41+
42+
strings.unshift(`${firstString}${firstArg}${secondString}`);
43+
strings.push(`${penultimateString}${lastArg}${lastString}`);
44+
45+
return html(<TemplateStringsArray>strings, ...args);
46+
}
47+
848
@customElement('compas-layout')
949
export class CompasLayout extends OscdLayout {
1050
@property({ type: String }) username: string | undefined;
51+
@property({ attribute: false }) compasApi?: CompasApi;
1152

1253
connectedCallback(): void {
1354
super.connectedCallback();
@@ -20,7 +61,37 @@ export class CompasLayout extends OscdLayout {
2061
this.username = event.detail.name;
2162
}
2263

23-
protected renderActionItems(): TemplateResult {
64+
protected renderPluginContent(plugin: RenderAblePlugin): TemplateResult {
65+
const tag = plugin.content?.tag ?? '';
66+
67+
if (!tag) {
68+
return html``;
69+
}
70+
71+
const osdcApi = new OscdApi(tag);
72+
return staticTagHtml`<${tag}
73+
.doc=${this.doc}
74+
.docName=${this.docName}
75+
.editCount=${this.editCount}
76+
.plugins=${this.host.storedPlugins}
77+
.docId=${this.host.docId}
78+
.pluginId=${plugin.src}
79+
.nsdoc=${this.host.nsdoc}
80+
.docs=${this.host.docs}
81+
.locale=${this.host.locale}
82+
.oscdApi=${osdcApi}
83+
.editor=${this.editor}
84+
.compasApi=${this.compasApi}
85+
class="${classMap({
86+
plugin: true,
87+
menu: plugin.kind === 'menu',
88+
validator: plugin.kind === 'validator',
89+
editor: plugin.kind === 'editor',
90+
})}"
91+
></${tag}>`;
92+
}
93+
94+
protected renderActionItems() {
2495
return this.componentHtml`
2596
${
2697
this.username

0 commit comments

Comments
 (0)