This repository was archived by the owner on Jul 30, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmarkdown-editor-preview.js
More file actions
173 lines (173 loc) · 9.96 KB
/
markdown-editor-preview.js
File metadata and controls
173 lines (173 loc) · 9.96 KB
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
"use strict";
var __extends = (this && this.__extends) || (function () {
var extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
var React = require("react");
var ej2_react_richtexteditor_1 = require("@syncfusion/ej2-react-richtexteditor");
var sample_base_1 = require("../common/sample-base");
var ej2_base_1 = require("@syncfusion/ej2-base");
var Marked = require("marked");
require("./markdown-editor-preview.css");
var Preview = (function (_super) {
__extends(Preview, _super);
function Preview() {
var _this = _super !== null && _super.apply(this, arguments) || this;
// set the value to RichTextEditor
_this.value = "In RichTextEditor , you click the toolbar buttons to format the words and the changes are visible immediately. \nMarkdown is not like that. When you format the word in Markdown format, you need to add Markdown syntax to the word to indicate which words \nand phrases should look different from each other\n \nRichTextEditor supports markdown editing when the editorMode set as **markdown** and using both *keyboard interaction* and *toolbar action*, you can apply the formatting to text.\n \nWe can add our own custom formation syntax for the Markdown formation, [sample link](https://ej2.syncfusion.com/home/).\n \nThe third-party library <b>Marked</b> is used in this sample to convert markdown into HTML content";
// RichTextEditor items list
_this.items = ['Bold', 'Italic', 'StrikeThrough', '|', 'Formats', 'OrderedList', 'UnorderedList', '|', 'CreateLink', 'Image', '|',
{ tooltipText: 'Preview', template: '<button id="preview-code" class="e-tbar-btn e-control e-btn e-icon-btn">' +
'<span class="e-btn-icon e-md-preview e-icons"></span></button>' },
{ tooltipText: 'Split Editor', template: '<button id="MD_Preview" class="e-tbar-btn e-control e-btn e-icon-btn">' +
'<span class="e-btn-icon e-view-side e-icons"></span></button>' }, 'FullScreen', '|', 'Undo', 'Redo'];
//RichTextEditor ToolbarSettings
_this.toolbarSettings = {
items: _this.items
};
return _this;
}
Preview.prototype.markDownConversion = function () {
if (this.mdSplit.classList.contains('e-active')) {
var id = this.rteObj.getID() + 'html-view';
var htmlPreview = this.rteObj.element.querySelector('#' + id);
this.htmlPreview.innerHTML = Marked(this.rteObj.contentModule.getEditPanel().value);
}
};
Preview.prototype.fullPreview = function (e) {
var id = this.rteObj.getID() + 'html-preview';
this.htmlPreview = this.rteObj.element.querySelector('#' + id);
if ((this.mdsource.classList.contains('e-active') || this.mdSplit.classList.contains('e-active')) && e.mode) {
this.mdsource.classList.remove('e-active');
this.mdSplit.classList.remove('e-active');
this.mdsource.parentElement.title = 'Preview';
this.textArea.style.display = 'block';
this.textArea.style.width = '100%';
this.htmlPreview.style.display = 'none';
}
else {
this.mdsource.classList.add('e-active');
this.mdSplit.classList.add('e-active');
if (!this.htmlPreview) {
this.htmlPreview = ej2_base_1.createElement('div', { className: 'e-content' });
this.htmlPreview.id = id;
this.textArea.parentNode.appendChild(this.htmlPreview);
}
if (e.type === 'preview') {
this.textArea.style.display = 'none';
this.htmlPreview.classList.add('e-pre-source');
}
else {
this.htmlPreview.classList.remove('e-pre-source');
this.textArea.style.width = '50%';
}
this.htmlPreview.style.display = 'block';
this.htmlPreview.innerHTML = Marked(this.rteObj.contentModule.getEditPanel().value);
this.mdsource.parentElement.title = 'Code View';
}
};
Preview.prototype.rendereComplete = function () {
var _this = this;
this.textArea = this.rteObj.contentModule.getEditPanel();
this.textArea.addEventListener('keyup', function (e) { _this.markDownConversion(); });
var rteObj = this.rteObj;
this.mdsource = document.getElementById('preview-code');
this.mdsource.addEventListener('click', function (e) {
_this.fullPreview({ mode: true, type: 'preview' });
if (e.currentTarget.classList.contains('e-active')) {
rteObj.disableToolbarItem(['Bold', 'Italic', 'StrikeThrough', '|',
'Formats', 'OrderedList', 'UnorderedList', '|',
'CreateLink', 'Image', 'Undo', 'Redo']);
e.currentTarget.parentElement.nextElementSibling.classList.add('e-overlay');
}
else {
rteObj.enableToolbarItem(['Bold', 'Italic', 'StrikeThrough', '|',
'Formats', 'OrderedList', 'UnorderedList', '|',
'CreateLink', 'Image', 'Undo', 'Redo']);
e.currentTarget.parentElement.nextElementSibling.classList.remove('e-overlay');
}
});
this.mdSplit = document.getElementById('MD_Preview');
this.mdSplit.addEventListener('click', function (e) {
if (rteObj.element.classList.contains('e-rte-full-screen')) {
_this.fullPreview({ mode: true, type: '' });
}
_this.mdsource.classList.remove('e-active');
if (!rteObj.element.classList.contains('e-rte-full-screen')) {
rteObj.showFullScreen();
}
});
};
Preview.prototype.actionComplete = function (e) {
if (e.targetItem === 'Maximize' && ej2_base_1.isNullOrUndefined(e.args)) {
this.fullPreview({ mode: true, type: '' });
}
else if (!this.mdSplit.parentElement.classList.contains('e-overlay')) {
if (e.targetItem === 'Minimize') {
this.textArea.style.display = 'block';
this.textArea.style.width = '100%';
if (this.htmlPreview) {
this.htmlPreview.style.display = 'none';
}
this.mdSplit.classList.remove('e-active');
this.mdsource.classList.remove('e-active');
}
this.markDownConversion();
}
setTimeout(function () { this.rteObj.toolbarModule.refreshToolbarOverflow(); }, 400);
};
Preview.prototype.handleFullScreen = function (e) {
var leftBar;
var transformElement;
if (ej2_base_1.Browser.isDevice) {
leftBar = document.querySelector('#right-sidebar');
transformElement = document.querySelector('.sample-browser.e-view.e-content-animation');
}
else {
leftBar = document.querySelector('#left-sidebar');
transformElement = document.querySelector('#right-pane');
}
if (e.targetItem === 'Maximize') {
ej2_base_1.addClass([leftBar], ['e-close']);
ej2_base_1.removeClass([leftBar], ['e-open']);
if (!ej2_base_1.Browser.isDevice) {
transformElement.style.marginLeft = '0px';
}
transformElement.style.transform = 'inherit';
}
else if (e.targetItem === 'Minimize') {
ej2_base_1.removeClass([leftBar], ['e-close']);
if (!ej2_base_1.Browser.isDevice) {
ej2_base_1.addClass([leftBar], ['e-open']);
transformElement.style.marginLeft = leftBar.offsetWidth + 'px';
}
transformElement.style.transform = 'translateX(0px)';
}
};
Preview.prototype.render = function () {
var _this = this;
return (React.createElement("div", { id: "dropdowndefault", className: 'control-pane' },
React.createElement("div", { className: 'control-section', id: "rtePreview" },
React.createElement("div", { className: "content-wrapper" },
React.createElement(ej2_react_richtexteditor_1.RichTextEditorComponent, { id: "markdownPreview", actionBegin: this.handleFullScreen.bind(this), actionComplete: this.actionComplete.bind(this), editorMode: 'Markdown', height: '300px', ref: function (richtexteditor) { _this.rteObj = richtexteditor; }, value: this.value, toolbarSettings: this.toolbarSettings },
React.createElement(ej2_react_richtexteditor_1.Inject, { services: [ej2_react_richtexteditor_1.MarkdownEditor, ej2_react_richtexteditor_1.Toolbar, ej2_react_richtexteditor_1.Image, ej2_react_richtexteditor_1.Link, ej2_react_richtexteditor_1.QuickToolbar] })))),
React.createElement("div", { id: "action-description" },
React.createElement("p", null, "This sample demonstrates how to preview markdown changes in rich text editor. Type or edit the display text, and apply format to view the preview of markdown. You can preview the markdown changes immediately in the preview area.")),
React.createElement("div", { id: "description" },
React.createElement("p", null,
"The rich text editor allows you to preview markdown changes immediately using ",
React.createElement("code", null, "preview"),
". The third-party library ",
React.createElement("code", null, "Marked"),
" is used in this sample to convert markdown into HTML content."))));
};
return Preview;
}(sample_base_1.SampleBase));
exports.Preview = Preview;