1
1
import "./styles/index.scss" ;
2
2
3
- import type { BlockContext , CommandFn , EventContext } from "doc-editor-core" ;
3
+ import type {
4
+ BlockContext ,
5
+ CommandFn ,
6
+ CopyContext ,
7
+ EventContext ,
8
+ PasteContext ,
9
+ } from "doc-editor-core" ;
4
10
import type { EditorKit } from "doc-editor-core" ;
5
- import { BlockPlugin , EDITOR_EVENT } from "doc-editor-core" ;
11
+ import { BlockPlugin , EDITOR_EVENT , isHTMLElement } from "doc-editor-core" ;
6
12
import type { RenderElementProps } from "doc-editor-delta" ;
13
+ import type { BlockElement } from "doc-editor-delta" ;
7
14
import { Transforms } from "doc-editor-delta" ;
8
- import { getUniqueId , KEYBOARD } from "doc-editor-utils" ;
15
+ import { getId , getUniqueId , KEYBOARD } from "doc-editor-utils" ;
9
16
import { isObject } from "doc-editor-utils" ;
10
17
import { getBlockAttributes , getBlockNode } from "doc-editor-utils" ;
11
18
import {
@@ -20,6 +27,7 @@ import {
20
27
import { setBlockNode , setUnBlockNode } from "doc-editor-utils" ;
21
28
import type { KeyboardEvent } from "react" ;
22
29
30
+ import { applyLineMarker } from "../clipboard/utils/apply" ;
23
31
import { H1 , H2 , H3 , HEADING_KEY } from "./types" ;
24
32
25
33
export class HeadingPlugin extends BlockPlugin {
@@ -125,4 +133,30 @@ export class HeadingPlugin extends BlockPlugin {
125
133
return context . stop ( ) ;
126
134
}
127
135
} ;
136
+
137
+ public serialize ( context : CopyContext ) : void {
138
+ const element = context . node as BlockElement ;
139
+ const heading = element [ HEADING_KEY ] ;
140
+ if ( ! heading ) return void 0 ;
141
+ const id = heading . id ;
142
+ const type = heading . type ;
143
+ const node = document . createElement ( type ) ;
144
+ node . id = id ;
145
+ node . setAttribute ( "data-type" , HEADING_KEY ) ;
146
+ node . appendChild ( context . html ) ;
147
+ context . html = node ;
148
+ }
149
+
150
+ public deserialize ( context : PasteContext ) : void {
151
+ const { nodes, html } = context ;
152
+ if ( ! isHTMLElement ( html ) ) return void 0 ;
153
+ const tagName = html . tagName . toLocaleLowerCase ( ) ;
154
+ if ( tagName . startsWith ( "h" ) && tagName . length === 2 ) {
155
+ let level = Number ( tagName . replace ( "h" , "" ) ) ;
156
+ if ( level <= 0 || level > 3 ) level = 3 ;
157
+ context . nodes = applyLineMarker ( this . editor , nodes , {
158
+ [ HEADING_KEY ] : { type : `h${ level } ` , id : getId ( ) } ,
159
+ } ) ;
160
+ }
161
+ }
128
162
}
0 commit comments