Commit aa7aa7a6 authored by Robert Knight's avatar Robert Knight

Refactor Notebook and Profile modal controllers to use PreactContainer

parent 1a40d976
import { render } from 'preact';
import type { Destroyable } from '../types/annotator'; import type { Destroyable } from '../types/annotator';
import NotebookModal from './components/NotebookModal'; import NotebookModal from './components/NotebookModal';
import type { NotebookConfig } from './components/NotebookModal'; import type { NotebookConfig } from './components/NotebookModal';
import type { EventBus } from './util/emitter'; import type { EventBus } from './util/emitter';
import { createShadowRoot } from './util/shadow-root'; import { PreactContainer } from './util/preact-container';
export class Notebook implements Destroyable { export class Notebook implements Destroyable {
private _outerContainer: HTMLElement; private _container: PreactContainer;
private _shadowRoot: ShadowRoot;
/** /**
* @param eventBus - Enables communication between components sharing the * @param eventBus - Enables communication between components sharing the
...@@ -19,22 +16,14 @@ export class Notebook implements Destroyable { ...@@ -19,22 +16,14 @@ export class Notebook implements Destroyable {
eventBus: EventBus, eventBus: EventBus,
config: NotebookConfig, config: NotebookConfig,
) { ) {
/** this._container = new PreactContainer('notebook', () => (
* Un-styled shadow host for the notebook content. <NotebookModal eventBus={eventBus} config={config} />
* This isolates the notebook from the page's styles. ));
*/ element.append(this._container.element);
this._outerContainer = document.createElement('hypothesis-notebook'); this._container.render();
element.appendChild(this._outerContainer);
this._shadowRoot = createShadowRoot(this._outerContainer);
render(
<NotebookModal eventBus={eventBus} config={config} />,
this._shadowRoot,
);
} }
destroy() { destroy() {
render(null, this._shadowRoot); this._container.destroy();
this._outerContainer.remove();
} }
} }
import { render } from 'preact';
import type { Destroyable } from '../types/annotator'; import type { Destroyable } from '../types/annotator';
import type { ProfileConfig } from './components/ProfileModal'; import type { ProfileConfig } from './components/ProfileModal';
import ProfileModal from './components/ProfileModal'; import ProfileModal from './components/ProfileModal';
import type { EventBus } from './util/emitter'; import type { EventBus } from './util/emitter';
import { createShadowRoot } from './util/shadow-root'; import { PreactContainer } from './util/preact-container';
export class Profile implements Destroyable { export class Profile implements Destroyable {
private _outerContainer: HTMLElement; private _container: PreactContainer;
public shadowRoot: ShadowRoot;
constructor(element: HTMLElement, eventBus: EventBus, config: ProfileConfig) { constructor(element: HTMLElement, eventBus: EventBus, config: ProfileConfig) {
this._outerContainer = document.createElement('hypothesis-profile'); this._container = new PreactContainer('profile', () => (
element.appendChild(this._outerContainer); <ProfileModal eventBus={eventBus} config={config} />
this.shadowRoot = createShadowRoot(this._outerContainer); ));
element.append(this._container.element);
render( this._container.render();
<ProfileModal eventBus={eventBus} config={config} />,
this.shadowRoot,
);
} }
destroy(): void { destroy(): void {
render(null, this.shadowRoot); this._container.destroy();
this._outerContainer.remove();
} }
} }
...@@ -47,7 +47,7 @@ describe('Notebook', () => { ...@@ -47,7 +47,7 @@ describe('Notebook', () => {
it('creates the container', () => { it('creates the container', () => {
assert.isFalse(container.hasChildNodes()); assert.isFalse(container.hasChildNodes());
const notebook = createNotebook(); const notebook = createNotebook();
const shadowRoot = notebook._outerContainer.shadowRoot; const shadowRoot = notebook._container.element.shadowRoot;
assert.isNotNull(shadowRoot); assert.isNotNull(shadowRoot);
assert.isNotNull(shadowRoot.querySelector('#notebook-modal')); assert.isNotNull(shadowRoot.querySelector('#notebook-modal'));
}); });
......
...@@ -47,7 +47,7 @@ describe('Profile', () => { ...@@ -47,7 +47,7 @@ describe('Profile', () => {
it('creates the container', () => { it('creates the container', () => {
assert.isFalse(container.hasChildNodes()); assert.isFalse(container.hasChildNodes());
const profile = createProfile(); const profile = createProfile();
const shadowRoot = profile._outerContainer.shadowRoot; const shadowRoot = profile._container.element.shadowRoot;
assert.isNotNull(shadowRoot); assert.isNotNull(shadowRoot);
assert.isNotNull(shadowRoot.querySelector('#profile-modal')); assert.isNotNull(shadowRoot.querySelector('#profile-modal'));
}); });
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment