Skip to content

Commit

Permalink
fix width transform on media elements
Browse files Browse the repository at this point in the history
  • Loading branch information
horacioh committed Dec 10, 2024
1 parent 01d63f5 commit 15fc943
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,7 @@ describe('EditorBlock to HMBlock', () => {
children: [],
props: {
url: 'ipfs://foobarcid_IMAGE',
width: '400',
},
content: [
{
Expand All @@ -492,7 +493,9 @@ describe('EditorBlock to HMBlock', () => {
text: ``,
link: 'ipfs://foobarcid_IMAGE',
annotations: [],
attributes: {},
attributes: {
width: 400,
},
}
const val = editorBlockToHMBlock(editorBlock)

Expand All @@ -506,7 +509,7 @@ describe('EditorBlock to HMBlock', () => {
children: [],
props: {
url: 'ipfs://foobarcid_IMAGE',
width: '',
width: '400',
},
content: [
{
Expand All @@ -523,7 +526,9 @@ describe('EditorBlock to HMBlock', () => {
text: ``,
link: 'ipfs://foobarcid_IMAGE',
annotations: [],
attributes: {},
attributes: {
width: 400,
},
}
const val = editorBlockToHMBlock(editorBlock)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,9 @@ describe('HMBlock to EditorBlock', () => {
text: ``,
link: 'ipfs://foobarcid_IMAGE',
annotations: [],
attributes: {},
attributes: {
width: 400,
},
revision: 'revision123',
}

Expand All @@ -504,6 +506,7 @@ describe('HMBlock to EditorBlock', () => {
props: {
url: 'ipfs://foobarcid_IMAGE',
revision: 'revision123',
width: '400',
},
content: [
{
Expand Down
31 changes: 7 additions & 24 deletions frontend/packages/shared/src/client/editorblock-to-hmblock.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import {EditorBlock, EditorInlineContent} from '../editor-types'
import {HMAnnotations, HMBlock, HMBlockSchema, HMBlockType} from '../hm-types'
import {
HMAnnotations,
HMBlock,
HMBlockSchema,
HMBlockType,
toNumber,
} from '../hm-types'
import {AnnotationSet, codePointLength} from './unicode'

function toHMBlockType(
Expand Down Expand Up @@ -227,26 +233,3 @@ function getParentBlock(block: HMBlock) {
if (block.type == 'Code') return block
return undefined
}

function toNumber(value?: string): number | null {
// Handle empty or whitespace-only strings
if (!value) return null
if (typeof value !== 'string') {
console.error('Unexpected numeric value is not a string: ', value)
return null
}
if (value.trim() === '') {
return null
}

// Convert to number and check if it's valid
const num = Number(value)

// Check if the conversion resulted in a valid number
// isNaN() will return true for NaN and invalid conversions
if (isNaN(num)) {
return null
}

return num
}
12 changes: 6 additions & 6 deletions frontend/packages/shared/src/hm-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,24 +217,24 @@ export const HMBlockMathSchema = z
})
.strict()

export function toNumber(value: any): number {
export function toNumber(value: any): number | null {
// If it's already a number, return it directly
if (typeof value === 'number' && !isNaN(value)) {
if (typeof value == 'number' && !isNaN(value)) {
return value
}

// If it's a string, try to convert it
if (typeof value === 'string') {
if (typeof value == 'string') {
const converted = Number(value)
if (!isNaN(converted)) {
return converted
}
}

// If we get here, throw an error
throw new Error(
console.warn(
'Value must be a number or a string that can be converted to a number',
value,
)
return null
}

export const HMBlockImageSchema = z
Expand Down
3 changes: 3 additions & 0 deletions frontend/packages/ui/src/document-content.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1173,6 +1173,7 @@ function BlockContentImage({
data-url={block?.link}
data-alt={block?.attributes?.alt}
data-width={block.attributes?.width}
maxWidth="100%"
paddingVertical="$3"
gap="$2"
ai="center"
Expand All @@ -1182,6 +1183,7 @@ function BlockContentImage({
width={
block.attributes?.width ? `${block.attributes?.width}px` : undefined
}
maxWidth="100%"
>
<img
alt={block?.attributes?.alt}
Expand Down Expand Up @@ -1228,6 +1230,7 @@ function BlockContentVideo({
width={
block.attributes?.width ? `${block.attributes?.width}px` : "100%"
}
maxWidth="100%"
position="relative"
paddingBottom={
link.startsWith("ipfs://") || link.startsWith("http")
Expand Down

0 comments on commit 15fc943

Please sign in to comment.