-
-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Wrap renderers using Builder pattern, refactor Render instantiation
- Loading branch information
1 parent
b8578b3
commit 34ae794
Showing
9 changed files
with
131 additions
and
164 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
src/util/renderers/abstractRenderer.ts → src/util/builders/renderers/abstract.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
export abstract class Renderer { | ||
abstract render(): Promise<any> | ||
abstract render(): void | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { Renderer } from './abstract.js' | ||
import { DesktopRendererDirector } from './desktop.director.js' | ||
import { VisualEditorRendererDirector } from './visual-editor.director.js' | ||
|
||
export class RendererBuilder { | ||
private renderer: Renderer | null = null | ||
|
||
setDesktopRenderer(renderOpts): RendererBuilder { | ||
this.renderer = new DesktopRendererDirector(renderOpts) | ||
return this | ||
} | ||
|
||
setVisualEditorRenderer(renderOpts): RendererBuilder { | ||
this.renderer = new VisualEditorRendererDirector(renderOpts) | ||
return this | ||
} | ||
|
||
createRenderer(): Renderer { | ||
if (this.renderer === null) { | ||
throw new Error('Article renderer type is not set.') | ||
} | ||
return this.renderer | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { RendererBuilder } from './renderer.builder.js' | ||
|
||
export class ArticleRenderer { | ||
private builder: RendererBuilder = new RendererBuilder() | ||
|
||
public renderArticle(renderOpts: any, renderMode: string): void { | ||
switch (renderMode) { | ||
case 'desktop': | ||
this.builder.setDesktopRenderer(renderOpts) | ||
break | ||
case 'visual-editor': | ||
this.builder.setVisualEditorRenderer(renderOpts) | ||
break | ||
default: | ||
throw new Error(`Unknown mwType: ${renderMode}`) | ||
} | ||
|
||
const renderer = this.builder.createRenderer() | ||
return renderer.render() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import domino from 'domino' | ||
import { Renderer } from './abstract.js' | ||
|
||
/* | ||
Represent 'https://{wikimedia-wiki}/w/api.php?action=visualeditor&mobileformat=html&format=json&paction=parse&page={title}' | ||
or | ||
'https://{3rd-part-wikimedia-wiki}/w/api.php?action=visualeditor&mobileformat=html&format=json&paction=parse&page={title}' | ||
*/ | ||
export class VisualEditorRendererDirector extends Renderer { | ||
private data | ||
private articleId | ||
private articleDetail | ||
private isMainPage | ||
|
||
constructor(renderOpts) { | ||
super() | ||
this.data = renderOpts.data | ||
this.articleId = renderOpts.articleId | ||
this.articleDetail = renderOpts.articleDetail | ||
this.isMainPage = renderOpts.isMainPage | ||
} | ||
|
||
// visualeditor.content returns an HTML for rendering | ||
public async render(): Promise<any> { | ||
if (!this.data) { | ||
throw new Error(`Cannot render [${this.data}] into an article`) | ||
} | ||
return this.isMainPage ? this.data.visualeditor.content : this.injectHeader(this.data.visualeditor.content, this.articleDetail) | ||
} | ||
|
||
private injectHeader(content: string, articleDetail: ArticleDetail): string { | ||
const doc = domino.createDocument(content) | ||
const header = doc.createElement('h1') | ||
|
||
header.appendChild(doc.createTextNode(articleDetail.title)) | ||
header.classList.add('article-header') | ||
|
||
const target = doc.querySelector('body.mw-body-content') | ||
|
||
target.insertAdjacentElement('afterbegin', header) | ||
|
||
return doc.documentElement.outerHTML | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters