Appearance
图片 Image
在 TipTap 官方 Image 扩展基础上增强:支持内联混排、四向缩放手柄、左右浮动。
用法
ts
import { Image } from '@qingpai/core'
const editor = new Editor({
extensions: [
Image.configure({
inline: true,
resize: {
enabled: true,
directions: ['top-left', 'top-right', 'bottom-left', 'bottom-right'],
alwaysPreserveAspectRatio: true,
minWidth: 16,
minHeight: 16,
},
}),
],
})配置项
| 名称 | 类型 | 默认值 | 说明 |
|---|---|---|---|
inline | boolean | false | 是否为内联图片(可与文字混排) |
allowBase64 | boolean | false | 是否允许 base64 图片地址 |
HTMLAttributes | Record<string, any> | {} | 渲染到图片元素上的 HTML 属性 |
resize | false | ResizeOptions | false | 缩放手柄配置,见下表 |
ResizeOptions
| 名称 | 类型 | 说明 |
|---|---|---|
enabled | boolean | 是否启用缩放手柄(未启用时不挂载 NodeView) |
directions | ResizableNodeViewDirection[] | 手柄方向,默认仅四个角 |
alwaysPreserveAspectRatio | boolean | 是否始终保持宽高比 |
minWidth | number | 最小宽度(px) |
minHeight | number | 最小高度(px) |
ResizableNodeViewDirection 取值
tiptap 官方类型,支持四边与四个角:
| 值 | 说明 |
|---|---|
'top' | 上边缘 |
'right' | 右边缘 |
'bottom' | 下边缘 |
'left' | 左边缘 |
'top-left' | 左上角 |
'top-right' | 右上角 |
'bottom-left' | 左下角 |
'bottom-right' | 右下角 |
属性
除官方 src、alt、title 外,新增:
| 名称 | 类型 | 默认值 | 说明 |
|---|---|---|---|
align | ImageAlign | null | null | 对齐方向;渲染为 qp-image-align-left / qp-image-align-right 类,从已有 class 中解析 |
ImageAlign 取值
| 值 | 说明 |
|---|---|
'left' | 左对齐(左浮动绕排) |
'right' | 右对齐(右浮动绕排) |
命令
| 命令 | 说明 |
|---|---|
setImage(options) | 官方命令,插入图片。options 含 src、alt、title、width、height |
setImageAlign(align?) | 设置图片对齐方向(取值),不传则取消对齐 |
ts
editor.commands.setImage({ src: 'https://example.com/a.png' })
editor.commands.setImageAlign('left')配套 UI
| 名称 | 说明 |
|---|---|
ImageButton | 工具栏插入图片按钮 |
ImageBubbleMenu | 选中图片时的气泡菜单(浮动等操作) |
Source code
packages/core/src/nodes/image/