-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
157 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
<script setup lang="ts"> | ||
import { useTiptapStore } from '@/hooks' | ||
import type { Editor } from '@tiptap/core' | ||
import ProseMirrorStyle from '@/styles/ProseMirror.scss?inline' | ||
import { useHotkeys } from '@/hooks' | ||
interface Props { | ||
editor: Editor | ||
disabled?: boolean | ||
containerRef: Object | ||
} | ||
const props = withDefaults(defineProps<Props>(), { | ||
disabled: false, | ||
containerRef: undefined, | ||
}) | ||
const { state } = useTiptapStore() | ||
const srcdoc = ref('') | ||
const iframeRef = ref<HTMLIFrameElement | null>(null) | ||
async function handlePrint() { | ||
props.editor.commands.blur() | ||
const content = props.editor.getHTML() | ||
srcdoc.value = ` | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<title>Echo Editor</title> | ||
<meta charset="UTF-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
<style>${ProseMirrorStyle}</style> | ||
</head> | ||
<body class="is-print"> | ||
<div class="tiptap ProseMirror" translate="no" aria-expanded="false"> | ||
${content} | ||
</div> | ||
</body> | ||
</html>` | ||
await nextTick() | ||
setupPrintListener(iframeRef.value!) | ||
iframeRef?.value?.contentWindow?.print() | ||
} | ||
function setupPrintListener(iframe: HTMLIFrameElement) { | ||
if (!iframe.contentWindow) { | ||
console.error('无法访问 iframe 的 contentWindow') | ||
return | ||
} | ||
const iframeWindow = iframe.contentWindow | ||
// 监听 afterprint 事件(用于处理用户取消打印的情况) | ||
iframeWindow.addEventListener('afterprint', () => { | ||
state.printer = false | ||
}) | ||
} | ||
watch( | ||
() => state.printer, | ||
val => { | ||
if (val) { | ||
handlePrint() | ||
} | ||
} | ||
) | ||
// 创建快捷键的绑定和解绑控制 | ||
const { bind, unbind } = useHotkeys('ctrl+p,command+p', () => { | ||
state.printer = true | ||
}) | ||
// 当编辑器获得焦点时绑定快捷键,失去焦点时解绑快捷键 | ||
props.editor.on('focus', bind) | ||
props.editor.on('blur', unbind) | ||
</script> | ||
<template> | ||
<iframe ref="iframeRef" v-if="state.printer" class="absolute w-0 h-0 border-none overflow-auto" :srcdoc="srcdoc" /> | ||
</template> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import ActionButton from '@/components/ActionButton.vue' | ||
import { useTiptapStore } from '@/hooks' | ||
import type { GeneralOptions } from '@/type' | ||
import { Extension } from '@tiptap/core' | ||
export interface PrinterOptions extends GeneralOptions<PrinterOptions> {} | ||
|
||
const { togglePrinter, state } = useTiptapStore() | ||
export const Printer = Extension.create<PrinterOptions>({ | ||
name: 'printer', | ||
addOptions() { | ||
return { | ||
...this.parent?.(), | ||
button: ({ editor, extension, t }) => ({ | ||
component: ActionButton, | ||
componentProps: { | ||
tooltip: t('editor.printer.tooltip'), | ||
action: () => togglePrinter(), | ||
icon: 'Printer', | ||
shortcutKeys: ['mod', 'P'], | ||
isActive: () => state.printer, | ||
}, | ||
}), | ||
} | ||
}, | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './Printer' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export { createContext, useContext } from './useContext' | ||
export { useTiptapStore } from './useStore' | ||
export { useHotkeys } from './useHotkeys' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import hotkeys from 'hotkeys-js' | ||
import { onBeforeUnmount } from 'vue' | ||
|
||
export const useHotkeys = (keys: string, callback: CallableFunction) => { | ||
hotkeys.filter = () => true | ||
|
||
// 绑定快捷键 | ||
const bind = () => { | ||
hotkeys(keys, (e: Event) => { | ||
e.preventDefault() | ||
callback() | ||
return false | ||
}) | ||
} | ||
|
||
// 解绑快捷键 | ||
const unbind = () => { | ||
hotkeys.unbind(keys) | ||
} | ||
|
||
// 在组件卸载时自动解绑 | ||
onBeforeUnmount(() => { | ||
unbind() | ||
}) | ||
|
||
// 返回用于手动控制的绑定和解绑函数 | ||
return { bind, unbind } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters