Skip to content

Commit 19252a1

Browse files
Support for new image alignment
- Support the new Notion image alignment system, with left, right and center alignment - Some types in asset-wrapper
1 parent d0f588e commit 19252a1

File tree

3 files changed

+38
-3
lines changed

3 files changed

+38
-3
lines changed

packages/notion-types/src/block.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,24 @@ export interface BaseContentBlock extends BaseBlock {
142142
file_ids?: string[]
143143
}
144144

145+
export interface BaseImageBlock extends BaseContentBlock {
146+
properties: {
147+
source: string[][]
148+
caption?: Decoration[]
149+
}
150+
format?: {
151+
block_alignment: 'center' | 'left' | 'right'
152+
block_width: number
153+
block_height: number
154+
display_source: string
155+
block_full_width: boolean
156+
block_page_width: boolean
157+
block_aspect_ratio: number
158+
block_preserve_scale: boolean
159+
}
160+
file_ids?: string[]
161+
}
162+
145163
export interface BasePageBlock extends BaseBlock {
146164
properties?: {
147165
title: Decoration[]

packages/react-notion-x/src/components/asset-wrapper.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ export const AssetWrapper: React.FC<{
7474
return figure
7575
}
7676

77-
function isValidURL(str) {
77+
function isValidURL(str: string) {
7878
// TODO: replace this with a more well-tested package
7979
const pattern = new RegExp(
8080
'^(https?:\\/\\/)?' + // protocol
@@ -88,7 +88,7 @@ function isValidURL(str) {
8888
return !!pattern.test(str)
8989
}
9090

91-
function extractHostname(url) {
91+
function extractHostname(url: string) {
9292
try {
9393
const hostname = new URL(url).hostname
9494
return hostname

packages/react-notion-x/src/components/asset.tsx

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as React from 'react'
2-
import { BaseContentBlock, Block } from 'notion-types'
2+
import { BaseContentBlock, BaseImageBlock, Block } from 'notion-types'
33
import { getTextContent } from 'notion-utils'
44

55
import { useNotionContext } from '../context'
@@ -87,6 +87,23 @@ export const Asset: React.FC<{
8787
}
8888
}
8989
} else {
90+
if (block.type == 'image') {
91+
switch ((block as BaseImageBlock).format?.block_alignment) {
92+
case 'center': {
93+
style.alignSelf = 'center'
94+
break
95+
}
96+
case 'left': {
97+
style.alignSelf = 'start'
98+
break
99+
}
100+
case 'right': {
101+
style.alignSelf = 'end'
102+
break
103+
}
104+
}
105+
}
106+
90107
if (block_width) {
91108
style.width = block_width
92109
}

0 commit comments

Comments
 (0)