Skip to content

Commit

Permalink
fix: refine max length prop
Browse files Browse the repository at this point in the history
  • Loading branch information
pd4d10 committed Jul 8, 2021
1 parent 634b94e commit 657b4d3
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 21 deletions.
19 changes: 2 additions & 17 deletions packages/bytemd/src/editor.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
export let locale: EditorProps['locale']
export let uploadImages: EditorProps['uploadImages']
export let overridePreview: EditorProps['overridePreview']
export let maxLength: EditorProps['maxLength']
export let maxLength: NonNullable<EditorProps['maxLength']> = Infinity
$: mergedLocale = { ...en, ...locale }
const dispatch = createEventDispatcher()
Expand All @@ -65,7 +65,6 @@
let activeTab: false | 'write' | 'preview'
let fullscreen = false
let sidebar: false | 'help' | 'toc' = false
let islimited = false
$: styles = (() => {
let edit: string
Expand Down Expand Up @@ -215,20 +214,6 @@
'Shift-Tab': 'indentLess',
})
editor.on('beforeChange', (editor, change) => {
if (maxLength && change.update) {
let str = change.text.join('\n')
const to = editor.indexFromPos(change.to)
const from = editor.indexFromPos(change.from)
const offset =
editor.getValue().length + (str.length - (to - from)) - maxLength
islimited = offset >= 0
if (islimited) {
str = str.substr(0, str.length - offset)
change.update(change.from, change.to, str.split('\n'))
}
}
})
editor.on('change', () => {
dispatch('change', { value: editor.getValue() })
})
Expand Down Expand Up @@ -471,7 +456,7 @@
showSync={!overridePreview && split}
value={debouncedValue}
{syncEnabled}
{islimited}
islimited={value.length > maxLength}
on:sync={(e) => {
syncEnabled = e.detail
}}
Expand Down
2 changes: 1 addition & 1 deletion packages/bytemd/src/status.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
{locale.lines}: <strong>{lines}</strong>
</span>
{#if islimited}
<span class="danger">{locale.limited}</span>
<span class="bytemd-status-error">{locale.limited}</span>
{/if}
</div>

Expand Down
6 changes: 3 additions & 3 deletions packages/bytemd/styles/index.scss
Original file line number Diff line number Diff line change
Expand Up @@ -264,9 +264,6 @@ $max-width: 800px;
float: left;
span {
padding-left: 16px;
&.danger {
color: $red-500;
}
}
strong {
font-weight: 600;
Expand All @@ -287,6 +284,9 @@ $max-width: 800px;
margin-right: 3px; // CSS reset
}
}
&-error {
color: $red-500;
}
}

&-fullscreen {
Expand Down

0 comments on commit 657b4d3

Please sign in to comment.