Skip to content

Commit

Permalink
Refactor color plugin arguments, #466
Browse files Browse the repository at this point in the history
  • Loading branch information
cocopon committed Feb 3, 2023
1 parent 92ed361 commit d528796
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 56 deletions.
27 changes: 15 additions & 12 deletions packages/core/src/input-binding/color/plugin-number.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,17 @@ function isForColor(params: Record<string, unknown>): boolean {
return false;
}

interface NumberColorInputParams extends ColorInputParams {
supportsAlpha: boolean;
}

/**
* @hidden
*/
export const NumberColorInputPlugin: InputBindingPlugin<
IntColor,
number,
ColorInputParams
NumberColorInputParams
> = {
id: 'input-color-number',
type: 'input',
Expand All @@ -60,33 +64,32 @@ export const NumberColorInputPlugin: InputBindingPlugin<
return result
? {
initialValue: value,
params: result,
params: {
...result,
supportsAlpha: shouldSupportAlpha(params),
},
}
: null;
},
binding: {
reader: (args) => {
return shouldSupportAlpha(args.params)
return args.params.supportsAlpha
? colorFromRgbaNumber
: colorFromRgbNumber;
},
equals: equalsColor,
writer: (args) => {
return createColorNumberWriter(shouldSupportAlpha(args.params));
return createColorNumberWriter(args.params.supportsAlpha);
},
},
controller: (args) => {
const supportsAlpha = shouldSupportAlpha(args.params);
const expanded =
'expanded' in args.params ? args.params.expanded : undefined;
const picker = 'picker' in args.params ? args.params.picker : undefined;
return new ColorController(args.document, {
colorType: 'int',
expanded: expanded ?? false,
formatter: createFormatter(supportsAlpha),
expanded: args.params.expanded ?? false,
formatter: createFormatter(args.params.supportsAlpha),
parser: createColorStringParser('int'),
pickerLayout: picker ?? 'popup',
supportsAlpha: supportsAlpha,
pickerLayout: args.params.picker ?? 'popup',
supportsAlpha: args.params.supportsAlpha,
value: args.value,
viewProps: args.viewProps,
});
Expand Down
31 changes: 18 additions & 13 deletions packages/core/src/input-binding/color/plugin-object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,17 @@ function createColorObjectFormatter(
};
}

interface ObjectColorInputParams extends ColorInputParams {
colorType: ColorType;
}

/**
* @hidden
*/
export const ObjectColorInputPlugin: InputBindingPlugin<
IntColor,
RgbColorObject | RgbaColorObject,
ColorInputParams
ObjectColorInputParams
> = {
id: 'input-color-object',
type: 'input',
Expand All @@ -71,32 +75,33 @@ export const ObjectColorInputPlugin: InputBindingPlugin<
return result
? {
initialValue: value,
params: result,
params: {
...result,
colorType: extractColorType(params) ?? 'int',
},
}
: null;
},
binding: {
reader: (args) =>
createColorObjectBindingReader(extractColorType(args.params) ?? 'int'),
reader: (args) => createColorObjectBindingReader(args.params.colorType),
equals: equalsColor,
writer: (args) =>
createColorObjectWriter(
shouldSupportAlpha(args.initialValue),
extractColorType(args.params) ?? 'int',
args.params.colorType,
),
},
controller: (args) => {
const supportsAlpha = isRgbaColorObject(args.initialValue);
const expanded =
'expanded' in args.params ? args.params.expanded : undefined;
const picker = 'picker' in args.params ? args.params.picker : undefined;
const type = extractColorType(args.params) ?? 'int';
return new ColorController(args.document, {
colorType: type,
expanded: expanded ?? false,
formatter: createColorObjectFormatter(supportsAlpha, type),
colorType: args.params.colorType,
expanded: args.params.expanded ?? false,
formatter: createColorObjectFormatter(
supportsAlpha,
args.params.colorType,
),
parser: createColorStringParser('int'),
pickerLayout: picker ?? 'popup',
pickerLayout: args.params.picker ?? 'popup',
supportsAlpha: supportsAlpha,
value: args.value,
viewProps: args.viewProps,
Expand Down
51 changes: 20 additions & 31 deletions packages/core/src/input-binding/color/plugin-string.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import {Formatter} from '../../common/converter/formatter';
import {TpError} from '../../common/tp-error';
import {InputBindingPlugin} from '../plugin';
import {ColorController} from './controller/color';
Expand All @@ -6,23 +7,29 @@ import {
detectStringColorFormat,
findColorStringifier,
readIntColorString,
StringColorFormat,
} from './converter/color-string';
import {createColorStringWriter} from './converter/writer';
import {equalsColor} from './model/color';
import {Color, equalsColor} from './model/color';
import {IntColor} from './model/int-color';
import {
ColorInputParams,
extractColorType,
parseColorInputParams,
} from './util';

interface StringColorInputParams extends ColorInputParams {
format: StringColorFormat;
stringifier: Formatter<Color>;
}

/**
* @hidden
*/
export const StringColorInputPlugin: InputBindingPlugin<
IntColor,
string,
ColorInputParams
StringColorInputParams
> = {
id: 'input-color-string',
type: 'input',
Expand All @@ -46,51 +53,33 @@ export const StringColorInputPlugin: InputBindingPlugin<
return result
? {
initialValue: value,
params: result,
params: {
...result,
format: format,
stringifier: stringifier,
},
}
: null;
},
binding: {
reader: () => readIntColorString,
equals: equalsColor,
writer: (args) => {
const format = detectStringColorFormat(
args.initialValue,
extractColorType(args.params),
);
if (!format) {
throw TpError.shouldNeverHappen();
}
const writer = createColorStringWriter(format);
const writer = createColorStringWriter(args.params.format);
if (!writer) {
throw TpError.notBindable();
}
return writer;
},
},
controller: (args) => {
const format = detectStringColorFormat(
args.initialValue,
extractColorType(args.params),
);
if (!format) {
throw TpError.shouldNeverHappen();
}
const stringifier = findColorStringifier(format);
if (!stringifier) {
throw TpError.shouldNeverHappen();
}

const expanded =
'expanded' in args.params ? args.params.expanded : undefined;
const picker = 'picker' in args.params ? args.params.picker : undefined;
return new ColorController(args.document, {
colorType: format.type,
expanded: expanded ?? false,
formatter: stringifier,
colorType: args.params.format.type,
expanded: args.params.expanded ?? false,
formatter: args.params.stringifier,
parser: createColorStringParser('int'),
pickerLayout: picker ?? 'popup',
supportsAlpha: format.alpha,
pickerLayout: args.params.picker ?? 'popup',
supportsAlpha: args.params.format.alpha,
value: args.value,
viewProps: args.viewProps,
});
Expand Down

0 comments on commit d528796

Please sign in to comment.