2
0
mirror of https://github.com/Ionaru/easy-markdown-editor synced 2025-09-24 16:40:55 -06:00

Added simple translate status bar and title toolbar. Added custom url for guide. Added onCursorActivity for update cursor position.

Signed-off-by: Dmitry Mazurov <dimabzz@gmail.com>
This commit is contained in:
Dmitry Mazurov 2020-04-08 13:50:08 +03:00
parent 228fe91a72
commit 7492bb71c8
3 changed files with 64 additions and 7 deletions

View File

@ -123,7 +123,6 @@ easyMDE.value('New input for **EasyMDE**');
- **submit_delay**: Delay before assuming that submit of the form failed and saving the text, in milliseconds. Defaults to `autosave.delay` or `10000` (10s). - **submit_delay**: Delay before assuming that submit of the form failed and saving the text, in milliseconds. Defaults to `autosave.delay` or `10000` (10s).
- **uniqueId**: You must set a unique string identifier so that EasyMDE can autosave. Something that separates this from other instances of EasyMDE elsewhere on your website. - **uniqueId**: You must set a unique string identifier so that EasyMDE can autosave. Something that separates this from other instances of EasyMDE elsewhere on your website.
- **timeFormat**: Set DateTimeFormat. More information see [DateTimeFormat instances](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/DateTimeFormat). Default `locale: en-US, format: hour:minute`. - **timeFormat**: Set DateTimeFormat. More information see [DateTimeFormat instances](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/DateTimeFormat). Default `locale: en-US, format: hour:minute`.
- **text**: Set text for autosave.
- **blockStyles**: Customize how certain buttons that style blocks of text behave. - **blockStyles**: Customize how certain buttons that style blocks of text behave.
- **bold**: Can be set to `**` or `__`. Defaults to `**`. - **bold**: Can be set to `**` or `__`. Defaults to `**`.
- **code**: Can be set to ```` ``` ```` or `~~~`. Defaults to ```` ``` ````. - **code**: Can be set to ```` ``` ```` or `~~~`. Defaults to ```` ``` ````.
@ -188,11 +187,13 @@ easyMDE.value('New input for **EasyMDE**');
- **nativeSpellcheck**: If set to `false`, disable native spell checker. Defaults to `true`. - **nativeSpellcheck**: If set to `false`, disable native spell checker. Defaults to `true`.
- **status**: If set to `false`, hide the status bar. Defaults to the array of built-in status bar items. - **status**: If set to `false`, hide the status bar. Defaults to the array of built-in status bar items.
- Optionally, you can set an array of status bar items to include, and in what order. You can even define your own custom status bar items. - Optionally, you can set an array of status bar items to include, and in what order. You can even define your own custom status bar items.
- **statusTexts**: Customize the text used to status bar.
- **styleSelectedText**: If set to `false`, remove the `CodeMirror-selectedtext` class from selected lines. Defaults to `true`. - **styleSelectedText**: If set to `false`, remove the `CodeMirror-selectedtext` class from selected lines. Defaults to `true`.
- **syncSideBySidePreviewScroll**: If set to `false`, disable syncing scroll in side by side mode. Defaults to `true`. - **syncSideBySidePreviewScroll**: If set to `false`, disable syncing scroll in side by side mode. Defaults to `true`.
- **tabSize**: If set, customize the tab size. Defaults to `2`. - **tabSize**: If set, customize the tab size. Defaults to `2`.
- **theme**: Override the theme. Defaults to `easymde`. - **theme**: Override the theme. Defaults to `easymde`.
- **toolbar**: If set to `false`, hide the toolbar. Defaults to the [array of icons](#toolbar-icons). - **toolbar**: If set to `false`, hide the toolbar. Defaults to the [array of icons](#toolbar-icons).
- **toolbarTitles**: Customize the title used to toolbar.
- **toolbarTips**: If set to `false`, disable toolbar button tips. Defaults to `true`. - **toolbarTips**: If set to `false`, disable toolbar button tips. Defaults to `true`.
@ -218,7 +219,6 @@ var editor = new EasyMDE({
minute: '2-digit', minute: '2-digit',
}, },
}, },
text: "Autosaved: "
}, },
blockStyles: { blockStyles: {
bold: "__", bold: "__",
@ -287,10 +287,18 @@ var editor = new EasyMDE({
el.innerHTML = ++this.keystrokes + " Keystrokes"; el.innerHTML = ++this.keystrokes + " Keystrokes";
}, },
}], // Another optional usage, with a custom status bar item that counts keystrokes }], // Another optional usage, with a custom status bar item that counts keystrokes
statusTexts: {
lines: "lines: ",
words: "words: ",
autosave: "Autosaved: ",
},
styleSelectedText: false, styleSelectedText: false,
syncSideBySidePreviewScroll: false, syncSideBySidePreviewScroll: false,
tabSize: 4, tabSize: 4,
toolbar: false, toolbar: false,
toobarTitles: {
"bold": {"title": "Bold"},
},
toolbarTips: false, toolbarTips: false,
}); });
``` ```

