Skip to content

Commit 3d10e70

Browse files
authored
feat: 🎸 add parameter max length (#97)
1 parent 08f9aff commit 3d10e70

File tree

9 files changed

+162
-70
lines changed

9 files changed

+162
-70
lines changed

examples/svelte/src/App.svelte

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
let value = '';
2525
let mode = 'auto';
2626
let localeKey = 'en';
27+
let maxLength;
2728
2829
$: currentLocale = locales[localeKey];
2930
@@ -105,10 +106,16 @@
105106
<label> <input type="checkbox" bind:checked={enabled[p]} />{p}</label>
106107
{/each}
107108
</div>
109+
<div class="line">
110+
Maximum value of characters
111+
<input bind:value={maxLength} type="number" />
112+
</div>
113+
108114
<Editor
109115
{value}
110116
{mode}
111117
{plugins}
118+
{maxLength}
112119
placeholder={'Start writing with ByteMD'}
113120
locale={currentLocale.bytemd}
114121
{uploadImages}

packages/bytemd/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ editor.$on('change', (e) => {
163163
| `editorConfig` | [documentation](https://codemirror.net/doc/manual.html#config) | CodeMirror editor config |
164164
| `locale` | | i18n locale. Available locales could be found at `bytemd/lib/locales` |
165165
| `uploadImages` | `function` | Specify how to upload images |
166+
| `maxLength` | `number` | Maximum length (number of characters) of value |
166167

167168
## Style customization
168169

packages/bytemd/src/editor.svelte

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
export let locale: EditorProps['locale'];
5050
export let uploadImages: EditorProps['uploadImages'];
5151
export let overridePreview: EditorProps['overridePreview'];
52+
export let maxLength: EditorProps['maxLength'];
5253
5354
// do deep merge to support incomplete locales, use en as fallback
5455
$: mergedLocale = deepmerge(en, locale ?? {}) as BytemdLocale;
@@ -72,6 +73,7 @@
7273
let activeTab: false | 'write' | 'preview';
7374
let fullscreen = false;
7475
let sidebar: false | 'help' | 'toc' = false;
76+
let islimited = false;
7577
7678
$: styles = (() => {
7779
let edit: string;
@@ -220,7 +222,21 @@
220222
Tab: 'indentMore',
221223
'Shift-Tab': 'indentLess',
222224
});
223-
editor.on('change', (doc, change) => {
225+
226+
editor.on('beforeChange', (editor, change) => {
227+
if (maxLength && change.update) {
228+
let str = change.text.join('\n');
229+
const to = editor.indexFromPos(change.to);
230+
const from = editor.indexFromPos(change.from);
231+
const offset = editor.getValue().length + (str.length - (to - from)) - maxLength;
232+
islimited = offset >= 0
233+
if (islimited) {
234+
str = str.substr(0, str.length - offset);
235+
change.update(change.from, change.to, str.split('\n'));
236+
}
237+
}
238+
})
239+
editor.on('change', () => {
224240
dispatch('change', { value: editor.getValue() });
225241
});
226242
@@ -462,6 +478,7 @@
462478
showSync={!overridePreview && split}
463479
value={debouncedValue}
464480
{syncEnabled}
481+
{islimited}
465482
on:sync={(e) => {
466483
syncEnabled = e.detail;
467484
}}

packages/bytemd/src/locales/en.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@
2323
"words": "Words",
2424
"lines": "Lines",
2525
"sync": "Scroll sync",
26-
"top": "Scroll to top"
26+
"top": "Scroll to top",
27+
"limited": "The maximum character limit has been reached"
2728
},
2829
"action": {
2930
"h1": "Heading 1",

packages/bytemd/src/locales/zh_Hans.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@
4747
"top": "回到顶部",
4848
"sync": "同步滚动",
4949
"lines": "行数",
50-
"words": "字数"
50+
"words": "字数",
51+
"limited": "已达最大字符数限制"
5152
},
5253
"sidebar": {
5354
"shortcuts": "快捷键",

packages/bytemd/src/status.svelte

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
<script lang="ts">
22
import type { BytemdLocale } from './types';
3-
import wordCount from 'word-count';
43
import { createEventDispatcher } from 'svelte';
4+
const wordCount = require('word-count');
55
66
export let showSync: boolean;
77
export let value: string;
88
export let syncEnabled: boolean;
99
export let locale: BytemdLocale;
10+
export let islimited: boolean;
1011
1112
const dispatch = createEventDispatcher();
1213
@@ -22,6 +23,9 @@
2223
<span>
2324
{locale.status.lines}: <strong>{lines}</strong>
2425
</span>
26+
{#if islimited}
27+
<span class="danger">{locale.status.limited}</span>
28+
{/if}
2529
</div>
2630

2731
<div class="bytemd-status-right">

packages/bytemd/src/types.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,10 @@ export interface EditorProps extends ViewerProps {
157157
* If specified, the built-in viewer would not take effect.
158158
*/
159159
overridePreview?(el: HTMLElement, props: ViewerProps): void;
160+
/**
161+
* Maximum length (number of characters) of value
162+
*/
163+
maxLength?: number;
160164
}
161165

162166
export interface ViewerProps {

packages/bytemd/styles/index.scss

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,9 @@ $max-width: 800px;
264264
float: left;
265265
span {
266266
padding-left: 16px;
267+
&.danger {
268+
color: $red-500;
269+
}
267270
}
268271
strong {
269272
font-weight: 600;

0 commit comments

Comments
 (0)