Skip to content

Commit

Permalink
AG-13782 - Apply all annotation options from options toolbar to subse…
Browse files Browse the repository at this point in the history
…quent fibonacci drawings
  • Loading branch information
manapeirov committed Jan 17, 2025
1 parent 9d88cc1 commit 587d5b7
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import {
type AnnotationOptionsColorPickerType,
AnnotationType,
type ChannelTextPosition,
type FibonacciAnnotationToolbarOptionsType,
type FibonacciAnnotationType,
type FibonacciBands,
type HasColorAnnotationType,
type HasFontSizeAnnotationType,
type HasLineStyleAnnotationType,
Expand All @@ -25,7 +25,7 @@ interface DefaultsMemento {
lineStyles: DefaultLineStyles;
lineTextAlignments: DefaultLineTextAlignments;
lineTextPositions: DefaultLineTextPositions;
fibonacciBands: DefaultFibonacciBands;
fibonacciOptions: DefaultFibonacciOptions;
}

type DefaultColors = Map<
Expand All @@ -36,7 +36,7 @@ type DefaultFontSizes = Map<HasFontSizeAnnotationType, number | undefined>;
type DefaultLineStyles = Map<HasLineStyleAnnotationType, AnnotationLineStyle | undefined>;
type DefaultLineTextAlignments = Map<HasLineTextAnnotationType, LineTextAlignment | undefined>;
type DefaultLineTextPositions = Map<HasLineTextAnnotationType, LineTextPosition | ChannelTextPosition | undefined>;
type DefaultFibonacciBands = Map<FibonacciAnnotationType, FibonacciBands | undefined>;
type DefaultFibonacciOptions = Map<FibonacciAnnotationType, FibonacciAnnotationToolbarOptionsType>;

export class AnnotationDefaults implements _ModuleSupport.MementoOriginator<DefaultsMemento> {
mementoOriginatorKey = 'annotation-defaults' as const;
Expand Down Expand Up @@ -101,7 +101,24 @@ export class AnnotationDefaults implements _ModuleSupport.MementoOriginator<Defa
[AnnotationType.DatePriceRange, undefined],
]);

private fibonacciBands: DefaultFibonacciBands = new Map([[AnnotationType.FibonacciRetracement, undefined]]);
private fibonacciOptions: DefaultFibonacciOptions = new Map([
[
AnnotationType.FibonacciRetracement,
{
bands: undefined,
reverse: undefined,
showFill: undefined,
},
],
[
AnnotationType.FibonacciRetracementTrendBased,
{
bands: undefined,
reverse: undefined,
showFill: undefined,
},
],
]);

createMemento() {
return {
Expand All @@ -110,7 +127,7 @@ export class AnnotationDefaults implements _ModuleSupport.MementoOriginator<Defa
lineStyles: deepClone(this.lineStyles),
lineTextAlignments: deepClone(this.lineTextAlignments),
lineTextPositions: deepClone(this.lineTextPositions),
fibonacciBands: deepClone(this.fibonacciBands),
fibonacciOptions: deepClone(this.fibonacciOptions),
};
}

Expand All @@ -124,7 +141,7 @@ export class AnnotationDefaults implements _ModuleSupport.MementoOriginator<Defa
this.lineStyles = deepClone(blob.lineStyles);
this.lineTextAlignments = deepClone(blob.lineTextAlignments);
this.lineTextPositions = deepClone(blob.lineTextPositions);
this.fibonacciBands = deepClone(blob.fibonacciBands);
this.fibonacciOptions = deepClone(blob.fibonacciOptions);
}

setDefaultColor(
Expand Down Expand Up @@ -168,8 +185,17 @@ export class AnnotationDefaults implements _ModuleSupport.MementoOriginator<Defa
this.lineTextPositions.set(type, position);
}

setDefaultFibonacciBands(type: FibonacciAnnotationType | HasLineStyleAnnotationType, bands: FibonacciBands) {
if (type == AnnotationType.FibonacciRetracement) this.fibonacciBands.set(type, bands);
setDefaultFibonacciOptions<K extends keyof FibonacciAnnotationToolbarOptionsType>(
type: FibonacciAnnotationType | HasLineStyleAnnotationType,
key: K,
value: FibonacciAnnotationToolbarOptionsType[K]
) {
if (type != AnnotationType.FibonacciRetracement && type != AnnotationType.FibonacciRetracementTrendBased)
return;

const options = this.fibonacciOptions.get(type)!;
options[key] = value;
this.fibonacciOptions.set(type, options);
}

applyDefaults(datum: AnnotationProperties) {
Expand Down Expand Up @@ -203,9 +229,15 @@ export class AnnotationDefaults implements _ModuleSupport.MementoOriginator<Defa
datum.text.alignment = alignment;
}

for (const [annotationType, bands] of this.fibonacciBands) {
if (datum.type !== annotationType || bands == null) continue;
datum.bands = bands;
for (const [annotationType, options] of this.fibonacciOptions) {
if (datum.type !== annotationType || options == null) continue;

Object.entries(options).forEach(([option, value]) => {
if (value == null) {
return;
}
datum.set({ [option]: value });
});
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ type MeasurerAnnotationType =

type EphemeralAnnotationType = AnnotationType.QuickDatePriceRange;

export type FibonacciAnnotationType = AnnotationType.FibonacciRetracement;
export type FibonacciAnnotationType =
| AnnotationType.FibonacciRetracement
| AnnotationType.FibonacciRetracementTrendBased;

export type HasColorAnnotationType = AnnotationType;
export type HasLineStyleAnnotationType = Exclude<
Expand Down Expand Up @@ -131,6 +133,8 @@ export type AnnotationContext = {

export type AnnotationOptionsColorPickerType = 'line-color' | 'fill-color' | 'text-color';

export type FibonacciAnnotationToolbarOptionsType = { bands?: FibonacciBands; reverse?: boolean; showFill?: boolean };

export type AnnotationLineStyle = {
type?: AgAnnotationLineStyleType;
strokeWidth?: number;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,12 @@ export class Annotations extends _ModuleSupport.BaseModuleInstance implements _M
sourceEvent,
onChangeLine: (props) => {
this.state.transition('lineProps', props);
if (props.bands) this.defaults.setDefaultFibonacciBands(datum.type, props.bands);
if (props.bands != null)
this.defaults.setDefaultFibonacciOptions(datum.type, 'bands', props.bands);
if (props.reverse != null)
this.defaults.setDefaultFibonacciOptions(datum.type, 'reverse', props.reverse);
if (props.showFill != null)
this.defaults.setDefaultFibonacciOptions(datum.type, 'showFill', props.showFill);
},
onChangeText: (props) => {
this.state.transition('lineText', props);
Expand Down

0 comments on commit 587d5b7

Please sign in to comment.