View File

@ -202,11 +202,11 @@
} }
.editor-statusbar .lines:before { .editor-statusbar .lines:before {
content: 'lines: ' content: attr(data-status-bar-before);
} }
.editor-statusbar .words:before { .editor-statusbar .words:before {
content: 'words: ' content: attr(data-status-bar-before);
} }
.editor-statusbar .characters:before { .editor-statusbar .characters:before {

View File

@ -1489,6 +1489,12 @@ var promptTexts = {
image: 'URL of the image:', image: 'URL of the image:',
}; };
var statusTexts = {
lines: 'lines: ',
words: 'words: ',
autosave: 'Autosaved: ',
};
var timeFormat = { var timeFormat = {
locale: 'en-US', locale: 'en-US',
format: { format: {
@ -1564,6 +1570,10 @@ function EasyMDE(options) {
document.getElementsByTagName('head')[0].appendChild(link); document.getElementsByTagName('head')[0].appendChild(link);
} }
if (options.markdownUrl != undefined) {
toolbarBuiltInButtons.guide.action = options.markdownUrl;
}
// Find the textarea to use // Find the textarea to use
if (options.element) { if (options.element) {
@ -1633,6 +1643,10 @@ function EasyMDE(options) {
options.promptTexts = extend({}, promptTexts, options.promptTexts || {}); options.promptTexts = extend({}, promptTexts, options.promptTexts || {});
// Merging the statusTexts, with the given options
options.statusTexts = extend({}, statusTexts, options.statusTexts || {});
// Merging the blockStyles, with the given options // Merging the blockStyles, with the given options
options.blockStyles = extend({}, blockStyles, options.blockStyles || {}); options.blockStyles = extend({}, blockStyles, options.blockStyles || {});
@ -2025,7 +2039,7 @@ EasyMDE.prototype.autosave = function () {
if (el != null && el != undefined && el != '') { if (el != null && el != undefined && el != '') {
var d = new Date(); var d = new Date();
var dd = new Intl.DateTimeFormat([this.options.autosave.timeFormat.locale, 'en-US'], this.options.autosave.timeFormat.format).format(d); var dd = new Intl.DateTimeFormat([this.options.autosave.timeFormat.locale, 'en-US'], this.options.autosave.timeFormat.format).format(d);
var save = this.options.autosave.text == undefined ? 'Autosaved: ' : this.options.autosave.text; var save = this.options.statusTexts.autosave;
el.innerHTML = save + dd; el.innerHTML = save + dd;
} }
@ -2265,9 +2279,13 @@ EasyMDE.prototype.createToolbar = function (items) {
var i; var i;
for (i = 0; i < items.length; i++) { for (i = 0; i < items.length; i++) {
if (toolbarBuiltInButtons[items[i]] != undefined) { if (toolbarBuiltInButtons[items[i]] != undefined) {
if (this.options.toolbarTitles != undefined && this.options.toolbarTitles[items[i]] != undefined) {
items[i] = extend({}, toolbarBuiltInButtons[items[i]], this.options.toolbarTitles[items[i]]);
} else {
items[i] = toolbarBuiltInButtons[items[i]]; items[i] = toolbarBuiltInButtons[items[i]];
} }
} }
}
var bar = document.createElement('div'); var bar = document.createElement('div');
bar.className = 'editor-toolbar'; bar.className = 'editor-toolbar';
@ -2372,25 +2390,31 @@ EasyMDE.prototype.createStatusbar = function (status) {
// Set up the built-in items // Set up the built-in items
var items = []; var items = [];
var i, onUpdate, defaultValue; var i, onUpdate, onCursorActivity, defaultValue, dataSet;
for (i = 0; i < status.length; i++) { for (i = 0; i < status.length; i++) {
// Reset some values // Reset some values
onUpdate = undefined; onUpdate = undefined;
onCursorActivity = undefined;
defaultValue = undefined; defaultValue = undefined;
dataSet = undefined;
// Handle if custom or not // Handle if custom or not
if (typeof status[i] === 'object') { if (typeof status[i] === 'object') {
items.push({ items.push({
className: status[i].className, className: status[i].className,
dataSet: status[i].dataSet,
defaultValue: status[i].defaultValue, defaultValue: status[i].defaultValue,
onUpdate: status[i].onUpdate, onUpdate: status[i].onUpdate,
onCursorActivity: status[i].onCursorActivity,
}); });
} else { } else {
var name = status[i]; var name = status[i];
if (name === 'words') { if (name === 'words') {
dataSet = options.statusTexts[name];
defaultValue = function (el) { defaultValue = function (el) {
el.innerHTML = wordCount(cm.getValue()); el.innerHTML = wordCount(cm.getValue());
}; };
@ -2398,6 +2422,8 @@ EasyMDE.prototype.createStatusbar = function (status) {
el.innerHTML = wordCount(cm.getValue()); el.innerHTML = wordCount(cm.getValue());
}; };
} else if (name === 'lines') { } else if (name === 'lines') {
dataSet = options.statusTexts[name];
defaultValue = function (el) { defaultValue = function (el) {
el.innerHTML = cm.lineCount(); el.innerHTML = cm.lineCount();
}; };
@ -2412,6 +2438,11 @@ EasyMDE.prototype.createStatusbar = function (status) {
var pos = cm.getCursor(); var pos = cm.getCursor();
el.innerHTML = pos.line + ':' + pos.ch; el.innerHTML = pos.line + ':' + pos.ch;
}; };
onCursorActivity = function (el) {
var pos = cm.getCursor();
el.innerHTML = pos.line + ':' + pos.ch;
};
} else if (name === 'autosave') { } else if (name === 'autosave') {
defaultValue = function (el) { defaultValue = function (el) {
if (options.autosave != undefined && options.autosave.enabled === true) { if (options.autosave != undefined && options.autosave.enabled === true) {
@ -2426,8 +2457,10 @@ EasyMDE.prototype.createStatusbar = function (status) {
items.push({ items.push({
className: name, className: name,
dataSet: dataSet,
defaultValue: defaultValue, defaultValue: defaultValue,
onUpdate: onUpdate, onUpdate: onUpdate,
onCursorActivity: onCursorActivity,
}); });
} }
} }
@ -2449,6 +2482,11 @@ EasyMDE.prototype.createStatusbar = function (status) {
el.className = item.className; el.className = item.className;
if (item.dataSet != undefined) {
el.dataset.statusBarBefore = item.dataSet;
}
// Ensure the defaultValue is a function // Ensure the defaultValue is a function
if (typeof item.defaultValue === 'function') { if (typeof item.defaultValue === 'function') {
item.defaultValue(el); item.defaultValue(el);
@ -2466,6 +2504,17 @@ EasyMDE.prototype.createStatusbar = function (status) {
} }
// Ensure the onCursorActivity is a function
if (typeof item.onCursorActivity === 'function') {
// Create a closure around the span of the current action, then execute the onCursorActivity handler
this.codemirror.on('cursorActivity', (function (el, item) {
return function () {
item.onCursorActivity(el);
};
}(el, item)));
}
// Append the item to the status bar // Append the item to the status bar
bar.appendChild(el); bar.appendChild(el);
} }