Skip to content

开源白板工具(SaaS),一体化白板,包含思维导图、流程图、自由画等。All in one open-source whiteboard tool with mind, flowchart, freehand and etc.

fix: add tooltip, fix get wrong percentage, change cursor when not allowed (#318)

Six5ec337d9408c
diff --git a/packages/drawnix/src/components/color-picker.tsx b/packages/drawnix/src/components/color-picker.tsx
index ece9c0b..c1967e6 100644
--- a/packages/drawnix/src/components/color-picker.tsx
+++ b/packages/drawnix/src/components/color-picker.tsx
@@ -49,6 +49,7 @@ export const ColorPicker = React.forwardRef((props: ColorPickerProps, ref) => {
return (
<Stack.Col gap={3}>
<SizeSlider
+ title={t('popupToolbar.opacity')}
step={5}
defaultValue={opacity}
onChange={(value) => {
diff --git a/packages/drawnix/src/components/size-slider.scss b/packages/drawnix/src/components/size-slider.scss
index a9e6f61..1092b0e 100644
--- a/packages/drawnix/src/components/size-slider.scss
+++ b/packages/drawnix/src/components/size-slider.scss
@@ -4,6 +4,11 @@
padding: 10px 0px;
&.disabled {
opacity: 50%;
+ cursor: not-allowed;
+
+ .slider-track,.slider-thumb {
+ cursor: not-allowed;
+ }
}
.slider-track {
position: relative;
diff --git a/packages/drawnix/src/components/size-slider.tsx b/packages/drawnix/src/components/size-slider.tsx
index 7c68f34..c3d4f0e 100644
--- a/packages/drawnix/src/components/size-slider.tsx
+++ b/packages/drawnix/src/components/size-slider.tsx
@@ -1,4 +1,10 @@
-import React, { useState, useRef, useCallback, useEffect } from 'react';
+import React, {
+ useState,
+ useRef,
+ useCallback,
+ useEffect,
+ useMemo,
+} from 'react';
import { toFixed } from '@plait/core';
import './size-slider.scss';
import classNames from 'classnames';
@@ -10,6 +16,7 @@ interface SliderProps {
step?: number;
defaultValue?: number;
disabled?: boolean;
+ title?: string;
onChange?: (value: number) => void;
beforeStart?: () => void;
afterEnd?: () => void;
@@ -24,37 +31,28 @@ export const SizeSlider: React.FC<SliderProps> = ({
onChange,
beforeStart,
afterEnd,
+ title,
}) => {
const [isDragging, setIsDragging] = useState(false);
const [value, setValue] = useState(defaultValue);
- const thumbPercentageRef = useRef(0);
const sliderRef = useRef<HTMLDivElement>(null);
const thumbRef = useRef<HTMLDivElement>(null);
-
- useEffect(() => {
- if (sliderRef.current && thumbRef.current) {
- const sliderRect = sliderRef.current.getBoundingClientRect();
- const thumbRect = thumbRef.current.getBoundingClientRect();
- thumbPercentageRef.current = toFixed(
- (thumbRect.width / 2 / sliderRect.width) * 100
- );
- }
- }, [thumbRef, sliderRef]);
-
- useEffect(() => {
- setValue(defaultValue);
- }, [defaultValue]);
+ const percentage = ((value - min) / (max - min)) * 100;
const handleSliderChange = useCallback(
throttle(
(event: React.MouseEvent<HTMLDivElement> | MouseEvent) => {
if (sliderRef.current && thumbRef.current) {
const sliderRect = sliderRef.current.getBoundingClientRect();
+ const thumbRect = thumbRef.current.getBoundingClientRect();
const x = event.clientX - sliderRect.left;
+ const thumbPercentage = toFixed(
+ (thumbRect.width / 2 / sliderRect.width) * 100
+ );
let percentage = Math.min(Math.max(x / sliderRect.width, 0), 1);
- if (percentage >= (100 - thumbPercentageRef.current) / 100) {
+ if (percentage >= (100 - thumbPercentage) / 100) {
percentage = 1;
- } else if (percentage <= thumbPercentageRef.current / 100) {
+ } else if (percentage <= thumbPercentage / 100) {
percentage = 0;
}
const newValue =
@@ -87,16 +85,16 @@ export const SizeSlider: React.FC<SliderProps> = ({
document.addEventListener('pointerup', handleMouseUp);
}, [handleSliderChange]);
- let percentage = ((value - min) / (max - min)) * 100;
- if (percentage >= 100 - thumbPercentageRef.current) {
- percentage = 100 - thumbPercentageRef.current;
- }
- if (percentage <= thumbPercentageRef.current) {
- percentage = thumbPercentageRef.current;
- }
+ useEffect(()=>{
+ setValue(defaultValue)
+ },[defaultValue])
return (
- <div className={classNames('slider-container', { disabled: disabled })}>
+ <div
+ data-tooltip
+ title={title}
+ className={classNames('slider-container', { disabled: disabled })}
+ >
<div
ref={sliderRef}
className="slider-track"
diff --git a/packages/drawnix/src/i18n/translations/en.ts b/packages/drawnix/src/i18n/translations/en.ts
index 5fc833e..3681579 100644
--- a/packages/drawnix/src/i18n/translations/en.ts
+++ b/packages/drawnix/src/i18n/translations/en.ts
@@ -121,6 +121,7 @@ const enTranslations: Translations = {
'popupToolbar.fontColor': 'Font Color',
'popupToolbar.link': 'Insert Link',
'popupToolbar.stroke': 'Stroke',
+ 'popupToolbar.opacity': 'Opacity',
// Text placeholders
'textPlaceholders.link': 'Link',
@@ -167,7 +168,8 @@ const enTranslations: Translations = {
'mind.abstractNodeText': 'Summary',
'tutorial.title': 'Drawnix',
- 'tutorial.description': 'All-in-one whiteboard, including mind maps, flowcharts, free drawing, and more',
+ 'tutorial.description':
+ 'All-in-one whiteboard, including mind maps, flowcharts, free drawing, and more',
'tutorial.dataDescription': 'All data is stored locally in your browser',
'tutorial.appToolbar': 'Export, language settings, ...',
'tutorial.creationToolbar': 'Select a tool to start your creation',
diff --git a/packages/drawnix/src/i18n/translations/zh.ts b/packages/drawnix/src/i18n/translations/zh.ts
index aea3147..5a44f50 100644
--- a/packages/drawnix/src/i18n/translations/zh.ts
+++ b/packages/drawnix/src/i18n/translations/zh.ts
@@ -72,7 +72,6 @@ const zhTranslations: Translations = {
'language.russian': 'Русский',
'language.arabic': 'عربي',
'language.vietnamese': 'Tiếng Việt',
-
// Menu items
'menu.open': '打开',
'menu.saveFile': '保存文件',
@@ -119,7 +118,8 @@ const zhTranslations: Translations = {
'popupToolbar.fontColor': '字体颜色',
'popupToolbar.link': '链接',
'popupToolbar.stroke': '边框',
-
+ 'popupToolbar.opacity': '不透明度',
+
// Text placeholders
'textPlaceholders.link': '链接',
'textPlaceholders.text': '文本',
@@ -138,7 +138,7 @@ const zhTranslations: Translations = {
// Draw elements text
'draw.lineText': '文本',
'draw.geometryText': '文本',
-
+
// Mind map elements text
'mind.centralText': '中心主题',
'mind.abstractNodeText': '摘要',
@@ -170,7 +170,6 @@ const zhTranslations: Translations = {
'tutorial.appToolbar': '导出,语言设置,...',
'tutorial.creationToolbar': '选择一个工具开始你的创作',
'tutorial.themeDescription': '在明亮和黑暗主题之间切换',
-
};
-export default zhTranslations;
\ No newline at end of file
+export default zhTranslations;
diff --git a/packages/drawnix/src/i18n/types.ts b/packages/drawnix/src/i18n/types.ts
index ec9e957..a17bad4 100644
--- a/packages/drawnix/src/i18n/types.ts
+++ b/packages/drawnix/src/i18n/types.ts
@@ -123,6 +123,7 @@ export interface Translations {
'popupToolbar.fontColor': string;
'popupToolbar.link': string;
'popupToolbar.stroke': string;
+ 'popupToolbar.opacity': string;
// Text placeholders
'textPlaceholders.link': string;