forked from halo-dev/halo
-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'upstream/main'
- Loading branch information
Showing
17 changed files
with
377 additions
and
23 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
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
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
120 changes: 120 additions & 0 deletions
120
ui/packages/editor/src/extensions/format-brush/index.ts
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,120 @@ | ||
import ToolbarItem from "@/components/toolbar/ToolbarItem.vue"; | ||
import { i18n } from "@/locales"; | ||
import { CoreEditor, Extension, Plugin, PluginKey } from "@/tiptap"; | ||
import { markRaw } from "vue"; | ||
import BxsBrushAlt from "~icons/bxs/brush-alt"; | ||
import { getMarksByFirstTextNode, setMarks } from "./util"; | ||
|
||
declare module "@/tiptap" { | ||
interface Commands<ReturnType> { | ||
formatBrush: { | ||
copyFormatBrush: () => ReturnType; | ||
pasteFormatBrush: () => ReturnType; | ||
}; | ||
} | ||
} | ||
|
||
export interface FormatBrushStore { | ||
formatBrush: boolean; | ||
formatBrushMarks: any[]; | ||
} | ||
|
||
const formatBrush = Extension.create<any, FormatBrushStore>({ | ||
addOptions() { | ||
return { | ||
...this.parent?.(), | ||
getToolbarItems({ editor }: { editor: CoreEditor }) { | ||
const formatBrush = | ||
editor.view.dom.classList.contains("format-brush-mode"); | ||
return { | ||
priority: 25, | ||
component: markRaw(ToolbarItem), | ||
props: { | ||
editor, | ||
isActive: formatBrush, | ||
icon: markRaw(BxsBrushAlt), | ||
title: formatBrush | ||
? i18n.global.t( | ||
"editor.extensions.format_brush.toolbar_item.cancel" | ||
) | ||
: i18n.global.t( | ||
"editor.extensions.format_brush.toolbar_item.title" | ||
), | ||
action: () => { | ||
if (formatBrush) { | ||
editor.commands.pasteFormatBrush(); | ||
} else { | ||
editor.commands.copyFormatBrush(); | ||
} | ||
}, | ||
}, | ||
}; | ||
}, | ||
}; | ||
}, | ||
|
||
addCommands() { | ||
return { | ||
copyFormatBrush: | ||
() => | ||
({ state }) => { | ||
const markRange = getMarksByFirstTextNode(state); | ||
this.storage.formatBrushMarks = markRange; | ||
this.storage.formatBrush = true; | ||
this.editor.view.dom.classList.add("format-brush-mode"); | ||
return true; | ||
}, | ||
pasteFormatBrush: () => () => { | ||
this.storage.formatBrushMarks = []; | ||
this.storage.formatBrush = false; | ||
this.editor.view.dom.classList.remove("format-brush-mode"); | ||
return true; | ||
}, | ||
}; | ||
}, | ||
|
||
addStorage() { | ||
return { | ||
formatBrush: false, | ||
formatBrushMarks: [], | ||
}; | ||
}, | ||
|
||
addProseMirrorPlugins() { | ||
const storage = this.storage; | ||
const editor = this.editor; | ||
return [ | ||
new Plugin({ | ||
key: new PluginKey("formatBrushPlugin"), | ||
props: { | ||
handleDOMEvents: { | ||
mouseup(view) { | ||
if (!storage.formatBrush) { | ||
return; | ||
} | ||
editor | ||
.chain() | ||
.command(({ tr }) => { | ||
setMarks(view.state, storage.formatBrushMarks, tr); | ||
return true; | ||
}) | ||
.pasteFormatBrush() | ||
.run(); | ||
}, | ||
}, | ||
}, | ||
}), | ||
]; | ||
}, | ||
|
||
addKeyboardShortcuts() { | ||
return { | ||
"Shift-Mod-c": () => { | ||
this.editor.commands.copyFormatBrush(); | ||
return true; | ||
}, | ||
}; | ||
}, | ||
}); | ||
|
||
export default formatBrush; |
Oops, something went wrong.