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

add char counter + max length

This commit is contained in:
Théo Attali 2019-08-13 16:33:46 +02:00
parent 9796aaafef
commit acc01d1e4d
4 changed files with 290 additions and 238 deletions

File diff suppressed because one or more lines are too long

6
dist/easymde.min.js vendored

File diff suppressed because one or more lines are too long

View File

@ -1,196 +1,220 @@
.CodeMirror { .CodeMirror {
box-sizing: border-box; box-sizing: border-box;
height: auto; height: auto;
border: 1px solid #ddd; border: 1px solid #ddd;
border-bottom-left-radius: 4px; border-bottom-left-radius: 4px;
border-bottom-right-radius: 4px; border-bottom-right-radius: 4px;
padding: 10px; padding: 10px;
font: inherit; font: inherit;
z-index: 1; z-index: 1;
word-wrap: break-word; word-wrap: break-word;
} }
.CodeMirror-scroll { .CodeMirror-scroll {
cursor: text; cursor: text;
} }
.CodeMirror-fullscreen { .CodeMirror-fullscreen {
background: #fff; background: #fff;
position: fixed !important; position: fixed !important;
top: 50px; top: 50px;
left: 0; left: 0;
right: 0; right: 0;
bottom: 0; bottom: 0;
height: auto; height: auto;
z-index: 9; z-index: 9;
border-right: none !important; border-right: none !important;
border-bottom-right-radius: 0 !important; border-bottom-right-radius: 0 !important;
} }
.CodeMirror-sided { .CodeMirror-sided {
width: 50% !important; width: 50% !important;
} }
.CodeMirror-placeholder { .CodeMirror-placeholder {
opacity: .5; opacity: 0.5;
} }
.CodeMirror-focused .CodeMirror-selected { .CodeMirror-focused .CodeMirror-selected {
background: #d9d9d9; background: #d9d9d9;
} }
.editor-toolbar { .editor-toolbar {
position: relative; position: relative;
-webkit-user-select: none; -webkit-user-select: none;
-moz-user-select: none; -moz-user-select: none;
-ms-user-select: none; -ms-user-select: none;
-o-user-select: none; -o-user-select: none;
user-select: none; user-select: none;
padding: 0 10px; padding: 0 10px;
border-top: 1px solid #bbb; border-top: 1px solid #bbb;
border-left: 1px solid #bbb; border-left: 1px solid #bbb;
border-right: 1px solid #bbb; border-right: 1px solid #bbb;
border-top-left-radius: 4px; border-top-left-radius: 4px;
border-top-right-radius: 4px; border-top-right-radius: 4px;
} }
.editor-toolbar:after, .editor-toolbar:after,
.editor-toolbar:before { .editor-toolbar:before {
display: block; display: block;
content: ' '; content: " ";
height: 1px; height: 1px;
} }
.editor-toolbar:before { .editor-toolbar:before {
margin-bottom: 8px margin-bottom: 8px;
} }
.editor-toolbar:after { .editor-toolbar:after {
margin-top: 8px margin-top: 8px;
} }
.editor-toolbar.fullscreen { .editor-toolbar.fullscreen {
width: 100%; width: 100%;
height: 50px; height: 50px;
overflow-x: auto; overflow-x: auto;
overflow-y: hidden; overflow-y: hidden;
white-space: nowrap; white-space: nowrap;
padding-top: 10px; padding-top: 10px;
padding-bottom: 10px; padding-bottom: 10px;
box-sizing: border-box; box-sizing: border-box;
background: #fff; background: #fff;
border: 0; border: 0;
position: fixed; position: fixed;
top: 0; top: 0;
left: 0; left: 0;
opacity: 1; opacity: 1;
z-index: 9; z-index: 9;
} }
.editor-toolbar.fullscreen::before { .editor-toolbar.fullscreen::before {
width: 20px; width: 20px;
height: 50px; height: 50px;
background: -moz-linear-gradient(left, rgba(255, 255, 255, 1) 0%, rgba(255, 255, 255, 0) 100%); background: -moz-linear-gradient(left, rgba(255, 255, 255, 1) 0%, rgba(255, 255, 255, 0) 100%);
background: -webkit-gradient(linear, left top, right top, color-stop(0%, rgba(255, 255, 255, 1)), color-stop(100%, rgba(255, 255, 255, 0))); background: -webkit-gradient(
background: -webkit-linear-gradient(left, rgba(255, 255, 255, 1) 0%, rgba(255, 255, 255, 0) 100%); linear,
background: -o-linear-gradient(left, rgba(255, 255, 255, 1) 0%, rgba(255, 255, 255, 0) 100%); left top,
background: -ms-linear-gradient(left, rgba(255, 255, 255, 1) 0%, rgba(255, 255, 255, 0) 100%); right top,
background: linear-gradient(to right, rgba(255, 255, 255, 1) 0%, rgba(255, 255, 255, 0) 100%); color-stop(0%, rgba(255, 255, 255, 1)),
position: fixed; color-stop(100%, rgba(255, 255, 255, 0))
top: 0; );
left: 0; background: -webkit-linear-gradient(left, rgba(255, 255, 255, 1) 0%, rgba(255, 255, 255, 0) 100%);
margin: 0; background: -o-linear-gradient(left, rgba(255, 255, 255, 1) 0%, rgba(255, 255, 255, 0) 100%);
padding: 0; background: -ms-linear-gradient(left, rgba(255, 255, 255, 1) 0%, rgba(255, 255, 255, 0) 100%);
background: linear-gradient(to right, rgba(255, 255, 255, 1) 0%, rgba(255, 255, 255, 0) 100%);
position: fixed;
top: 0;
left: 0;
margin: 0;
padding: 0;
} }
.editor-toolbar.fullscreen::after { .editor-toolbar.fullscreen::after {
width: 20px; width: 20px;
height: 50px; height: 50px;
background: -moz-linear-gradient(left, rgba(255, 255, 255, 0) 0%, rgba(255, 255, 255, 1) 100%); background: -moz-linear-gradient(left, rgba(255, 255, 255, 0) 0%, rgba(255, 255, 255, 1) 100%);
background: -webkit-gradient(linear, left top, right top, color-stop(0%, rgba(255, 255, 255, 0)), color-stop(100%, rgba(255, 255, 255, 1))); background: -webkit-gradient(
background: -webkit-linear-gradient(left, rgba(255, 255, 255, 0) 0%, rgba(255, 255, 255, 1) 100%); linear,
background: -o-linear-gradient(left, rgba(255, 255, 255, 0) 0%, rgba(255, 255, 255, 1) 100%); left top,
background: -ms-linear-gradient(left, rgba(255, 255, 255, 0) 0%, rgba(255, 255, 255, 1) 100%); right top,
background: linear-gradient(to right, rgba(255, 255, 255, 0) 0%, rgba(255, 255, 255, 1) 100%); color-stop(0%, rgba(255, 255, 255, 0)),
position: fixed; color-stop(100%, rgba(255, 255, 255, 1))
top: 0; );
right: 0; background: -webkit-linear-gradient(left, rgba(255, 255, 255, 0) 0%, rgba(255, 255, 255, 1) 100%);
margin: 0; background: -o-linear-gradient(left, rgba(255, 255, 255, 0) 0%, rgba(255, 255, 255, 1) 100%);
padding: 0; background: -ms-linear-gradient(left, rgba(255, 255, 255, 0) 0%, rgba(255, 255, 255, 1) 100%);
background: linear-gradient(to right, rgba(255, 255, 255, 0) 0%, rgba(255, 255, 255, 1) 100%);
position: fixed;
top: 0;
right: 0;
margin: 0;
padding: 0;
} }
.editor-toolbar-label { .editor-toolbar-label {
position: relative; position: relative;
-webkit-user-select: none; -webkit-user-select: none;
-moz-user-select: none; -moz-user-select: none;
-ms-user-select: none; -ms-user-select: none;
-o-user-select: none; -o-user-select: none;
user-select: none; user-select: none;
padding: 0 10px; padding: 0 10px;
border-top: 1px solid #bbb; border-top: 1px solid #bbb;
border-left: 1px solid #bbb; border-left: 1px solid #bbb;
border-right: 1px solid #bbb; border-right: 1px solid #bbb;
border-top-left-radius: 4px; border-top-left-radius: 4px;
border-top-right-radius: 4px; border-top-right-radius: 4px;
display: flex; display: flex;
flex-direction: row; flex-direction: row;
align-items: baseline; align-items: baseline;
justify-content: space-between; justify-content: space-between;
} }
.editor-toolbar-label.fullscreen { .editor-toolbar-label.fullscreen {
width: 100%; width: 100%;
height: 50px; height: 50px;
overflow-x: auto; overflow-x: auto;
overflow-y: hidden; overflow-y: hidden;
white-space: nowrap; white-space: nowrap;
padding-top: 10px; padding-top: 10px;
padding-bottom: 10px; padding-bottom: 10px;
box-sizing: border-box; box-sizing: border-box;
background: #fff; background: #fff;
border: 0; border: 0;
position: fixed; position: fixed;
top: 0; top: 0;
left: 0; left: 0;
opacity: 1; opacity: 1;
z-index: 9; z-index: 9;
} }
.editor-toolbar-label.fullscreen::before { .editor-toolbar-label.fullscreen::before {
width: 20px; width: 20px;
height: 50px; height: 50px;
background: -moz-linear-gradient(left, rgba(255, 255, 255, 1) 0%, rgba(255, 255, 255, 0) 100%); background: -moz-linear-gradient(left, rgba(255, 255, 255, 1) 0%, rgba(255, 255, 255, 0) 100%);
background: -webkit-gradient(linear, left top, right top, color-stop(0%, rgba(255, 255, 255, 1)), color-stop(100%, rgba(255, 255, 255, 0))); background: -webkit-gradient(
background: -webkit-linear-gradient(left, rgba(255, 255, 255, 1) 0%, rgba(255, 255, 255, 0) 100%); linear,
background: -o-linear-gradient(left, rgba(255, 255, 255, 1) 0%, rgba(255, 255, 255, 0) 100%); left top,
background: -ms-linear-gradient(left, rgba(255, 255, 255, 1) 0%, rgba(255, 255, 255, 0) 100%); right top,
background: linear-gradient(to right, rgba(255, 255, 255, 1) 0%, rgba(255, 255, 255, 0) 100%); color-stop(0%, rgba(255, 255, 255, 1)),
position: fixed; color-stop(100%, rgba(255, 255, 255, 0))
top: 0; );
left: 0; background: -webkit-linear-gradient(left, rgba(255, 255, 255, 1) 0%, rgba(255, 255, 255, 0) 100%);
margin: 0; background: -o-linear-gradient(left, rgba(255, 255, 255, 1) 0%, rgba(255, 255, 255, 0) 100%);
padding: 0; background: -ms-linear-gradient(left, rgba(255, 255, 255, 1) 0%, rgba(255, 255, 255, 0) 100%);
background: linear-gradient(to right, rgba(255, 255, 255, 1) 0%, rgba(255, 255, 255, 0) 100%);
position: fixed;
top: 0;
left: 0;
margin: 0;
padding: 0;
} }
.editor-toolbar-label.fullscreen::after { .editor-toolbar-label.fullscreen::after {
width: 20px; width: 20px;
height: 50px; height: 50px;
background: -moz-linear-gradient(left, rgba(255, 255, 255, 0) 0%, rgba(255, 255, 255, 1) 100%); background: -moz-linear-gradient(left, rgba(255, 255, 255, 0) 0%, rgba(255, 255, 255, 1) 100%);
background: -webkit-gradient(linear, left top, right top, color-stop(0%, rgba(255, 255, 255, 0)), color-stop(100%, rgba(255, 255, 255, 1))); background: -webkit-gradient(
background: -webkit-linear-gradient(left, rgba(255, 255, 255, 0) 0%, rgba(255, 255, 255, 1) 100%); linear,
background: -o-linear-gradient(left, rgba(255, 255, 255, 0) 0%, rgba(255, 255, 255, 1) 100%); left top,
background: -ms-linear-gradient(left, rgba(255, 255, 255, 0) 0%, rgba(255, 255, 255, 1) 100%); right top,
background: linear-gradient(to right, rgba(255, 255, 255, 0) 0%, rgba(255, 255, 255, 1) 100%); color-stop(0%, rgba(255, 255, 255, 0)),
position: fixed; color-stop(100%, rgba(255, 255, 255, 1))
top: 0; );
right: 0; background: -webkit-linear-gradient(left, rgba(255, 255, 255, 0) 0%, rgba(255, 255, 255, 1) 100%);
margin: 0; background: -o-linear-gradient(left, rgba(255, 255, 255, 0) 0%, rgba(255, 255, 255, 1) 100%);
padding: 0; background: -ms-linear-gradient(left, rgba(255, 255, 255, 0) 0%, rgba(255, 255, 255, 1) 100%);
background: linear-gradient(to right, rgba(255, 255, 255, 0) 0%, rgba(255, 255, 255, 1) 100%);
position: fixed;
top: 0;
right: 0;
margin: 0;
padding: 0;
} }
.editor-toolbar-label .editor-toolbar { .editor-toolbar-label .editor-toolbar {
border: 0; border: 0;
border-radius: 0; border-radius: 0;
} }
.editor-toolbar-label .editor-label::before, .editor-toolbar-label .editor-label::before,
@ -213,199 +237,203 @@
} }
.editor-toolbar button { .editor-toolbar button {
background: transparent; background: transparent;
display: inline-block; display: inline-block;
text-align: center; text-align: center;
text-decoration: none !important; text-decoration: none !important;
width: 30px; width: 30px;
height: 30px; height: 30px;
margin: 0; margin: 0;
padding: 0; padding: 0;
border: 1px solid transparent; border: 1px solid transparent;
border-radius: 3px; border-radius: 3px;
cursor: pointer; cursor: pointer;
} }
.editor-toolbar button.active, .editor-toolbar button.active,
.editor-toolbar button:hover { .editor-toolbar button:hover {
background: #fcfcfc; background: #fcfcfc;
border-color: #95a5a6; border-color: #95a5a6;
} }
.editor-toolbar i.separator { .editor-toolbar i.separator {
display: inline-block; display: inline-block;
width: 0; width: 0;
border-left: 1px solid #d9d9d9; border-left: 1px solid #d9d9d9;
border-right: 1px solid #fff; border-right: 1px solid #fff;
color: transparent; color: transparent;
text-indent: -10px; text-indent: -10px;
margin: 0 6px; margin: 0 6px;
} }
.editor-toolbar button:after { .editor-toolbar button:after {
font-family: Arial, "Helvetica Neue", Helvetica, sans-serif; font-family: Arial, "Helvetica Neue", Helvetica, sans-serif;
font-size: 65%; font-size: 65%;
vertical-align: text-bottom; vertical-align: text-bottom;
position: relative; position: relative;
top: 2px; top: 2px;
} }
.editor-toolbar button.heading-1:after { .editor-toolbar button.heading-1:after {
content: "1"; content: "1";
} }
.editor-toolbar button.heading-2:after { .editor-toolbar button.heading-2:after {
content: "2"; content: "2";
} }
.editor-toolbar button.heading-3:after { .editor-toolbar button.heading-3:after {
content: "3"; content: "3";
} }
.editor-toolbar button.heading-bigger:after { .editor-toolbar button.heading-bigger:after {
content: "▲"; content: "▲";
} }
.editor-toolbar button.heading-smaller:after { .editor-toolbar button.heading-smaller:after {
content: "▼"; content: "▼";
} }
.editor-toolbar.disabled-for-preview button:not(.no-disable) { .editor-toolbar.disabled-for-preview button:not(.no-disable) {
opacity: .6; opacity: 0.6;
pointer-events: none; pointer-events: none;
} }
@media only screen and (max-width: 700px) { @media only screen and (max-width: 700px) {
.editor-toolbar i.no-mobile { .editor-toolbar i.no-mobile {
display: none; display: none;
} }
} }
.editor-statusbar { .editor-statusbar {
padding: 8px 10px; padding: 8px 10px;
font-size: 12px; font-size: 12px;
color: #959694; color: #959694;
text-align: right; text-align: right;
} }
.editor-statusbar span { .editor-statusbar span {
display: inline-block; display: inline-block;
min-width: 4em; min-width: 4em;
margin-left: 1em; margin-left: 1em;
} }
.editor-statusbar .lines:before { .editor-statusbar .lines:before {
content: 'lines: ' content: "lines: ";
} }
.editor-statusbar .words:before { .editor-statusbar .words:before {
content: 'words: ' content: "words: ";
} }
.editor-statusbar .characters:before { .editor-statusbar .characters:before {
content: 'characters: ' content: "characters: ";
}
.editor-statusbar .characters.max-length-exceeded .counter {
color: red;
} }
.editor-preview-full { .editor-preview-full {
position: absolute; position: absolute;
width: 100%; width: 100%;
height: 100%; height: 100%;
top: 0; top: 0;
left: 0; left: 0;
z-index: 7; z-index: 7;
overflow: auto; overflow: auto;
display: none; display: none;
box-sizing: border-box; box-sizing: border-box;
} }
.editor-preview-side { .editor-preview-side {
position: fixed; position: fixed;
bottom: 0; bottom: 0;
width: 50%; width: 50%;
top: 50px; top: 50px;
right: 0; right: 0;
z-index: 9; z-index: 9;
overflow: auto; overflow: auto;
display: none; display: none;
box-sizing: border-box; box-sizing: border-box;
border: 1px solid #ddd; border: 1px solid #ddd;
word-wrap: break-word; word-wrap: break-word;
} }
.editor-preview-active-side { .editor-preview-active-side {
display: block display: block;
} }
.editor-preview-active { .editor-preview-active {
display: block display: block;
} }
.editor-preview { .editor-preview {
padding: 10px; padding: 10px;
background: #fafafa; background: #fafafa;
} }
.editor-preview > p { .editor-preview > p {
margin-top: 0 margin-top: 0;
} }
.editor-preview pre { .editor-preview pre {
background: #eee; background: #eee;
margin-bottom: 10px; margin-bottom: 10px;
} }
.editor-preview table td, .editor-preview table td,
.editor-preview table th { .editor-preview table th {
border: 1px solid #ddd; border: 1px solid #ddd;
padding: 5px; padding: 5px;
} }
.cm-s-easymde .cm-tag { .cm-s-easymde .cm-tag {
color: #63a35c; color: #63a35c;
} }
.cm-s-easymde .cm-attribute { .cm-s-easymde .cm-attribute {
color: #795da3; color: #795da3;
} }
.cm-s-easymde .cm-string { .cm-s-easymde .cm-string {
color: #183691; color: #183691;
} }
.cm-s-easymde .cm-header-1 { .cm-s-easymde .cm-header-1 {
font-size: 200%; font-size: 200%;
line-height: 200%; line-height: 200%;
} }
.cm-s-easymde .cm-header-2 { .cm-s-easymde .cm-header-2 {
font-size: 160%; font-size: 160%;
line-height: 160%; line-height: 160%;
} }
.cm-s-easymde .cm-header-3 { .cm-s-easymde .cm-header-3 {
font-size: 125%; font-size: 125%;
line-height: 125%; line-height: 125%;
} }
.cm-s-easymde .cm-header-4 { .cm-s-easymde .cm-header-4 {
font-size: 110%; font-size: 110%;
line-height: 110%; line-height: 110%;
} }
.cm-s-easymde .cm-comment { .cm-s-easymde .cm-comment {
background: rgba(0, 0, 0, .05); background: rgba(0, 0, 0, 0.05);
border-radius: 2px; border-radius: 2px;
} }
.cm-s-easymde .cm-link { .cm-s-easymde .cm-link {
color: #7f8c8d; color: #7f8c8d;
} }
.cm-s-easymde .cm-url { .cm-s-easymde .cm-url {
color: #aab2b3; color: #aab2b3;
} }
.cm-s-easymde .cm-quote { .cm-s-easymde .cm-quote {
color: #7f8c8d; color: #7f8c8d;
font-style: italic; font-style: italic;
} }

View File

@ -1240,6 +1240,23 @@ function wordCount(data) {
return count; return count;
} }
function maxLengthCount(el, value, options) {
el.innerHTML = '';
var counter = document.createElement('span');
counter.classList.add('counter');
counter.innerHTML = String(value.length);
el.append(counter);
if (undefined !== options.maxLength && options.maxLength > 0) {
var maxLength = document.createElement('span');
maxLength.classList.add('max-length');
if (value.length > options.maxLength) {
el.classList.add('max-length-exceeded');
}
maxLength.innerHTML = String(' / ' + options.maxLength);
el.append(maxLength);
}
}
var toolbarBuiltInButtons = { var toolbarBuiltInButtons = {
'bold': { 'bold': {
name: 'bold', name: 'bold',
@ -2362,6 +2379,13 @@ 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;
}; };
} else if (name === 'characters') {
defaultValue = function (el) {
maxLengthCount(el, 0, options);
};
onUpdate = function (el) {
maxLengthCount(el, cm.getValue(), options);
};
} 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) {