Skip to content

Commit

Permalink
Improvements with addModifier() and addToModifierContext().
Browse files Browse the repository at this point in the history
Remove duplicate implementations and add comments to overrides.
  • Loading branch information
ronyeh committed Aug 29, 2021
1 parent fcaa0d2 commit 44fb8c0
Show file tree
Hide file tree
Showing 10 changed files with 68 additions and 96 deletions.
9 changes: 6 additions & 3 deletions src/barnote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import { log } from './util';
import { Note } from './note';
import { Barline, BarlineType } from './stavebarline';
import { ModifierContext } from 'modifiercontext';

// eslint-disable-next-line
function L(...args: any[]) {
Expand Down Expand Up @@ -62,12 +63,14 @@ export class BarNote extends Note {
return this;
}

/** overridden to ignore. */
addToModifierContext(): this {
/* Overridden to ignore */
// eslint-disable-next-line
addToModifierContext(mc: ModifierContext): this {
// DO NOTHING.
return this;
}

/** overridden to ignore. */
/** Overridden to ignore. */
preFormat(): this {
this.setPreFormatted(true);
return this;
Expand Down
8 changes: 5 additions & 3 deletions src/clefnote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { Glyph } from './glyph';
import { RenderContext } from './types/common';
import { BoundingBox } from './boundingbox';
import { ClefType } from './clef';
import { ModifierContext } from 'modifiercontext';

/** ClefNote implements clef annotations in measures. */
export class ClefNote extends Note {
Expand Down Expand Up @@ -60,9 +61,10 @@ export class ClefNote extends Note {
return super.getBoundingBox();
}

/** Overridden to ignore. */
addToModifierContext(): this {
/* overridden to ignore */
/* Overridden to ignore */
// eslint-disable-next-line
addToModifierContext(mc: ModifierContext): this {
// DO NOTHING.
return this;
}

Expand Down
8 changes: 6 additions & 2 deletions src/ghostnote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { RuntimeError } from './util';
import { StemmableNote } from './stemmablenote';
import { Stave } from './stave';
import { NoteStruct } from './note';
import { ModifierContext } from 'modifiercontext';

export class GhostNote extends StemmableNote {
/** @constructor */
Expand Down Expand Up @@ -45,8 +46,11 @@ export class GhostNote extends StemmableNote {
return this;
}

addToModifierContext(): this {
/* intentionally overridden */ return this;
/* Overridden to ignore */
// eslint-disable-next-line
addToModifierContext(mc: ModifierContext): this {
// DO NOTHING.
return this;
}

preFormat(): this {
Expand Down
12 changes: 0 additions & 12 deletions src/glyphnote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import { BoundingBox } from './boundingbox';
import { Glyph } from './glyph';
import { Note, NoteStruct } from './note';
import { ModifierContext } from './modifiercontext';

export interface GlyphNoteOptions {
ignoreTicks?: boolean;
Expand Down Expand Up @@ -39,17 +38,6 @@ export class GlyphNote extends Note {
return this.glyph.getBoundingBox();
}

// Add self to modifier context. `mContext` is the `ModifierContext`
// to be added to.
addToModifierContext(mContext: ModifierContext): this {
this.modifierContext = mContext;
for (let i = 0; i < this.modifiers.length; ++i) {
this.modifierContext.addMember(this.modifiers[i]);
}
this.setPreFormatted(false);
return this;
}

preFormat(): this {
if (!this.preFormatted && this.modifierContext) {
this.modifierContext.preFormat();
Expand Down
7 changes: 5 additions & 2 deletions src/keysignote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import { Note } from './note';
import { KeySignature } from './keysignature';
import { ModifierContext } from 'modifiercontext';

export class KeySigNote extends Note {
protected keySignature: KeySignature;
Expand All @@ -17,8 +18,10 @@ export class KeySigNote extends Note {
this.ignore_ticks = true;
}

addToModifierContext(): this {
/* overridden to ignore */
/* Overridden to ignore */
// eslint-disable-next-line
addToModifierContext(mc: ModifierContext): this {
// DO NOTHING.
return this;
}

Expand Down
14 changes: 7 additions & 7 deletions src/modifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export class Modifier extends Element {
protected x_shift: number;

private spacingFromNextModifier: number;
private modifier_context?: ModifierContext;
private modifierContext?: ModifierContext;

/** Modifiers category string. */
static get CATEGORY(): string {
Expand Down Expand Up @@ -145,18 +145,18 @@ export class Modifier extends Element {

/** Get `ModifierContext`. */
getModifierContext(): ModifierContext | undefined {
return this.modifier_context;
return this.modifierContext;
}

/** Check and get `ModifierContext`. */
checkModifierContext(): ModifierContext {
if (!this.modifier_context) throw new RuntimeError('NoModifierContext', 'Modifier Context Required');
return this.modifier_context;
if (!this.modifierContext) throw new RuntimeError('NoModifierContext', 'Modifier Context Required');
return this.modifierContext;
}

/** Every modifier must be part of a `ModifierContext`. */
setModifierContext(c: ModifierContext): this {
this.modifier_context = c;
this.modifierContext = c;
return this;
}

Expand Down Expand Up @@ -211,15 +211,15 @@ export class Modifier extends Element {
return this;
}

/** Get shitf modifier `x` */
/** Get shift modifier `x` */
getXShift(): number {
return this.x_shift;
}

/** Render the modifier onto the canvas. */
draw(): void {
this.checkContext();
throw new RuntimeError('MethodNotImplemented', 'draw() not implemented for this modifier.');
throw new RuntimeError('NotImplemented', 'draw() not implemented for this modifier.');
}

// aligns sub notes of NoteSubGroup (or GraceNoteGroup) to the main note with correct x-offset
Expand Down
3 changes: 2 additions & 1 deletion src/modifiercontext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { Vibrato } from './vibrato';
import { Modifier } from './modifier';
import { TabNote } from './tabnote';
import { Note } from './note';
import { Tickable } from 'tickable';

export interface ModifierContextState {
right_shift: number;
Expand All @@ -36,7 +37,7 @@ export interface ModifierContextMetrics {
spacing: number;
}

export type ModifierContextMember = Modifier | StaveNote | TabNote;
export type ModifierContextMember = Tickable | Modifier | StaveNote | TabNote;

// To enable logging for this class. Set `Vex.Flow.ModifierContext.DEBUG` to `true`.
// eslint-disable-next-line
Expand Down
63 changes: 8 additions & 55 deletions src/stavenote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ import { Modifier } from './modifier';
import { Dot } from './dot';
import { KeyProps } from './types/common';
import { Beam } from './beam';
import { ModifierContext } from './modifiercontext';
import { ElementStyle } from './element';
import { Stave } from './stave';
import { Note, NoteStruct } from './note';
import { ModifierContextState } from './modifiercontext';
import { Accidental } from 'accidental';

export interface StaveNoteHeadBounds {
y_top: number;
Expand Down Expand Up @@ -306,19 +306,14 @@ export class StaveNote extends StemmableNote {
}

static formatByY(notes: StaveNote[], state: ModifierContextState): void {
// NOTE: this function does not support more than two voices per stave
// use with care.
let hasStave = true;
// NOTE: this function does not support more than two voices per stave. Use with care.

let hasStave = true;
for (let i = 0; i < notes.length; i++) {
hasStave = hasStave && notes[i].getStave() != undefined;
}

if (!hasStave) {
throw new RuntimeError(
'Stave Missing',
'All notes must have a stave - Vex.Flow.ModifierContext.formatMultiVoice!'
);
throw new RuntimeError('NoStave', 'All notes must have a stave.');
}

let xShift = 0;
Expand Down Expand Up @@ -874,48 +869,6 @@ export class StaveNote extends StemmableNote {
return this.keyProps[index].line;
}

// Add self to modifier context. `mContext` is the `ModifierContext`
// to be added to.
addToModifierContext(mContext: ModifierContext): this {
this.modifierContext = mContext;
for (let i = 0; i < this.modifiers.length; ++i) {
this.modifierContext.addMember(this.modifiers[i]);
}
this.modifierContext.addMember(this);
this.setPreFormatted(false);
return this;
}

// Generic function to add modifiers to a note
//
// Parameters:
// * `index`: The index of the key that we're modifying
// * `modifier`: The modifier to add
addModifier(a: number | Modifier, b: number | Modifier): this {
let index: number;
let modifier: Modifier;

if (typeof a === 'object' && typeof b === 'number') {
index = b;
modifier = a;
} else if (typeof a === 'number' && typeof b === 'object') {
// eslint-disable-next-line
console.warn('deprecated call signature to addModifier, use addModifier(modifier, index) instead');
index = a;
modifier = b;
} else {
throw new RuntimeError(
'WrongParams',
'Call signature to addModifier not supported, use addModifier(modifier, index) instead.'
);
}
modifier.setNote(this);
modifier.setIndex(index);
this.modifiers.push(modifier);
this.setPreFormatted(false);
return this;
}

// Helper function to add an accidental to a key
addAccidental(index: number, accidental: Modifier): this {
return this.addModifier(accidental, index);
Expand Down Expand Up @@ -948,17 +901,17 @@ export class StaveNote extends StemmableNote {
}

// Get all accidentals in the `ModifierContext`
getAccidentals(): Modifier[] {
getAccidentals(): Accidental[] {
if (!this.modifierContext)
throw new RuntimeError('NoModifierContext', 'No modifier context attached to this note.');
return this.modifierContext.getMembers('accidentals') as Modifier[];
return this.modifierContext.getMembers('accidentals') as Accidental[];
}

// Get all dots in the `ModifierContext`
getDots(): Modifier[] {
getDots(): Dot[] {
if (!this.modifierContext)
throw new RuntimeError('NoModifierContext', 'No modifier context attached to this note.');
return this.modifierContext.getMembers('dots') as Modifier[];
return this.modifierContext.getMembers('dots') as Dot[];
}

// Get the width of the note if it is displaced. Used for `Voice`
Expand Down
33 changes: 24 additions & 9 deletions src/tickable.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
// [VexFlow](http://vexflow.com) - Copyright (c) Mohit Muthanna 2010.
// MIT License

import { RuntimeError } from './util';
import { Stave } from './stave';
import { Element } from './element';
import { Flow } from './flow';
import { Fraction } from './fraction';
import { TickContext } from './tickcontext';
import { Modifier } from './modifier';
import { ModifierContext } from './modifiercontext';
import { TickContext } from './tickcontext';
import { Tuplet } from './tuplet';
import { RuntimeError } from './util';
import { Voice } from './voice';
import { Modifier } from './modifier';

/** Formatter metrics interface */
export interface FormatterMetrics {
Expand Down Expand Up @@ -253,17 +254,26 @@ export abstract class Tickable extends Element {
return this;
}

/** Optional, if tickable has modifiers, set modifierContext. */
addToModifierContext(mc: ModifierContext): void {
/**
* Add self to the provided ModifierContext `mc`.
* If this tickable has modifiers, set modifierContext.
* @returns this
*/
addToModifierContext(mc: ModifierContext): this {
this.modifierContext = mc;
// Add modifiers to modifier context (if any)
this.preFormatted = false;
for (let i = 0; i < this.modifiers.length; ++i) {
this.modifierContext.addMember(this.modifiers[i]);
}
this.modifierContext.addMember(this);
this.setPreFormatted(false);
return this;
}

/** Optional, if tickable has modifiers, associate a Modifier. */
addModifier(mod: Modifier): this {
// eslint-disable-next-line
addModifier(mod: Modifier, ...optionalArgs: any[]): this {
this.modifiers.push(mod);
this.preFormatted = false;
this.setPreFormatted(false);
return this;
}

Expand All @@ -289,6 +299,11 @@ export abstract class Tickable extends Element {
}
}

/** Set preformatted status. */
setPreFormatted(value: boolean): void {
this.preFormatted = value;
}

/** Postformat the Tickable. */
postFormat(): this {
if (this.postFormatted) return this;
Expand Down
7 changes: 5 additions & 2 deletions src/timesignote.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// [VexFlow](http://vexflow.com) - Copyright (c) Mohit Muthanna 2010.
// Author Taehoon Moon 2014

import { ModifierContext } from 'modifiercontext';
import { Note } from './note';
import { TimeSignature, TimeSignatureInfo } from './timesignature';

Expand All @@ -19,8 +20,10 @@ export class TimeSigNote extends Note {
this.ignore_ticks = true;
}

addToModifierContext(): this {
/* overridden to ignore */
/* Overridden to ignore */
// eslint-disable-next-line
addToModifierContext(mc: ModifierContext): this {
// DO NOTHING.
return this;
}

Expand Down

0 comments on commit 44fb8c0

Please sign in to comment.