chore(editor): merge clamp functions (#10577)
Closes: [BS-2625](https://linear.app/affine-design/issue/BS-2625/合并clamp函数)
This commit is contained in:
@@ -1,4 +1,3 @@
|
|||||||
import { clamp } from '@blocksuite/affine-shared/utils';
|
|
||||||
import {
|
import {
|
||||||
createPropertyConvert,
|
createPropertyConvert,
|
||||||
getTagColor,
|
getTagColor,
|
||||||
@@ -6,6 +5,7 @@ import {
|
|||||||
} from '@blocksuite/data-view';
|
} from '@blocksuite/data-view';
|
||||||
import { presetPropertyConverts } from '@blocksuite/data-view/property-presets';
|
import { presetPropertyConverts } from '@blocksuite/data-view/property-presets';
|
||||||
import { propertyModelPresets } from '@blocksuite/data-view/property-pure-presets';
|
import { propertyModelPresets } from '@blocksuite/data-view/property-pure-presets';
|
||||||
|
import { clamp } from '@blocksuite/global/utils';
|
||||||
import { nanoid, Text } from '@blocksuite/store';
|
import { nanoid, Text } from '@blocksuite/store';
|
||||||
|
|
||||||
import { richTextPropertyModelConfig } from './rich-text/define.js';
|
import { richTextPropertyModelConfig } from './rich-text/define.js';
|
||||||
|
|||||||
@@ -1,9 +1,10 @@
|
|||||||
import { LINE_WIDTHS, LineWidth } from '@blocksuite/affine-model';
|
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 { WithDisposable } from '@blocksuite/global/utils';
|
||||||
import { css, html, LitElement, nothing, type PropertyValues } from 'lit';
|
import { css, html, LitElement, nothing, type PropertyValues } from 'lit';
|
||||||
import { property, query } from 'lit/decorators.js';
|
import { property, query } from 'lit/decorators.js';
|
||||||
import { repeat } from 'lit/directives/repeat.js';
|
import { repeat } from 'lit/directives/repeat.js';
|
||||||
|
import clamp from 'lodash-es/clamp';
|
||||||
|
|
||||||
interface Config {
|
interface Config {
|
||||||
width: number;
|
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 { css, html, LitElement } from 'lit';
|
||||||
import { property } from 'lit/decorators.js';
|
import { property } from 'lit/decorators.js';
|
||||||
import { repeat } from 'lit/directives/repeat.js';
|
import { repeat } from 'lit/directives/repeat.js';
|
||||||
|
import clamp from 'lodash-es/clamp';
|
||||||
|
|
||||||
const MIN_SCALE = 0;
|
const MIN_SCALE = 0;
|
||||||
const MAX_SCALE = 400;
|
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 { DoneIcon } from '@blocksuite/icons/lit';
|
||||||
import { css, html, LitElement, nothing } from 'lit';
|
import { css, html, LitElement, nothing } from 'lit';
|
||||||
import { property } from 'lit/decorators.js';
|
import { property } from 'lit/decorators.js';
|
||||||
import { repeat } from 'lit/directives/repeat.js';
|
import { repeat } from 'lit/directives/repeat.js';
|
||||||
|
import clamp from 'lodash-es/clamp';
|
||||||
|
|
||||||
const MIN_SIZE = 1;
|
const MIN_SIZE = 1;
|
||||||
const MAX_SIZE = 200;
|
const MAX_SIZE = 200;
|
||||||
|
|||||||
@@ -35,7 +35,6 @@ import {
|
|||||||
import { EMBED_CARD_HEIGHT } from '@blocksuite/affine-shared/consts';
|
import { EMBED_CARD_HEIGHT } from '@blocksuite/affine-shared/consts';
|
||||||
import { unsafeCSSVarV2 } from '@blocksuite/affine-shared/theme';
|
import { unsafeCSSVarV2 } from '@blocksuite/affine-shared/theme';
|
||||||
import {
|
import {
|
||||||
clamp,
|
|
||||||
getElementsWithoutGroup,
|
getElementsWithoutGroup,
|
||||||
getSelectedRect,
|
getSelectedRect,
|
||||||
requestThrottledConnectedFrame,
|
requestThrottledConnectedFrame,
|
||||||
@@ -68,6 +67,7 @@ import { css, html, nothing } from 'lit';
|
|||||||
import { state } from 'lit/decorators.js';
|
import { state } from 'lit/decorators.js';
|
||||||
import { ifDefined } from 'lit/directives/if-defined.js';
|
import { ifDefined } from 'lit/directives/if-defined.js';
|
||||||
import { styleMap } from 'lit/directives/style-map.js';
|
import { styleMap } from 'lit/directives/style-map.js';
|
||||||
|
import clamp from 'lodash-es/clamp';
|
||||||
|
|
||||||
import type { EdgelessRootBlockComponent } from '../../edgeless-root-block.js';
|
import type { EdgelessRootBlockComponent } from '../../edgeless-root-block.js';
|
||||||
import {
|
import {
|
||||||
|
|||||||
@@ -14,7 +14,6 @@ import {
|
|||||||
MindmapElementModel,
|
MindmapElementModel,
|
||||||
RootBlockSchema,
|
RootBlockSchema,
|
||||||
} from '@blocksuite/affine-model';
|
} from '@blocksuite/affine-model';
|
||||||
import { clamp } from '@blocksuite/affine-shared/utils';
|
|
||||||
import type { BlockStdScope } from '@blocksuite/block-std';
|
import type { BlockStdScope } from '@blocksuite/block-std';
|
||||||
import type {
|
import type {
|
||||||
GfxController,
|
GfxController,
|
||||||
@@ -35,6 +34,7 @@ import {
|
|||||||
import { BlockSuiteError, ErrorCode } from '@blocksuite/global/exceptions';
|
import { BlockSuiteError, ErrorCode } from '@blocksuite/global/exceptions';
|
||||||
import { Bound, getCommonBound } from '@blocksuite/global/utils';
|
import { Bound, getCommonBound } from '@blocksuite/global/utils';
|
||||||
import { effect } from '@preact/signals-core';
|
import { effect } from '@preact/signals-core';
|
||||||
|
import clamp from 'lodash-es/clamp';
|
||||||
|
|
||||||
import { RootService } from '../root-service.js';
|
import { RootService } from '../root-service.js';
|
||||||
import { TemplateJob } from './services/template.js';
|
import { TemplateJob } from './services/template.js';
|
||||||
|
|||||||
@@ -27,7 +27,6 @@ import {
|
|||||||
TelemetryProvider,
|
TelemetryProvider,
|
||||||
} from '@blocksuite/affine-shared/services';
|
} from '@blocksuite/affine-shared/services';
|
||||||
import {
|
import {
|
||||||
clamp,
|
|
||||||
handleNativeRangeAtPoint,
|
handleNativeRangeAtPoint,
|
||||||
resetNativeSelection,
|
resetNativeSelection,
|
||||||
} from '@blocksuite/affine-shared/utils';
|
} from '@blocksuite/affine-shared/utils';
|
||||||
@@ -52,6 +51,7 @@ import {
|
|||||||
Vec,
|
Vec,
|
||||||
} from '@blocksuite/global/utils';
|
} from '@blocksuite/global/utils';
|
||||||
import { effect } from '@preact/signals-core';
|
import { effect } from '@preact/signals-core';
|
||||||
|
import clamp from 'lodash-es/clamp';
|
||||||
|
|
||||||
import type { EdgelessRootBlockComponent } from '../edgeless-root-block.js';
|
import type { EdgelessRootBlockComponent } from '../edgeless-root-block.js';
|
||||||
import { prepareCloneData } from '../utils/clone-utils.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 = {
|
type CollisionBox = {
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import type { Color } from '@blocksuite/affine-model';
|
import type { Color } from '@blocksuite/affine-model';
|
||||||
import { on, once, stopPropagation } from '@blocksuite/affine-shared/utils';
|
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 { batch, computed, signal } from '@preact/signals-core';
|
||||||
import { html, LitElement } from 'lit';
|
import { html, LitElement } from 'lit';
|
||||||
import { property, query } from 'lit/decorators.js';
|
import { property, query } from 'lit/decorators.js';
|
||||||
@@ -24,7 +24,6 @@ import type {
|
|||||||
} from './types.js';
|
} from './types.js';
|
||||||
import {
|
import {
|
||||||
bound01,
|
bound01,
|
||||||
clamp,
|
|
||||||
defaultHsva,
|
defaultHsva,
|
||||||
eq,
|
eq,
|
||||||
hsvaToHex8,
|
hsvaToHex8,
|
||||||
@@ -55,7 +54,7 @@ export class EdgelessColorPicker extends SignalWatcher(
|
|||||||
const orignalValue = target.value;
|
const orignalValue = target.value;
|
||||||
let value = orignalValue.trim().replace(/[^0-9]/, '');
|
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 a = bound01(alpha, 100);
|
||||||
const hsva = this.hsva$.peek();
|
const hsva = this.hsva$.peek();
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
// https://www.w3.org/TR/css-color-4/
|
// https://www.w3.org/TR/css-color-4/
|
||||||
|
|
||||||
import type { Color, ColorScheme } from '@blocksuite/affine-model';
|
import type { Color, ColorScheme } from '@blocksuite/affine-model';
|
||||||
|
import { clamp } from '@blocksuite/global/utils';
|
||||||
|
|
||||||
import { COLORS, FIRST_COLOR } from './consts.js';
|
import { COLORS, FIRST_COLOR } from './consts.js';
|
||||||
import type {
|
import type {
|
||||||
@@ -52,9 +53,6 @@ export function linearGradientAt(t: number): Rgb {
|
|||||||
|
|
||||||
const lerp = (a: number, b: number, t: number) => a + t * (b - a);
|
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) => {
|
export const bound01 = (n: number, max: number) => {
|
||||||
n = clamp(0, n, max);
|
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 { isSameDay, isSameMonth, isToday } from 'date-fns';
|
||||||
import {
|
import {
|
||||||
html,
|
html,
|
||||||
@@ -13,7 +13,7 @@ import { styleMap } from 'lit/directives/style-map.js';
|
|||||||
|
|
||||||
import { arrowLeftIcon } from './icons.js';
|
import { arrowLeftIcon } from './icons.js';
|
||||||
import { datePickerStyle } from './style.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 days = ['Su', 'Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa'];
|
||||||
const months = [
|
const months = [
|
||||||
@@ -522,7 +522,7 @@ export class DatePicker extends WithDisposable(LitElement) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
openYearSelector() {
|
openYearSelector() {
|
||||||
this._yearCursor = clamp(this._minYear, this._maxYear, this.year);
|
this._yearCursor = clamp(this.year, this._minYear, this._maxYear);
|
||||||
this._mode = 'year';
|
this._mode = 'year';
|
||||||
this._getYearMatrix();
|
this._getYearMatrix();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -66,8 +66,3 @@ export function getMonthMatrix(maybeDate: MaybeDate) {
|
|||||||
}
|
}
|
||||||
return matrix;
|
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 { createPropertyConvert } from '../core/index.js';
|
||||||
import { multiSelectPropertyModelConfig } from './multi-select/define.js';
|
import { multiSelectPropertyModelConfig } from './multi-select/define.js';
|
||||||
|
|||||||
@@ -1,11 +1,10 @@
|
|||||||
import { NoteBlockModel, RootBlockModel } from '@blocksuite/affine-model';
|
import { NoteBlockModel, RootBlockModel } from '@blocksuite/affine-model';
|
||||||
import { BLOCK_ID_ATTR, type BlockComponent } from '@blocksuite/block-std';
|
import { BLOCK_ID_ATTR, type BlockComponent } from '@blocksuite/block-std';
|
||||||
import { SurfaceBlockModel } from '@blocksuite/block-std/gfx';
|
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 type { BlockModel } from '@blocksuite/store';
|
||||||
|
|
||||||
import { BLOCK_CHILDREN_CONTAINER_PADDING_LEFT } from '../../consts/index.js';
|
import { BLOCK_CHILDREN_CONTAINER_PADDING_LEFT } from '../../consts/index.js';
|
||||||
import { clamp } from '../math.js';
|
|
||||||
import { matchModels } from '../model/checker.js';
|
import { matchModels } from '../model/checker.js';
|
||||||
|
|
||||||
const ATTR_SELECTOR = `[${BLOCK_ID_ATTR}]`;
|
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;
|
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) {
|
export function rangeWrap(n: number, min: number, max: number) {
|
||||||
max = max - min;
|
max = max - min;
|
||||||
n = (n - min + max) % max;
|
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 BLOCK_CHILDREN_CONTAINER_PADDING_LEFT = 24;
|
||||||
export const NOTE_MIN_WIDTH = 450 + 24 * 2;
|
export const NOTE_MIN_WIDTH = 450 + 24 * 2;
|
||||||
export const NOTE_MIN_HEIGHT = 92;
|
export const NOTE_MIN_HEIGHT = 92;
|
||||||
@@ -11,9 +13,6 @@ export enum NoteDisplayMode {
|
|||||||
EdgelessOnly = 'edgeless',
|
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) => {
|
export const bound01 = (n: number, max: number) => {
|
||||||
n = clamp(0, n, max);
|
n = clamp(0, n, max);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user