Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
C
canifa_note
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Vũ Hoàng Anh
canifa_note
Commits
bd00fa79
Unverified
Commit
bd00fa79
authored
Dec 18, 2022
by
boojack
Committed by
GitHub
Dec 18, 2022
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
chore: simplify ordered list in editor (#767)
chore: simplify editor
parent
a41745c9
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
10 additions
and
30 deletions
+10
-30
MemoEditor.tsx
web/src/components/MemoEditor.tsx
+10
-30
No files found.
web/src/components/MemoEditor.tsx
View file @
bd00fa79
import
{
last
,
toLower
}
from
"lodash"
;
import
{
isNumber
,
last
,
toLower
}
from
"lodash"
;
import
React
,
{
useCallback
,
useEffect
,
useMemo
,
useRef
,
useState
}
from
"react"
;
import
{
useTranslation
}
from
"react-i18next"
;
import
{
deleteMemoResource
,
upsertMemoResource
}
from
"../helpers/api"
;
...
...
@@ -15,7 +15,7 @@ import showResourcesSelectorDialog from "./ResourcesSelectorDialog";
import
"../less/memo-editor.less"
;
const
listItemSymbolList
=
[
"- [ ] "
,
"- [x] "
,
"- [X] "
,
"* "
,
"- "
];
const
emptyOlReg
=
/^
(
[
1-9
][
0-9
]
*
)\.
$/
;
const
emptyOlReg
=
/^
(
\d
+
)\.
$/
;
const
getEditorContentCache
=
():
string
=>
{
return
storage
.
get
([
"editorContentCache"
]).
editorContentCache
??
""
;
...
...
@@ -129,7 +129,7 @@ const MemoEditor = () => {
event
.
preventDefault
();
editorRef
.
current
.
removeText
(
cursorPosition
-
rowValue
.
length
,
rowValue
.
length
);
}
else
{
// unordered
list / checked
list
// unordered
/todo
list
let
matched
=
false
;
for
(
const
listItemSymbol
of
listItemSymbolList
)
{
if
(
rowValue
.
startsWith
(
listItemSymbol
))
{
...
...
@@ -139,35 +139,15 @@ const MemoEditor = () => {
break
;
}
}
if
(
!
matched
)
{
// ordered list
const
olReg
=
/^
([
1-9
][
0-9
]
*
)\.
/
;
const
olRes
=
olReg
.
exec
(
rowValue
);
if
(
olRes
)
{
let
order
=
parseInt
(
olRes
[
1
])
+
1
;
event
.
preventDefault
();
const
contentAfterCursor
=
editorRef
.
current
.
getContent
().
slice
(
cursorPosition
);
editorRef
.
current
.
insertText
(
""
,
`\n
${
order
}
. `
);
if
(
contentAfterCursor
)
{
// correct the order
order
++
;
const
nextRows
=
contentAfterCursor
.
split
(
"
\n
"
).
slice
(
1
);
const
rowStart
=
contentBeforeCursor
.
split
(
"
\n
"
).
length
+
1
;
const
content
=
editorRef
.
current
.
getContent
().
split
(
"
\n
"
);
let
updated
=
false
;
for
(
let
i
=
0
;
i
<
nextRows
.
length
;
i
++
)
{
const
rowRes
=
olReg
.
exec
(
nextRows
[
i
]);
if
(
rowRes
)
{
content
[
rowStart
+
i
]
=
nextRows
[
i
].
replace
(
rowRes
[
1
],
(
order
+
i
).
toString
());
updated
=
true
;
}
else
{
break
;
}
}
if
(
updated
)
{
editorRef
.
current
.
setContent
(
content
.
join
(
"
\n
"
));
editorRef
.
current
.
setCursorPosition
(
cursorPosition
+
4
);
}
const
olMatchRes
=
/^
(\d
+
)\.
/
.
exec
(
rowValue
);
if
(
olMatchRes
)
{
const
order
=
parseInt
(
olMatchRes
[
1
]);
if
(
isNumber
(
order
))
{
event
.
preventDefault
();
editorRef
.
current
.
insertText
(
""
,
`\n
${
order
+
1
}
. `
);
}
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment