diff --git a/src/index.mjs b/src/index.mjs index 2d95c94..a4015fa 100644 --- a/src/index.mjs +++ b/src/index.mjs @@ -74,16 +74,51 @@ export const paperOptions = { } }; +const defaultSubcircuitButtons = [ + { + id: "zoomOut", + hidden: false, + buttonText: "–", + callback: ({circuit, model, paper}) => { + const newZoom = model.get('zoomLevel') - 1; + circuit.scaleAndRefreshPaper(paper, newZoom); + model.set("zoomLevel", newZoom); + } + }, + { + id: "zoomIn", + hidden: false, + buttonText: "+", + callback: ({circuit, model, paper}) => { + const newZoom = model.get('zoomLevel') + 1; + circuit.scaleAndRefreshPaper(paper, newZoom); + model.set("zoomLevel", newZoom); + } + } +]; + export class Circuit extends HeadlessCircuit { - constructor(data, { windowCallback = Circuit.prototype._defaultWindowCallback, layoutEngine = "elkjs", ...options } = {}) { + constructor(data, { windowCallback = Circuit.prototype._defaultWindowCallback, layoutEngine = "elkjs", subcircuitButtons = [], ...options } = {}) { if (!options.engine) options.engine = BrowserSynchEngine; super(data, options); this._layoutEngine = layoutEngine this._windowCallback = windowCallback; + this._subcircuitButtons = this._mergeSubcircuitButtons(subcircuitButtons); this.listenTo(this._engine, 'changeRunning', () => { this.trigger('changeRunning'); }); } + _mergeSubcircuitButtons(buttons = []) { + const res = new Map(); + for (const button of defaultSubcircuitButtons.concat(buttons)) { + if (button?.hidden) { + res.delete(button.id); + } else { + res.set(button.id, button); + } + } + return Array.from(res.values()); + } _defaultWindowCallback(type, div, closingCallback) { const maxWidth = () => $(window).width() * 0.9; const maxHeight = () => $(window).height() * 0.9; @@ -168,17 +203,13 @@ export class Circuit extends HeadlessCircuit { }); paper.unfreeze(); // subcircuit display + const circuit = this; this.listenTo(paper, 'open:subcircuit', (model) => { const subcircuitModal = $('