Add VirtualSequenceDiagram convenience wrapper in standalone exports
This commit is contained in:
parent
43830414ff
commit
3c8ee0acf6
|
@ -1,21 +1,6 @@
|
||||||
#!/usr/bin/env node
|
#!/usr/bin/env node
|
||||||
|
|
||||||
const {
|
const {VirtualSequenceDiagram} = require('../lib/sequence-diagram');
|
||||||
headlessTextSizerFactory,
|
|
||||||
SequenceDiagram,
|
|
||||||
VirtualDocument,
|
|
||||||
} = require('../lib/sequence-diagram');
|
|
||||||
|
|
||||||
function render(code) {
|
|
||||||
const sd = new SequenceDiagram({
|
|
||||||
code,
|
|
||||||
document: new VirtualDocument(),
|
|
||||||
namespace: '',
|
|
||||||
textSizerFactory: headlessTextSizerFactory,
|
|
||||||
});
|
|
||||||
|
|
||||||
return sd.dom().outerHTML;
|
|
||||||
}
|
|
||||||
|
|
||||||
function read(pipe) {
|
function read(pipe) {
|
||||||
return new Promise((resolve) => {
|
return new Promise((resolve) => {
|
||||||
|
@ -41,6 +26,15 @@ function getCodeArg() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
getCodeArg().then((code) => {
|
function processError(err) {
|
||||||
process.stdout.write(render(code) + '\n');
|
if(typeof err === 'object' && err.message) {
|
||||||
});
|
return err.message;
|
||||||
|
} else {
|
||||||
|
return err;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
getCodeArg()
|
||||||
|
.then(VirtualSequenceDiagram.render)
|
||||||
|
.then((svg) => process.stdout.write(svg + '\n'))
|
||||||
|
.catch((err) => process.stderr.write(processError(err) + '\n'));
|
||||||
|
|
|
@ -9679,6 +9679,14 @@
|
||||||
return this.renderer.getThemes();
|
return this.renderer.getThemes();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
getSVGCodeSynchronous() {
|
||||||
|
return this.exporter.getSVGContent(this.renderer);
|
||||||
|
}
|
||||||
|
|
||||||
|
getSVGCode() {
|
||||||
|
return Promise.resolve(this.getSVGCodeSynchronous());
|
||||||
|
}
|
||||||
|
|
||||||
getSVGSynchronous() {
|
getSVGSynchronous() {
|
||||||
return this.exporter.getSVGURL(this.renderer);
|
return this.exporter.getSVGURL(this.renderer);
|
||||||
}
|
}
|
||||||
|
|
File diff suppressed because one or more lines are too long
|
@ -9679,6 +9679,14 @@
|
||||||
return this.renderer.getThemes();
|
return this.renderer.getThemes();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
getSVGCodeSynchronous() {
|
||||||
|
return this.exporter.getSVGContent(this.renderer);
|
||||||
|
}
|
||||||
|
|
||||||
|
getSVGCode() {
|
||||||
|
return Promise.resolve(this.getSVGCodeSynchronous());
|
||||||
|
}
|
||||||
|
|
||||||
getSVGSynchronous() {
|
getSVGSynchronous() {
|
||||||
return this.exporter.getSVGURL(this.renderer);
|
return this.exporter.getSVGURL(this.renderer);
|
||||||
}
|
}
|
||||||
|
@ -10194,7 +10202,7 @@
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class UnitaryTextSizer {
|
class VirtualTextSizer {
|
||||||
// Simplified text sizer, which assumes all characters render as
|
// Simplified text sizer, which assumes all characters render as
|
||||||
// 1x1 px squares for repeatable renders in all browsers
|
// 1x1 px squares for repeatable renders in all browsers
|
||||||
|
|
||||||
|
@ -10223,10 +10231,51 @@
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const virtualTextSizerFactory = () => new VirtualTextSizer();
|
||||||
|
|
||||||
|
class VirtualSequenceDiagram extends SequenceDiagram {
|
||||||
|
constructor(code = null, options = {}) {
|
||||||
|
const opts = {
|
||||||
|
document: new VirtualDocument(),
|
||||||
|
namespace: '',
|
||||||
|
textSizerFactory: virtualTextSizerFactory,
|
||||||
|
};
|
||||||
|
|
||||||
|
if(code && typeof code === 'object') {
|
||||||
|
Object.assign(opts, code);
|
||||||
|
} else {
|
||||||
|
Object.assign(opts, options, {code});
|
||||||
|
}
|
||||||
|
|
||||||
|
Object.assign(opts, {
|
||||||
|
container: null,
|
||||||
|
interactive: false,
|
||||||
|
});
|
||||||
|
|
||||||
|
super(opts);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function render(code, options = {}) {
|
||||||
|
return new VirtualSequenceDiagram(code, options).getSVGCodeSynchronous();
|
||||||
|
}
|
||||||
|
|
||||||
|
Object.assign(VirtualSequenceDiagram, {
|
||||||
|
Exporter: SequenceDiagram.Exporter,
|
||||||
|
Generator: SequenceDiagram.Generator,
|
||||||
|
Parser: SequenceDiagram.Parser,
|
||||||
|
Renderer: SequenceDiagram.Renderer,
|
||||||
|
addTheme: SequenceDiagram.addTheme,
|
||||||
|
getDefaultThemeNames: SequenceDiagram.getDefaultThemeNames,
|
||||||
|
render,
|
||||||
|
themes: SequenceDiagram.themes,
|
||||||
|
});
|
||||||
|
|
||||||
const out = {
|
const out = {
|
||||||
SequenceDiagram,
|
SequenceDiagram,
|
||||||
VirtualDocument,
|
VirtualDocument,
|
||||||
headlessTextSizerFactory: () => new UnitaryTextSizer(),
|
VirtualSequenceDiagram,
|
||||||
|
virtualTextSizerFactory,
|
||||||
};
|
};
|
||||||
|
|
||||||
if(typeof exports !== 'undefined') {
|
if(typeof exports !== 'undefined') {
|
||||||
|
|
File diff suppressed because one or more lines are too long
|
@ -192,6 +192,14 @@ export default class SequenceDiagram extends EventObject {
|
||||||
return this.renderer.getThemes();
|
return this.renderer.getThemes();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
getSVGCodeSynchronous() {
|
||||||
|
return this.exporter.getSVGContent(this.renderer);
|
||||||
|
}
|
||||||
|
|
||||||
|
getSVGCode() {
|
||||||
|
return Promise.resolve(this.getSVGCodeSynchronous());
|
||||||
|
}
|
||||||
|
|
||||||
getSVGSynchronous() {
|
getSVGSynchronous() {
|
||||||
return this.exporter.getSVGURL(this.renderer);
|
return this.exporter.getSVGURL(this.renderer);
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,45 @@
|
||||||
|
import SequenceDiagram from './SequenceDiagram.mjs';
|
||||||
|
import {VirtualDocument} from '../core/documents/VirtualDocument.mjs';
|
||||||
|
import VirtualTextSizer from '../svg/VirtualTextSizer.mjs';
|
||||||
|
|
||||||
|
export {VirtualDocument};
|
||||||
|
|
||||||
|
export const virtualTextSizerFactory = () => new VirtualTextSizer();
|
||||||
|
|
||||||
|
export default class VirtualSequenceDiagram extends SequenceDiagram {
|
||||||
|
constructor(code = null, options = {}) {
|
||||||
|
const opts = {
|
||||||
|
document: new VirtualDocument(),
|
||||||
|
namespace: '',
|
||||||
|
textSizerFactory: virtualTextSizerFactory,
|
||||||
|
};
|
||||||
|
|
||||||
|
if(code && typeof code === 'object') {
|
||||||
|
Object.assign(opts, code);
|
||||||
|
} else {
|
||||||
|
Object.assign(opts, options, {code});
|
||||||
|
}
|
||||||
|
|
||||||
|
Object.assign(opts, {
|
||||||
|
container: null,
|
||||||
|
interactive: false,
|
||||||
|
});
|
||||||
|
|
||||||
|
super(opts);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function render(code, options = {}) {
|
||||||
|
return new VirtualSequenceDiagram(code, options).getSVGCodeSynchronous();
|
||||||
|
}
|
||||||
|
|
||||||
|
Object.assign(VirtualSequenceDiagram, {
|
||||||
|
Exporter: SequenceDiagram.Exporter,
|
||||||
|
Generator: SequenceDiagram.Generator,
|
||||||
|
Parser: SequenceDiagram.Parser,
|
||||||
|
Renderer: SequenceDiagram.Renderer,
|
||||||
|
addTheme: SequenceDiagram.addTheme,
|
||||||
|
getDefaultThemeNames: SequenceDiagram.getDefaultThemeNames,
|
||||||
|
render,
|
||||||
|
themes: SequenceDiagram.themes,
|
||||||
|
});
|
|
@ -1,39 +1,14 @@
|
||||||
|
import VirtualSequenceDiagram, {
|
||||||
|
VirtualDocument,
|
||||||
|
virtualTextSizerFactory,
|
||||||
|
} from './sequence/VirtualSequenceDiagram.mjs';
|
||||||
import SequenceDiagram from './sequence/SequenceDiagram.mjs';
|
import SequenceDiagram from './sequence/SequenceDiagram.mjs';
|
||||||
import {VirtualDocument} from './core/documents/VirtualDocument.mjs';
|
|
||||||
|
|
||||||
class UnitaryTextSizer {
|
|
||||||
// Simplified text sizer, which assumes all characters render as
|
|
||||||
// 1x1 px squares for repeatable renders in all browsers
|
|
||||||
|
|
||||||
baseline() {
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
measureHeight({formatted}) {
|
|
||||||
return formatted.length;
|
|
||||||
}
|
|
||||||
|
|
||||||
prepMeasurement(attrs, formatted) {
|
|
||||||
return formatted;
|
|
||||||
}
|
|
||||||
|
|
||||||
prepComplete() {
|
|
||||||
// No-op
|
|
||||||
}
|
|
||||||
|
|
||||||
performMeasurement(data) {
|
|
||||||
return data.reduce((total, part) => total + part.text.length, 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
teardown() {
|
|
||||||
// No-op
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const out = {
|
const out = {
|
||||||
SequenceDiagram,
|
SequenceDiagram,
|
||||||
VirtualDocument,
|
VirtualDocument,
|
||||||
headlessTextSizerFactory: () => new UnitaryTextSizer(),
|
VirtualSequenceDiagram,
|
||||||
|
virtualTextSizerFactory,
|
||||||
};
|
};
|
||||||
|
|
||||||
if(typeof exports !== 'undefined') {
|
if(typeof exports !== 'undefined') {
|
||||||
|
|
|
@ -0,0 +1,28 @@
|
||||||
|
export default class VirtualTextSizer {
|
||||||
|
// Simplified text sizer, which assumes all characters render as
|
||||||
|
// 1x1 px squares for repeatable renders in all browsers
|
||||||
|
|
||||||
|
baseline() {
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
measureHeight({formatted}) {
|
||||||
|
return formatted.length;
|
||||||
|
}
|
||||||
|
|
||||||
|
prepMeasurement(attrs, formatted) {
|
||||||
|
return formatted;
|
||||||
|
}
|
||||||
|
|
||||||
|
prepComplete() {
|
||||||
|
// No-op
|
||||||
|
}
|
||||||
|
|
||||||
|
performMeasurement(data) {
|
||||||
|
return data.reduce((total, part) => total + part.text.length, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
teardown() {
|
||||||
|
// No-op
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue