Skip to content
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

feat: 新增链接和图片处理接口 #1348

Merged
merged 4 commits into from
Jan 15, 2023
Merged
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
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,7 @@ new Vditor('vditor', {
| url | md 解析请求 | - |
| parse(element: HTMLElement) | 预览回调 | - |
| transform(html: string): string | 渲染之前回调 | - |
| showImage: boolean | 是否双击图片预览 | _ |

#### options.preview.hljs

Expand Down Expand Up @@ -352,6 +353,24 @@ new Vditor('vditor', {
| className | 按钮类名 | - |
| click(key: string) | 按钮点击回调事件 | - |

#### options.previewImage
``` ts
previewImage: (img: HTMLImageElement) => void;
```
对原图片双击预览的拦截,对图片的扩展操作。

#### options.link
``` ts
link?: {
open?: boolean;
trigger?: (href: string) => void;
}
```
| | 说明 | 默认值 |
| - | - | - |
| open | 是否点击打开(window.open)地址 | - |
| trigger | 地址点击触发 | - |

#### options.hint

| | 说明 | 默认值 |
Expand Down
7 changes: 6 additions & 1 deletion src/ts/ir/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,12 @@ class IR {
// 打开链接
const aElement = hasClosestByAttribute(event.target, "data-type", "a");
if (aElement && (!aElement.classList.contains("vditor-ir__node--expand"))) {
window.open(aElement.querySelector(":scope > .vditor-ir__marker--link").textContent);
if (vditor.options.link && vditor.options.link.trigger) {
vditor.options.link.trigger(aElement.querySelector(":scope > .vditor-ir__marker--link").textContent);
}
if (vditor.options.link && vditor.options.link.open) {
window.open(aElement.querySelector(":scope > .vditor-ir__marker--link").textContent);
}
return;
}

Expand Down
5 changes: 4 additions & 1 deletion src/ts/preview/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,10 @@ export class Preview {
}
return;
}
if (event.target.tagName === "IMG") {
if (vditor.options.previewImage) {
vditor.options.previewImage(event.target as HTMLImageElement)
}
if (vditor.options.preview.showImage) {
previewImage(event.target as HTMLImageElement, vditor.options.lang, vditor.options.theme);
}
});
Expand Down
7 changes: 6 additions & 1 deletion src/ts/util/editorCommonEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,12 @@ export const focusEvent = (vditor: IVditor, editorElement: HTMLElement) => {
export const dblclickEvent = (vditor: IVditor, editorElement: HTMLElement) => {
editorElement.addEventListener("dblclick", (event: MouseEvent & { target: HTMLElement }) => {
if (event.target.tagName === "IMG") {
previewImage(event.target as HTMLImageElement, vditor.options.lang, vditor.options.theme);
if (vditor.options.previewImage) {
vditor.options.previewImage(event.target as HTMLImageElement)
}
if (vditor.options.preview.showImage) {
previewImage(event.target as HTMLImageElement, vditor.options.lang, vditor.options.theme);
}
}
});
};
Expand Down
6 changes: 6 additions & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,7 @@ interface IPreview {
theme?: IPreviewTheme;
/** @link https://ld246.com/article/1549638745630#options-preview-actions */
actions?: Array<IPreviewAction | IPreviewActionCustom>;
showImage?: boolean;

/** 预览回调 */
parse?(element: HTMLElement): void;
Expand Down Expand Up @@ -619,6 +620,11 @@ interface IOptions {
mode?: "wysiwyg" | "sv" | "ir";
/** @link https://ld246.com/article/1549638745630#options-preview */
preview?: IPreview;
previewImage?: (img: HTMLImageElement) => void;
link?: {
open?: boolean;
trigger?: (href: string) => void;
},
/** @link https://ld246.com/article/1549638745630#options-hint */
hint?: IHint;
/** @link https://ld246.com/article/1549638745630#options-toolbarConfig */
Expand Down