Skip to content

Commit

Permalink
💄 改进字数统计
Browse files Browse the repository at this point in the history
  • Loading branch information
stevapple committed Apr 9, 2020
1 parent 2845b17 commit 9bdc88f
Show file tree
Hide file tree
Showing 5 changed files with 7 additions and 6 deletions.
5 changes: 3 additions & 2 deletions src/ts/counter/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ export class Counter {
this.element = document.createElement("div");
this.element.className = "vditor-counter";

this.render(0, vditor.options.counter);
this.render("", vditor.options.counter);

}

public render(length: number, counter: number) {
public render(text: string, counter: number) {
const length = text.endsWith("\n") ? text.length - 1 : text.length;
if (length > counter) {
this.element.className = "vditor-counter vditor-counter--error";
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/ts/ir/process.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export const processAfterRender = (vditor: IVditor, options = {
}

if (vditor.options.counter > 0) {
vditor.counter.render(text.length, vditor.options.counter);
vditor.counter.render(text, vditor.options.counter);
}

if (vditor.options.cache.enable) {
Expand Down
2 changes: 1 addition & 1 deletion src/ts/sv/inputEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export const inputEvent = (vditor: IVditor, options = {
enableInput: true,
}) => {
if (vditor.options.counter > 0) {
vditor.counter.render(getMarkdown(vditor).length, vditor.options.counter);
vditor.counter.render(getMarkdown(vditor), vditor.options.counter);
}
if (typeof vditor.options.input === "function" && options.enableInput) {
vditor.options.input(getMarkdown(vditor),
Expand Down
2 changes: 1 addition & 1 deletion src/ts/wysiwyg/afterRenderEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export const afterRenderEvent = (vditor: IVditor, options = {
}

if (vditor.options.counter > 0) {
vditor.counter.render(text.length, vditor.options.counter);
vditor.counter.render(text, vditor.options.counter);
}

if (vditor.options.cache.enable) {
Expand Down
2 changes: 1 addition & 1 deletion types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ interface IVditor {
};
counter?: {
element: HTMLElement
render(length: number, counter: number): void,
render(text: string, counter: number): void,
};
resize?: {
element: HTMLElement,
Expand Down

0 comments on commit 9bdc88f

Please sign in to comment.