-
Notifications
You must be signed in to change notification settings - Fork 342
/
line-height.ts
93 lines (74 loc) · 2.02 KB
/
line-height.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/*!
* Jodit Editor (https://xdsoft.net/jodit/)
* Released under MIT see LICENSE.txt in the project root for license information.
* Copyright (c) 2013-2024 Valeriy Chupurnov. All rights reserved. https://xdsoft.net
*/
/**
* [[include:plugins/line-height/README.md]]
* @packageDocumentation
* @module plugins/line-height
*/
import type { IJodit } from 'jodit/types';
import { autobind } from 'jodit/core/decorators';
import { Dom } from 'jodit/core/dom/dom';
import { extendLang, pluginSystem } from 'jodit/core/global';
import { css } from 'jodit/core/helpers';
import { Plugin } from 'jodit/core/plugin';
import './config';
import * as langs from './langs';
export class lineHeight extends Plugin {
override buttons: Plugin['buttons'] = [
{
name: 'lineHeight',
group: 'font'
}
];
constructor(jodit: IJodit) {
super(jodit);
extendLang(langs);
}
protected afterInit(jodit: IJodit): void {
css(jodit.editor, {
lineHeight: jodit.o.defaultLineHeight
});
jodit.registerCommand('applyLineHeight', this.applyLineHeight);
}
@autobind
private applyLineHeight(ignore: string, ignoreA: any, value: any): void {
const { s, createInside: c, editor: root, o } = this.j;
if (!s.isFocused()) {
s.focus();
}
s.save();
let addStyle: boolean | undefined;
const apply = (node: Node): void => {
let parentBlock = Dom.closest(node, Dom.isBlock, root);
if (!parentBlock) {
parentBlock = Dom.wrap(node, o.enter, c);
}
const previousValue = css(parentBlock, 'lineHeight');
if (addStyle === undefined) {
addStyle = previousValue.toString() !== value.toString();
}
css(parentBlock, 'lineHeight', addStyle ? value : null);
};
try {
if (s.isCollapsed()) {
const fake = c.fake();
s.insertNode(fake, false, false);
apply(fake);
Dom.safeRemove(fake);
} else {
s.eachSelection(apply);
}
} finally {
s.restore();
}
}
protected beforeDestruct(jodit: IJodit): void {
css(jodit.editor, {
lineHeight: null
});
}
}
pluginSystem.add('lineHeight', lineHeight);