Skip to content

fix(extension): base64导出支持画布局部渲染时导出&修复相对路径图片导出问题&补充文档 #2114

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added examples/feature-examples/public/images/test.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 10 additions & 0 deletions examples/feature-examples/src/pages/extensions/snapshot/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,16 @@ export default {
y: 100,
text: '菱形',
},
{
Copy link
Collaborator Author

@DymoneLewis DymoneLewis Apr 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

新增了一个展示上面所示的项目路径下图片的节点

id: 'test_image_1',
type: 'test-image',
x: 300,
y: 200,
properties: {
width: 82,
height: 96,
},
},
],
edges: [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,43 @@ class ImageNode extends RectNode {
}
}

export default {
class TestImageModel extends RectNodeModel {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

图片展示节点的定义

initNodeData(data: any) {
super.initNodeData(data)
this.width = 100
this.height = 75
}
}

class TestImageNode extends RectNode {
getImageHref() {
return '/images/test.jpeg'
}
getShape() {
const { x, y, width, height } = this.props.model
const href = this.getImageHref()
const attrs = {
x: x - (1 / 2) * width,
y: y - (1 / 2) * height,
width,
height,
href,
preserveAspectRatio: 'none meet',
}
return h('g', {}, [h('image', { ...attrs })])
}
}

const defaultImageNode = {
type: 'image',
view: ImageNode,
model: ImageModel,
}

export const testImage = {
type: 'test-image',
view: TestImageNode,
model: TestImageModel,
}

export default defaultImageNode
64 changes: 40 additions & 24 deletions examples/feature-examples/src/pages/extensions/snapshot/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ import {
InputNumber,
Switch,
} from 'antd'
import ImageNode from './imageNode'
import CustomHtml from '@/components/nodes/custom-html/Html'
import ImageNode, { testImage } from './imageNode'
import data from './data'
import { circle as circleSvgUrl, rect as rectSvgUrl } from './svg'

Expand Down Expand Up @@ -89,6 +89,7 @@ export default function SnapshotExample() {
})
lf.register(CustomHtml)
lf.register(ImageNode)
lf.register(testImage)

lf.setPatternItems([
{
Expand All @@ -103,6 +104,12 @@ export default function SnapshotExample() {
text: 'circle',
icon: rectSvgUrl,
},
{
type: 'test-image',
label: 'Test Image',
text: 'Test Image',
icon: rectSvgUrl,
},
])

lf.on('custom:button-click', (model: any) => {
Expand Down Expand Up @@ -156,9 +163,18 @@ export default function SnapshotExample() {
// 预览 blob
const previewBlob = () => {
if (lfRef.current) {
setBase64Data('')
setBlobData('')
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里setBase64改成setBlob主要是觉得base64的图片和blob的图片展示并不互斥,所以这里改成了setBlobData,在赋值前重置一下

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤔这么一看感觉好像能删了,毕竟下面会直接赋值

const params: ToImageOptions = {
fileType,
backgroundColor,
partial,
width,
height,
padding,
quality,
}
lfRef.current
.getSnapshotBlob(backgroundColor, fileType)
.getSnapshotBlob(backgroundColor, fileType, params)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

导出blob支持传入ToImageOptions

.then(
({
data,
Expand All @@ -177,25 +193,25 @@ export default function SnapshotExample() {
}

// 预览 base64
const previewBase64 = () => {
const previewBase64 = async () => {
if (lfRef.current) {
setBlobData('')
lfRef.current
.getSnapshotBase64(backgroundColor)
.then(
({
data,
width,
height,
}: {
data: string
width: number
height: number
}) => {
setBase64Data(data)
console.log('width, height ', width, height)
},
)
setBase64Data('')
const params: ToImageOptions = {
fileType,
backgroundColor,
partial,
width,
height,
padding,
quality,
}
const result = await lfRef.current.getSnapshotBase64(
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

base64的新调用方法

'white',
'png',
params,
)
setBase64Data(result.data)
console.log('width, height ', result)
}
}

Expand Down Expand Up @@ -303,17 +319,17 @@ export default function SnapshotExample() {
<div ref={containerRef} className="graph"></div>
</Flex>
<Row>
<Col span={12}>
<Col span={24}>
{blobData && (
<>
<h2>blobData</h2>
<img key="blob" src={blobData} />
<img style={{ width: '100%' }} key="blob" src={blobData} />
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

之前父元素比较窄,导出图片小的时候图挤成一团,图片大的时候又得左右滚动页面看,交互太难受了所以就调整了一下布局

</>
)}
{base64Data && (
<>
<h2>base64Data</h2>
<img key="base64" src={base64Data} />
<img style={{ width: '100%' }} key="base64" src={base64Data} />
</>
)}
</Col>
Expand Down
4 changes: 4 additions & 0 deletions packages/extension/src/style/raw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,10 @@ export const content = `@import url('medium-editor/dist/css/medium-editor.min.cs
background: #eaedf2;
border: 1px solid #93a3b4;
}
.lf-mini-map .lf-graph {
width: 100% !important;
height: 100% !important;
}
.lf-mini-map-graph {
position: relative;
overflow: hidden;
Expand Down
Loading