Skip to content

Commit

Permalink
feat: 图片相关&链接open (#1348)
Browse files Browse the repository at this point in the history
* feat: 图片相关
- #1220 改善图片预览问题,增加options->preview->showImage: boolean控制是否双击预览
- 增加options->previewImage函数获取触发的图片节点,参数img: HTMLImageElement

链接相关
- 增加options-> link属性,options->open: boolean控制点击打开链接,intercept函数控制点击的链接,参数 url: string

* style: 回滚主分支的代码格式

* refactor: 重新对link的触发函数命名

* docs: 增加对img和link的readme
  • Loading branch information
heiyehk committed Jan 15, 2023
1 parent 9dbbc79 commit 6da6b67
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 3 deletions.
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

0 comments on commit 6da6b67

Please sign in to comment.