@shenjipo/mention-editor 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,31 @@
1
+ import { Editor } from '@tiptap/core';
2
+
3
+ interface MentionBridge {
4
+ items: MentionItem[];
5
+ selectedIndex: number;
6
+ select: (index: number) => void;
7
+ confirm: (index: number) => void;
8
+ setOptions: (val: Array<MentionItem>) => void;
9
+ }
10
+ interface MentionItem {
11
+ id: string;
12
+ label: string;
13
+ description?: string;
14
+ }
15
+
16
+ interface CreateMentionEditorOptions {
17
+ element: HTMLElement;
18
+ suggestionDom: HTMLElement;
19
+ bridge: MentionBridge;
20
+ content?: string;
21
+ onCommand?: (item: MentionItem) => void;
22
+ placeholder?: string;
23
+ }
24
+ interface MentionEditor {
25
+ editor: Editor;
26
+ destroy: () => void;
27
+ getJSON: () => any;
28
+ }
29
+ declare function createMentionEditor(options: CreateMentionEditorOptions): MentionEditor;
30
+
31
+ export { type MentionBridge, type MentionEditor, type MentionItem, createMentionEditor };
@@ -0,0 +1,2 @@
1
+ import{Editor as y}from"@tiptap/core";import u from"@tiptap/extension-document";import p from"@tiptap/extension-paragraph";import M from"@tiptap/extension-text";var d=[u,p,M];import E from"@tiptap/extension-mention";function s(e,t={}){let n=document.createElement(e);return Object.assign(n,t),n}function l(e){return()=>{let t=null,n=[],o=0,m;function a(){t&&(i(),t.innerHTML="",t.appendChild(e.suggestionDom))}let i=()=>{e.bridge.items=n,e.bridge.selectedIndex=o};return e.bridge.select=r=>{o=r,i()},e.bridge.confirm=r=>{m(n[r])},{onStart(r){console.log("onStart"),t=s("div",{className:"mention-menu"}),document.body.appendChild(t),f(t,r.clientRect()),n=r.items,m=r.command,o=0,a()},onUpdate(r){console.log("onUpdate"),n=r.items,o=0,a()},onKeyDown({event:r}){return r.key==="ArrowDown"?(console.log("ArrowDown"),o=(o+1)%n.length,i(),!0):r.key==="ArrowUp"?(console.log("ArrowUp"),o=(o-1+n.length)%n.length,i(),!0):r.key==="Enter"?(m(n[o]),!0):!1},onExit(){t?.remove(),t=null}}}}function f(e,t){e.style.position="absolute",e.style.left=`${t.left}px`,e.style.top=`${t.bottom+4}px`}function g(e){return{char:"/",startOfLine:!1,items:({query:t})=>e.bridge.items,command:({editor:t,range:n,props:o})=>{t.chain().focus().deleteRange(n).insertContent({type:"mention",attrs:{id:o.id,label:o.label}}).run(),e.onCommand?.(o)},render:l({suggestionDom:e.suggestionDom,bridge:e.bridge})}}function c(e){return E.configure({HTMLAttributes:{class:"mention","data-type":"mention"},suggestion:g(e),renderHTML({node:t}){return["span",{class:"mention","data-id":t.attrs.id,"data-label":t.attrs.label},t.attrs.label]}})}import b from"@tiptap/extension-placeholder";function x(e){let t=new y({element:e.element,extensions:[...d,c({onCommand:e.onCommand,suggestionDom:e.suggestionDom,bridge:e.bridge}),b.configure({placeholder:e.placeholder||"\u8BF7\u8F93\u5165...",emptyEditorClass:"is-editor-empty"})],content:e.content??""});return{editor:t,destroy:()=>t.destroy(),getJSON:()=>t.getJSON()}}export{x as createMentionEditor};
2
+ //# sourceMappingURL=index.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/editor.ts","../../src/schema/base.ts","../../src/mention/index.ts","../../src/utils/dom.ts","../../src/mention/renderer.ts","../../src/mention/suggestion.ts"],"sourcesContent":["import { Editor } from '@tiptap/core'\nimport { baseExtensions } from './schema/base'\nimport { createMentionExtension } from './mention/index'\nimport type { MentionBridge, MentionItem } from './mention/types'\nimport Placeholder from '@tiptap/extension-placeholder'\n\n\nexport interface CreateMentionEditorOptions {\n element: HTMLElement\n suggestionDom: HTMLElement,\n bridge: MentionBridge,\n content?: string\n onCommand?: (item: MentionItem) => void,\n placeholder?: string\n}\n\nexport interface MentionEditor {\n editor: Editor\n destroy: () => void\n getJSON: () => any\n}\n\nexport function createMentionEditor(options: CreateMentionEditorOptions,): MentionEditor {\n const editor = new Editor({\n element: options.element,\n extensions: [\n ...baseExtensions,\n createMentionExtension({\n onCommand: options.onCommand,\n suggestionDom: options.suggestionDom,\n bridge: options.bridge\n }),\n Placeholder.configure({\n placeholder: options.placeholder || '请输入...',\n emptyEditorClass: 'is-editor-empty'\n })\n ],\n content: options.content ?? '',\n })\n\n return {\n editor,\n destroy: () => editor.destroy(),\n getJSON: () => editor.getJSON(),\n }\n}\n","import Document from '@tiptap/extension-document'\nimport Paragraph from '@tiptap/extension-paragraph'\nimport Text from '@tiptap/extension-text'\n\nexport const baseExtensions = [\n Document,\n Paragraph,\n Text,\n]\n","import Mention from '@tiptap/extension-mention'\nimport { createSuggestion } from './suggestion'\nimport type { MentionBridge, MentionItem } from './types'\n\ninterface MentionExtensionOptions {\n onCommand?: (item: MentionItem) => void,\n suggestionDom: HTMLElement,\n bridge: MentionBridge,\n}\n\nexport function createMentionExtension(options: MentionExtensionOptions) {\n return Mention.configure({\n HTMLAttributes: {\n class: 'mention',\n 'data-type': 'mention',\n },\n suggestion: createSuggestion(options),\n renderHTML({ node }) {\n return [\n 'span',\n {\n class: 'mention',\n 'data-id': node.attrs.id,\n 'data-label': node.attrs.label,\n },\n node.attrs.label,\n ]\n }\n })\n}\n","export function createElement<K extends keyof HTMLElementTagNameMap>(\n tag: K,\n props: Partial<HTMLElementTagNameMap[K]> = {},\n): HTMLElementTagNameMap[K] {\n const el = document.createElement(tag)\n Object.assign(el, props)\n return el\n}\n","import type { MentionItem, MentionBridge } from './types'\nimport { createElement } from '../utils/dom'\n\nexport function createMentionRenderer(options: {\n suggestionDom: HTMLElement\n bridge: MentionBridge\n}) {\n return () => {\n let container: HTMLElement | null = null\n let items: MentionItem[] = []\n let selectedIndex = 0\n let command: (item: MentionItem) => void\n\n function render() {\n if (!container) return\n syncBridge()\n container.innerHTML = ''\n container!.appendChild(options.suggestionDom)\n }\n\n const syncBridge = () => {\n options.bridge.items = items\n options.bridge.selectedIndex = selectedIndex\n }\n\n options.bridge.select = (index: number) => {\n selectedIndex = index\n syncBridge()\n }\n\n options.bridge.confirm = (index: number) => {\n command(items[index])\n }\n\n return {\n onStart(props: any) {\n console.log('onStart')\n container = createElement('div', {\n className: 'mention-menu',\n })\n\n document.body.appendChild(container)\n position(container, props.clientRect())\n\n items = props.items\n command = props.command\n selectedIndex = 0\n render()\n },\n\n onUpdate(props: any) {\n console.log('onUpdate')\n items = props.items\n selectedIndex = 0\n render()\n },\n\n onKeyDown({ event }: { event: KeyboardEvent }) {\n if (event.key === 'ArrowDown') {\n console.log('ArrowDown')\n selectedIndex = (selectedIndex + 1) % items.length\n syncBridge()\n return true\n }\n if (event.key === 'ArrowUp') {\n console.log('ArrowUp')\n selectedIndex = (selectedIndex - 1 + items.length) % items.length\n syncBridge()\n return true\n }\n if (event.key === 'Enter') {\n command(items[selectedIndex])\n return true\n }\n return false\n },\n\n onExit() {\n container?.remove()\n container = null\n },\n }\n }\n}\n\nfunction position(el: HTMLElement, rect: DOMRect) {\n el.style.position = 'absolute'\n el.style.left = `${rect.left}px`\n el.style.top = `${rect.bottom + 4}px`\n}\n","import type { Editor } from '@tiptap/core'\nimport type { MentionBridge, MentionItem } from './types'\nimport { createMentionRenderer } from './renderer'\n\ninterface SuggestionOptions {\n onCommand?: (item: MentionItem) => void,\n suggestionDom: HTMLElement,\n bridge: MentionBridge,\n}\n\ninterface CommandProps {\n editor: Editor\n range: { from: number; to: number }\n props: MentionItem\n}\n\nexport function createSuggestion(options: SuggestionOptions) {\n return {\n char: '/',\n startOfLine: false,\n\n items: ({ query }: { query: string }): MentionItem[] => {\n // const list: MentionItem[] = [\n // { id: 'summary', label: '总结' },\n // { id: 'translate', label: '翻译' },\n // { id: 'rewrite', label: '改写' },\n // ]\n\n // if (!query) return list\n return options.bridge.items\n },\n\n command: ({ editor, range, props }: CommandProps) => {\n editor.chain().focus().deleteRange(range).insertContent({\n type: 'mention',\n attrs: {\n id: props.id,\n label: props.label,\n },\n }).run()\n\n options.onCommand?.(props)\n },\n\n render: createMentionRenderer({\n suggestionDom: options.suggestionDom,\n bridge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AAqD,CACrF,IAAMC,EAAS,IAAIC,EAAO,CACtB,QAASF,EAAQ,QACjB,WAAY,CACR,GAAGG,EACHC,EAAuB,CACnB,UAAWJ,EAAQ,UACnB,cAAeA,EAAQ,cACvB,OAAQA,EAAQ,MACpB,CAAC,EACDF,EAAY,UAAU,CAClB,YAAaE,EAAQ,aAAe,wBACpC,iBAAkB,iBACtB,CAAC,CACL,EACA,QAASA,EAAQ,SAAW,EAChC,CAAC,EAED,MAAO,CACH,OAAAC,EACA,QAAS,IAAMA,EAAO,QAAQ,EAC9B,QAAS,IAAMA,EAAO,QAAQ,CAClC,CACJ","names":["Editor","Document","Paragraph","Text","baseExtensions","Mention","createElement","tag","props","el","createMentionRenderer","options","container","items","selectedIndex","command","render","syncBridge","index","props","createElement","position","event","el","rect","createSuggestion","options","query","editor","range","props","createMentionRenderer","createMentionExtension","options","Mention","createSuggestion","node","Placeholder","createMentionEditor","options","editor","Editor","baseExtensions","createMentionExtension"]}
package/package.json ADDED
@@ -0,0 +1,39 @@
1
+ {
2
+ "name": "@shenjipo/mention-editor",
3
+ "version": "0.1.0",
4
+ "module": "dist/esm/index.mjs",
5
+ "exports": {
6
+ ".": {
7
+ "import": "./dist/esm/index.mjs",
8
+ "types": "./dist/esm/index.d.mts"
9
+ }
10
+ },
11
+ "files": [
12
+ "dist"
13
+ ],
14
+ "publishConfig": {
15
+ "access": "public"
16
+ },
17
+ "scripts": {
18
+ "build:watch": "tsup --watch",
19
+ "build": "tsup"
20
+ },
21
+ "keywords": [],
22
+ "author": "",
23
+ "license": "ISC",
24
+ "packageManager": "pnpm@10.10.0",
25
+ "dependencies": {
26
+ "@tiptap/core": "2.7.1",
27
+ "@tiptap/extension-document": "2.7.1",
28
+ "@tiptap/extension-text": "2.7.1",
29
+ "@tiptap/extension-paragraph": "2.7.1",
30
+ "@tiptap/extension-mention": "2.7.1",
31
+ "@tiptap/extension-placeholder": "2.7.1",
32
+ "prosemirror-state": "^1.4.3",
33
+ "prosemirror-model": "^1.21.0",
34
+ "prosemirror-view": "^1.33.7"
35
+ },
36
+ "main": "index.js",
37
+ "devDependencies": {},
38
+ "description": ""
39
+ }