Skip to content

Commit 88efc72

Browse files
authored
fix: use ratio for columns (#65)
1 parent 05f8bbd commit 88efc72

File tree

6 files changed

+40
-28
lines changed

6 files changed

+40
-28
lines changed

src/application/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ export interface SubpageNodeData extends BlockData {
201201
}
202202

203203
export interface ColumnNodeData extends BlockData {
204-
width?: number;
204+
ratio?: number;
205205
}
206206

207207
export enum MentionType {

src/components/editor/components/blocks/columns/Column.tsx

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,12 @@ import { ColumnNode, EditorElementProps } from '@/components/editor/editor.type'
22
import React, { forwardRef, memo } from 'react';
33

44
export const Column = memo(
5-
forwardRef<HTMLDivElement, EditorElementProps<ColumnNode>>(({ node, children, ...attributes }, ref) => {
6-
const data = node.data;
7-
5+
forwardRef<HTMLDivElement, EditorElementProps<ColumnNode>>(({ node: _node, children, ...attributes }, ref) => {
86
return (
97
<div
10-
ref={ref} {...attributes}
11-
className={`${attributes.className ?? ''} min-w-[120px] overflow-hidden`}
12-
style={{
13-
width: data.width ? `${data.width}px` : '100%',
14-
}}
8+
ref={ref}
9+
{...attributes}
10+
className={`${attributes.className ?? ''} overflow-hidden`}
1511
>
1612
{children}
1713
</div>

src/components/editor/components/blocks/columns/Columns.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import React, { forwardRef, memo } from 'react';
33

44
export const Columns = memo(
55
forwardRef<HTMLDivElement, EditorElementProps<ColumnsNode>>(({ node: _node, children, ...attributes }, ref) => {
6+
67
return (
78
<div
89
ref={ref}

src/components/editor/components/blocks/image/Img.tsx

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ function Img({ onLoad, imgRef, url, width }: {
1111
width: number | string;
1212
}) {
1313
const { t } = useTranslation();
14-
const [localUrl, setLocalUrl] = useState<string | null>(null);
1514
const [loading, setLoading] = useState(true);
1615
const [imgError, setImgError] = useState<{
1716
ok: boolean;
@@ -32,18 +31,18 @@ function Img({ onLoad, imgRef, url, width }: {
3231

3332
const attemptCheck: () => Promise<boolean> = async() => {
3433
try {
34+
console.log(`Checking ${pollingInterval}ms`);
3535
const result = await checkImage(url);
3636

3737
// Success case
3838
if(result.ok) {
3939
setImgError(null);
4040
setLoading(false);
41-
setLocalUrl(result.validatedUrl || url);
4241
setTimeout(() => {
4342
if(onLoad) {
4443
onLoad();
4544
}
46-
}, 500);
45+
}, 200);
4746

4847
return true;
4948
}
@@ -92,7 +91,7 @@ function Img({ onLoad, imgRef, url, width }: {
9291
<>
9392
<img
9493
ref={imgRef}
95-
src={localUrl || url}
94+
src={url}
9695
alt={''}
9796
onLoad={() => {
9897
setLoading(false);
@@ -112,7 +111,7 @@ function Img({ onLoad, imgRef, url, width }: {
112111
) : imgError ? (
113112
<div
114113
className={
115-
'flex h-[48px] top-0 absolute bg-bg-body w-full items-center justify-center gap-2 rounded border border-function-error bg-red-50'
114+
'flex h-[48px] w-full items-center justify-center gap-2 rounded border border-function-error bg-red-50'
116115
}
117116
>
118117
<ErrorOutline className={'text-function-error'} />

src/components/editor/components/element/Element.tsx

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -161,11 +161,27 @@ export const Element = ({
161161
}
162162
}, [type]) as FC<EditorElementProps>;
163163

164+
const blockStyle = useMemo(() => {
165+
const type = node.type as BlockType;
166+
const style = {};
167+
168+
if(type === BlockType.ColumnBlock) {
169+
const ratio = (node.data as ColumnNodeData)?.ratio;
170+
171+
Object.assign(style, {
172+
flexGrow: ratio ? ratio * 1000 : 500,
173+
flexBasis: 0,
174+
flexShrink: 0,
175+
});
176+
}
177+
178+
return style;
179+
}, [node.data, node.type]);
180+
164181
const className = useMemo(() => {
165182
const data = (node.data as BlockData) || {};
166183
const align = data.align;
167184
const classList = ['block-element relative flex rounded-[4px]'];
168-
const type = node.type as BlockType;
169185

170186
if(selected) {
171187
classList.push('selected');
@@ -175,16 +191,8 @@ export const Element = ({
175191
classList.push(`block-align-${align}`);
176192
}
177193

178-
if(type === BlockType.ColumnBlock) {
179-
const width = (data as ColumnNodeData).width;
180-
181-
if(!width) {
182-
classList.push('flex-1');
183-
}
184-
}
185-
186194
return classList.join(' ');
187-
}, [node.data, selected, node.type]);
195+
}, [node.data, selected]);
188196

189197
const style = useMemo(() => {
190198
const data = (node.data as BlockData) || {};
@@ -193,8 +201,17 @@ export const Element = ({
193201
color: data.font_color ? renderColor(data.font_color) : undefined,
194202
};
195203

204+
const type = node.type as BlockType;
205+
206+
if(type === BlockType.ColumnsBlock) {
207+
Object.assign(properties, {
208+
display: 'flex',
209+
width: '100%',
210+
});
211+
}
212+
196213
return properties;
197-
}, [node.data, selected]);
214+
}, [node.data, node.type, selected]);
198215

199216
const fallbackRender = useMemo(() => {
200217
return (props: FallbackProps) => {
@@ -229,6 +246,7 @@ export const Element = ({
229246
{...attributes}
230247
data-block-type={type}
231248
className={className}
249+
style={blockStyle}
232250
>
233251
<Component
234252
style={style}

src/utils/image.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,7 @@ export const checkImage = async(url: string) => {
4242
});
4343
};
4444

45-
const cacheBuster = `?cb=${Date.now()}`;
46-
47-
img.src = url + cacheBuster;
45+
img.src = url;
4846

4947
});
5048
};

0 commit comments

Comments
 (0)