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,4 +1,3 @@
import { clamp } from '@blocksuite/affine-shared/utils';
import {
createPropertyConvert,
getTagColor,
@@ -6,6 +5,7 @@ import {
} from '@blocksuite/data-view';
import { presetPropertyConverts } from '@blocksuite/data-view/property-presets';
import { propertyModelPresets } from '@blocksuite/data-view/property-pure-presets';
import { clamp } from '@blocksuite/global/utils';
import { nanoid, Text } from '@blocksuite/store';
import { richTextPropertyModelConfig } from './rich-text/define.js';
@@ -1,9 +1,10 @@
import { LINE_WIDTHS, LineWidth } from '@blocksuite/affine-model';
import { clamp, on, once } from '@blocksuite/affine-shared/utils';
import { on, once } from '@blocksuite/affine-shared/utils';
import { WithDisposable } from '@blocksuite/global/utils';
import { css, html, LitElement, nothing, type PropertyValues } from 'lit';
import { property, query } from 'lit/decorators.js';
import { repeat } from 'lit/directives/repeat.js';
import clamp from 'lodash-es/clamp';
interface Config {
width: number;
@@ -1,7 +1,8 @@
import { clamp, stopPropagation } from '@blocksuite/affine-shared/utils';
import { stopPropagation } from '@blocksuite/affine-shared/utils';
import { css, html, LitElement } from 'lit';
import { property } from 'lit/decorators.js';
import { repeat } from 'lit/directives/repeat.js';
import clamp from 'lodash-es/clamp';
const MIN_SCALE = 0;
const MAX_SCALE = 400;
@@ -1,8 +1,9 @@
import { clamp, stopPropagation } from '@blocksuite/affine-shared/utils';
import { stopPropagation } from '@blocksuite/affine-shared/utils';
import { DoneIcon } from '@blocksuite/icons/lit';
import { css, html, LitElement, nothing } from 'lit';
import { property } from 'lit/decorators.js';
import { repeat } from 'lit/directives/repeat.js';
import clamp from 'lodash-es/clamp';
const MIN_SIZE = 1;
const MAX_SIZE = 200;
@@ -35,7 +35,6 @@ import {
import { EMBED_CARD_HEIGHT } from '@blocksuite/affine-shared/consts';
import { unsafeCSSVarV2 } from '@blocksuite/affine-shared/theme';
import {
clamp,
getElementsWithoutGroup,
getSelectedRect,
requestThrottledConnectedFrame,
@@ -68,6 +67,7 @@ import { css, html, nothing } from 'lit';
import { state } from 'lit/decorators.js';
import { ifDefined } from 'lit/directives/if-defined.js';
import { styleMap } from 'lit/directives/style-map.js';
import clamp from 'lodash-es/clamp';
import type { EdgelessRootBlockComponent } from '../../edgeless-root-block.js';
import {
@@ -14,7 +14,6 @@ import {
MindmapElementModel,
RootBlockSchema,
} from '@blocksuite/affine-model';
import { clamp } from '@blocksuite/affine-shared/utils';
import type { BlockStdScope } from '@blocksuite/block-std';
import type {
GfxController,
@@ -35,6 +34,7 @@ import {
import { BlockSuiteError, ErrorCode } from '@blocksuite/global/exceptions';
import { Bound, getCommonBound } from '@blocksuite/global/utils';
import { effect } from '@preact/signals-core';
import clamp from 'lodash-es/clamp';
import { RootService } from '../root-service.js';
import { TemplateJob } from './services/template.js';
@@ -27,7 +27,6 @@ import {
TelemetryProvider,
} from '@blocksuite/affine-shared/services';
import {
clamp,
handleNativeRangeAtPoint,
resetNativeSelection,
} from '@blocksuite/affine-shared/utils';
@@ -52,6 +51,7 @@ import {
Vec,
} from '@blocksuite/global/utils';
import { effect } from '@preact/signals-core';
import clamp from 'lodash-es/clamp';
import type { EdgelessRootBlockComponent } from '../edgeless-root-block.js';
import { prepareCloneData } from '../utils/clone-utils.js';
@@ -1,4 +1,4 @@
import { clamp } from '@blocksuite/affine-shared/utils';
import clamp from 'lodash-es/clamp';
type CollisionBox = {
/**
@@ -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);
}
@@ -1,4 +1,4 @@
import { clamp } from '@blocksuite/affine-shared/utils';
import { clamp } from '@blocksuite/global/utils';
import { createPropertyConvert } from '../core/index.js';
import { multiSelectPropertyModelConfig } from './multi-select/define.js';
@@ -1,11 +1,10 @@
import { NoteBlockModel, RootBlockModel } from '@blocksuite/affine-model';
import { BLOCK_ID_ATTR, type BlockComponent } from '@blocksuite/block-std';
import { SurfaceBlockModel } from '@blocksuite/block-std/gfx';
import type { Point, Rect } from '@blocksuite/global/utils';
import { clamp, type Point, type Rect } from '@blocksuite/global/utils';
import type { BlockModel } from '@blocksuite/store';
import { BLOCK_CHILDREN_CONTAINER_PADDING_LEFT } from '../../consts/index.js';
import { clamp } from '../math.js';
import { matchModels } from '../model/checker.js';
const ATTR_SELECTOR = `[${BLOCK_ID_ATTR}]`;
@@ -2,12 +2,6 @@ export function almostEqual(a: number, b: number, epsilon = 0.0001) {
return Math.abs(a - b) < epsilon;
}
export function clamp(value: number, min: number, max: number): number {
if (value < min) return min;
if (value > max) return max;
return value;
}
export function rangeWrap(n: number, min: number, max: number) {
max = max - min;
n = (n - min + max) % max;
@@ -1,3 +1,5 @@
import { clamp } from '@blocksuite/global/utils';
export const BLOCK_CHILDREN_CONTAINER_PADDING_LEFT = 24;
export const NOTE_MIN_WIDTH = 450 + 24 * 2;
export const NOTE_MIN_HEIGHT = 92;
@@ -11,9 +13,6 @@ export enum NoteDisplayMode {
EdgelessOnly = 'edgeless',
}
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);