图片
插入图片地址,并调整宽度和对齐方式。默认情况下,地址限制为 http:、https: 或同站路径,新图片会以 60% 宽度居中开始。
宽度只保存为固定档位,对齐方式保存在包裹图片的段落上。要使用 blob: 或 data:image/... 预览,需要在图片 wing 和编辑器组装处都明确允许本地 URL。不允许 SVG data URL。
npm install nabi-noteimport {
createLocale, createNabiWith, mountSurface, mountToolbar,
mountContextToolbar, mountHints, watchSettle, tableWings, imageWing,
youtubeWing, mountViewTools,
} from 'nabi-note'
const selected = [
...tableWings,
imageWing,
youtubeWing,
]
const locale = createLocale('zh')
const { nabi, registry } = createNabiWith(selected, { locale })
const root = document.querySelector('.nabi')!
const content = document.querySelector('.nabi-content')!
// The locale also sets the direction — Arabic and Urdu run right to left
mountSurface({ nabi, registry, root: content, locale })
const settle = watchSettle(document, { surface: content })
const shared = { nabi, registry, surface: content, settle, locale }
const toolbar = mountToolbar({ ...shared, root: document.querySelector('#toolbar')! })
const context = mountContextToolbar({ ...shared, root: document.querySelector('#context')! })
mountHints({ toolbar, context, root, surface: content })
// The preview and fullscreen buttons — they stand their own box at the end of the row
mountViewTools({ ...shared, root, container: document.querySelector('#toolbar')! })
// on every change — hook up your own code here
// nabi.onChange(() => user_callback(nabi.getHtml()))
// Call from your language selector. Content and editing history stay intact.
// locale.setLocale('ko')const selected = wings().use('img', {
allowLocalUrls: false,
}).build()这个 wing 只是把地址插入文档,并不会上传文件。要把文件发送到服务器,请连接上传 wing。
连接图片选择器
通过 mountToolbar() 的 panels.img,可以将图片按钮默认的 URL 输入窗口替换为服务自己的图片选择器。键名使用工具栏槽位名称;未指定的工具保留默认输入窗口。
mode: 'modal' 在覆盖整个屏幕的半透明背景上打开窗口。mode: 'inline' 在桌面端的工具按钮附近打开,在移动端则全屏显示。是否采用移动端显示由视口宽度和 --nabi-mobile-breakpoint 决定;如果打开的 inline 面板跨越该阈值,面板会关闭。
两种模式都只提供空的 root,不创建标题、输入框或按钮。在 render 中加入所需的 HTML 或 UI,将关闭按钮连接到 close(),将图片选择连接到 insertImage(url, 'pointer')。原有的函数形式配置(img: renderer)保持原来的显示方式。
import { mountToolbar } from 'nabi-note'
const toolbar = mountToolbar({
nabi,
registry,
root: toolbarRoot,
surface: content,
panels: {
img: {
mode: 'inline',
render: ({ root, signal, close, insertImage }) =>
mountMyImagePicker(root, {
signal,
onClose: close,
onSelect: (url: string) => insertImage(url, 'pointer'),
}),
},
},
})mountMyImagePicker 是由服务自行实现的函数。它在传入的 root 中同步创建所需 UI,并返回清理函数。加载图片列表或上传等异步任务应接入 signal,并将选中的图片 URL 传给 onSelect。此 API 不传输文件,图片 URL 仍遵循现有的允许规则。
insertImage(src, by?) 等同于 run('insertImage', { src }, by),返回值和选区恢复规则也相同。省略 by 时使用 'keyboard'。请勿将 render 写成 async 函数。
关闭窗口或卸载工具栏时,signal 会中止,清理函数也会被调用。run() 关闭窗口,并在窗口打开时记录的选区上执行一次命令。如果窗口已经关闭,或者打开后文档内容发生了变化,则返回 false,不执行命令。
如果只是移动了光标,图片仍会插入打开面板时的原始位置。如果在表格单元格或浮动区域内打开面板,图片就会插入其中。编辑或替换文档后,即使撤销恢复了相同内容,也会取消插入并提示;请重新打开面板选择图片。
CSS 样式
用 .nabi-content img 设置图片样式。保持保存的宽度和对齐方式不变,只改变边框或阴影等视觉细节。
.article-body img {
border-radius: 12px;
box-shadow: 0 8px 24px rgb(0 0 0 / 12%);
}
.dark .article-body img { box-shadow: 0 8px 24px rgb(0 0 0 / 35%); }保留 max-inline-size、block-size、width 和对齐方式的默认规则。图片大小保存在文档中,因此强制设置固定 CSS 宽度可能会和作者选择的宽度冲突。