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
5a723f00
Commit
5a723f00
authored
Jan 15, 2024
by
Steven
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
chore: split editor keydown handler
parent
728a9705
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
46 additions
and
45 deletions
+46
-45
handlers.tsx
web/src/components/MemoEditor/handlers.tsx
+43
-0
index.tsx
web/src/components/MemoEditor/index.tsx
+3
-45
No files found.
web/src/components/MemoEditor/handlers.tsx
0 → 100644
View file @
5a723f00
import
{
EditorRefActions
}
from
"./Editor"
;
export
const
handleEditorKeydownWithMarkdownShortcuts
=
(
event
:
React
.
KeyboardEvent
,
editorRef
:
EditorRefActions
)
=>
{
if
(
event
.
key
===
"b"
)
{
const
boldDelimiter
=
"**"
;
event
.
preventDefault
();
styleHighlightedText
(
editorRef
,
boldDelimiter
);
}
else
if
(
event
.
key
===
"i"
)
{
const
italicsDelimiter
=
"*"
;
event
.
preventDefault
();
styleHighlightedText
(
editorRef
,
italicsDelimiter
);
}
else
if
(
event
.
key
===
"k"
)
{
event
.
preventDefault
();
hyperlinkHighlightedText
(
editorRef
);
}
};
const
styleHighlightedText
=
(
editor
:
EditorRefActions
,
delimiter
:
string
)
=>
{
const
cursorPosition
=
editor
.
getCursorPosition
();
const
selectedContent
=
editor
.
getSelectedContent
();
if
(
selectedContent
.
startsWith
(
delimiter
)
&&
selectedContent
.
endsWith
(
delimiter
))
{
editor
.
insertText
(
selectedContent
.
slice
(
delimiter
.
length
,
-
delimiter
.
length
));
const
newContentLength
=
selectedContent
.
length
-
delimiter
.
length
*
2
;
editor
.
setCursorPosition
(
cursorPosition
,
cursorPosition
+
newContentLength
);
}
else
{
editor
.
insertText
(
`
${
delimiter
}${
selectedContent
}${
delimiter
}
`
);
editor
.
setCursorPosition
(
cursorPosition
+
delimiter
.
length
,
cursorPosition
+
delimiter
.
length
+
selectedContent
.
length
);
}
};
const
hyperlinkHighlightedText
=
(
editor
:
EditorRefActions
,
url
?:
string
)
=>
{
const
cursorPosition
=
editor
.
getCursorPosition
();
const
selectedContent
=
editor
.
getSelectedContent
();
const
blankURL
=
"url"
;
url
=
url
??
blankURL
;
editor
.
insertText
(
`[
${
selectedContent
}
](
${
url
}
)`
);
if
(
url
===
blankURL
)
{
const
newCursorStart
=
cursorPosition
+
selectedContent
.
length
+
3
;
editor
.
setCursorPosition
(
newCursorStart
,
newCursorStart
+
url
.
length
);
}
};
web/src/components/MemoEditor/index.tsx
View file @
5a723f00
...
...
@@ -6,7 +6,6 @@ import { useTranslation } from "react-i18next";
import
useLocalStorage
from
"react-use/lib/useLocalStorage"
;
import
{
memoServiceClient
}
from
"@/grpcweb"
;
import
{
TAB_SPACE_WIDTH
,
UNKNOWN_ID
}
from
"@/helpers/consts"
;
import
{
isValidUrl
}
from
"@/helpers/utils"
;
import
{
useGlobalStore
,
useResourceStore
}
from
"@/store/module"
;
import
{
useMemoStore
,
useUserStore
}
from
"@/store/v1"
;
import
{
MemoRelation
,
MemoRelation_Type
}
from
"@/types/proto/api/v2/memo_relation_service"
;
...
...
@@ -24,6 +23,7 @@ import TagSelector from "./ActionButton/TagSelector";
import
Editor
,
{
EditorRefActions
}
from
"./Editor"
;
import
RelationListView
from
"./RelationListView"
;
import
ResourceListView
from
"./ResourceListView"
;
import
{
handleEditorKeydownWithMarkdownShortcuts
}
from
"./handlers"
;
interface
Props
{
className
?:
string
;
...
...
@@ -116,20 +116,8 @@ const MemoEditor = (props: Props) => {
handleSaveBtnClick
();
return
;
}
if
(
event
.
key
===
"b"
)
{
const
boldDelimiter
=
"**"
;
event
.
preventDefault
();
styleHighlightedText
(
editorRef
.
current
,
boldDelimiter
);
}
if
(
event
.
key
===
"i"
)
{
const
italicsDelimiter
=
"*"
;
event
.
preventDefault
();
styleHighlightedText
(
editorRef
.
current
,
italicsDelimiter
);
}
if
(
event
.
key
===
"k"
)
{
event
.
preventDefault
();
hyperlinkHighlightedTest
(
editorRef
.
current
);
}
handleEditorKeydownWithMarkdownShortcuts
(
event
,
editorRef
.
current
);
}
if
(
event
.
key
===
"Tab"
)
{
event
.
preventDefault
();
...
...
@@ -254,9 +242,6 @@ const MemoEditor = (props: Props) => {
if
(
event
.
clipboardData
&&
event
.
clipboardData
.
files
.
length
>
0
)
{
event
.
preventDefault
();
await
uploadMultiFiles
(
event
.
clipboardData
.
files
);
}
else
if
(
editorRef
.
current
!=
null
&&
isValidUrl
(
event
.
clipboardData
.
getData
(
"Text"
)))
{
event
.
preventDefault
();
hyperlinkHighlightedTest
(
editorRef
.
current
,
event
.
clipboardData
.
getData
(
"Text"
));
}
};
...
...
@@ -357,33 +342,6 @@ const MemoEditor = (props: Props) => {
editorRef
.
current
?.
focus
();
};
const
styleHighlightedText
=
(
editor
:
EditorRefActions
,
delimiter
:
string
)
=>
{
const
cursorPosition
=
editor
.
getCursorPosition
();
const
selectedContent
=
editor
.
getSelectedContent
();
if
(
selectedContent
.
startsWith
(
delimiter
)
&&
selectedContent
.
endsWith
(
delimiter
))
{
editor
.
insertText
(
selectedContent
.
slice
(
delimiter
.
length
,
-
delimiter
.
length
));
const
newContentLength
=
selectedContent
.
length
-
delimiter
.
length
*
2
;
editor
.
setCursorPosition
(
cursorPosition
,
cursorPosition
+
newContentLength
);
}
else
{
editor
.
insertText
(
`
${
delimiter
}${
selectedContent
}${
delimiter
}
`
);
editor
.
setCursorPosition
(
cursorPosition
+
delimiter
.
length
,
cursorPosition
+
delimiter
.
length
+
selectedContent
.
length
);
}
};
const
hyperlinkHighlightedTest
=
(
editor
:
EditorRefActions
,
url
?:
string
)
=>
{
const
cursorPosition
=
editor
.
getCursorPosition
();
const
selectedContent
=
editor
.
getSelectedContent
();
const
blankURL
=
"url"
;
url
=
url
??
blankURL
;
editor
.
insertText
(
`[
${
selectedContent
}
](
${
url
}
)`
);
if
(
url
===
blankURL
)
{
const
newCursorStart
=
cursorPosition
+
selectedContent
.
length
+
3
;
editor
.
setCursorPosition
(
newCursorStart
,
newCursorStart
+
url
.
length
);
}
};
const
editorConfig
=
useMemo
(
()
=>
({
className
:
editorClassName
??
""
,
...
...
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