chore(editor): merge clamp functions (#10577)

Closes: [BS-2625](https://linear.app/affine-design/issue/BS-2625/合并clamp函数)
This commit is contained in:
Saul-Mirone
2025-03-03 10:33:38 +00:00
parent 899a957fab
commit 4c3c953a36
16 changed files with 21 additions and 34 deletions
@@ -1,6 +1,6 @@
import type { Color } from '@blocksuite/affine-model';
import { on, once, stopPropagation } from '@blocksuite/affine-shared/utils';
import { SignalWatcher, WithDisposable } from '@blocksuite/global/utils';
import { clamp, SignalWatcher, WithDisposable } from '@blocksuite/global/utils';
import { batch, computed, signal } from '@preact/signals-core';
import { html, LitElement } from 'lit';
import { property, query } from 'lit/decorators.js';
@@ -24,7 +24,6 @@ import type {
} from './types.js';
import {
bound01,
clamp,
defaultHsva,
eq,
hsvaToHex8,
@@ -55,7 +54,7 @@ export class EdgelessColorPicker extends SignalWatcher(
const orignalValue = target.value;
let value = orignalValue.trim().replace(/[^0-9]/, '');
const alpha = clamp(0, Number(value), 100);
const alpha = clamp(Number(value), 0, 100);
const a = bound01(alpha, 100);
const hsva = this.hsva$.peek();
@@ -1,6 +1,7 @@
// https://www.w3.org/TR/css-color-4/
import type { Color, ColorScheme } from '@blocksuite/affine-model';
import { clamp } from '@blocksuite/global/utils';
import { COLORS, FIRST_COLOR } from './consts.js';
import type {
@@ -52,9 +53,6 @@ export function linearGradientAt(t: number): Rgb {
const lerp = (a: number, b: number, t: number) => a + t * (b - a);
export const clamp = (min: number, val: number, max: number) =>
Math.min(Math.max(min, val), max);
export const bound01 = (n: number, max: number) => {
n = clamp(0, n, max);
@@ -1,4 +1,4 @@
import { WithDisposable } from '@blocksuite/global/utils';
import { clamp, WithDisposable } from '@blocksuite/global/utils';
import { isSameDay, isSameMonth, isToday } from 'date-fns';
import {
html,
@@ -13,7 +13,7 @@ import { styleMap } from 'lit/directives/style-map.js';
import { arrowLeftIcon } from './icons.js';
import { datePickerStyle } from './style.js';
import { clamp, getMonthMatrix, toDate } from './utils.js';
import { getMonthMatrix, toDate } from './utils.js';
const days = ['Su', 'Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa'];
const months = [
@@ -522,7 +522,7 @@ export class DatePicker extends WithDisposable(LitElement) {
}
openYearSelector() {
this._yearCursor = clamp(this._minYear, this._maxYear, this.year);
this._yearCursor = clamp(this.year, this._minYear, this._maxYear);
this._mode = 'year';
this._getYearMatrix();
}
@@ -66,8 +66,3 @@ export function getMonthMatrix(maybeDate: MaybeDate) {
}
return matrix;
}
export function clamp(num1: number, num2: number, value: number) {
const [min, max] = [num1, num2].sort((a, b) => a - b);
return Math.min(Math.max(value, min), max);
}