|
@@ -3,6 +3,33 @@ import {Plugin} from "prosemirror-state"
|
|
|
import {toggleMark, setBlockType, wrapIn} from "prosemirror-commands"
|
|
|
import {schema} from "prosemirror-schema-basic"
|
|
|
|
|
|
+// 自定义 MenuBarView 类
|
|
|
+class MenuBarView {
|
|
|
+ constructor(editorView, options) {
|
|
|
+ this.editorView = editorView;
|
|
|
+ this.options = options;
|
|
|
+ this.menuItems = []; // 存储所有的 MenuView 实例
|
|
|
+ }
|
|
|
+
|
|
|
+ addMenuItem(menuItem) {
|
|
|
+ this.menuItems.push(menuItem);
|
|
|
+ }
|
|
|
+
|
|
|
+ render() {
|
|
|
+ const element = document.createElement('div');
|
|
|
+ element.classList.add('menu-bar');
|
|
|
+ for (const menuItem of this.menuItems) {
|
|
|
+ const menuElement = menuItem.render();
|
|
|
+ element.appendChild(menuElement);
|
|
|
+ }
|
|
|
+ return element;
|
|
|
+ }
|
|
|
+
|
|
|
+ mount() {
|
|
|
+ const dom = this.render();
|
|
|
+ this.editorView.dom.parentNode.insertBefore(dom, this.editorView.dom);
|
|
|
+ }
|
|
|
+}
|
|
|
export class MenuView {
|
|
|
constructor(items, editorView) {
|
|
|
this.items = items
|
|
@@ -33,7 +60,7 @@ export class MenuView {
|
|
|
destroy() { this.dom.remove() }
|
|
|
}
|
|
|
|
|
|
-export function menuPlugin(items) {
|
|
|
+export function menuPlugin(items,editorView) {
|
|
|
return new Plugin({
|
|
|
view(editorView) {
|
|
|
let menuView = new MenuView(items, editorView)
|
|
@@ -61,17 +88,39 @@ export function icon(text, name) {
|
|
|
export const common = {
|
|
|
methods:{
|
|
|
didCreate(val){
|
|
|
- console.log(val)
|
|
|
- this.editor = val
|
|
|
- this.plugins = menuPlugin([
|
|
|
+ var menu = menuPlugin([
|
|
|
{command: toggleMark(schema.marks.strong), dom: icon("B", "strong")},
|
|
|
{command: toggleMark(schema.marks.em), dom: icon("i", "em")},
|
|
|
{command: setBlockType(schema.nodes.paragraph), dom: icon("p", "paragraph")},
|
|
|
heading(1), heading(2), heading(3),
|
|
|
{command: wrapIn(schema.nodes.blockquote), dom: icon(">", "blockquote")}
|
|
|
])
|
|
|
- console.log(this.plugins)
|
|
|
+ console.log(menu)
|
|
|
+ return menu
|
|
|
},
|
|
|
}
|
|
|
|
|
|
-}
|
|
|
+}
|
|
|
+export function addMenu(editorView){
|
|
|
+ const menuBarView = new MenuBarView(editorView, { /* options */ });
|
|
|
+ // const menuItem1 = new MenuView({ label: 'Item 1' });
|
|
|
+ // const menuItem2 = new MenuView({ label: 'Item 2' });
|
|
|
+ // menuBarView.addMenuItem(menuItem1);
|
|
|
+ // menuBarView.addMenuItem(menuItem2);
|
|
|
+ var menu = menuPlugin([
|
|
|
+ {command: toggleMark(schema.marks.strong), dom: icon("B", "strong")},
|
|
|
+ {command: toggleMark(schema.marks.em), dom: icon("i", "em")},
|
|
|
+ {command: setBlockType(schema.nodes.paragraph), dom: icon("p", "paragraph")},
|
|
|
+ heading(1), heading(2), heading(3),
|
|
|
+ {command: wrapIn(schema.nodes.blockquote), dom: icon(">", "blockquote")}
|
|
|
+ ],editorView)
|
|
|
+ console.log(menu)
|
|
|
+ menuBarView.addMenuItem(menu);
|
|
|
+ for(var i =0;i<menu.length;i++){
|
|
|
+ console.log(1)
|
|
|
+ menuBarView.addMenuItem(menu[i]);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 将 MenuBarView 挂载到编辑器视图的上方
|
|
|
+ menuBarView.mount();
|
|
|
+}
|