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
8bba7f70
Commit
8bba7f70
authored
Apr 27, 2024
by
Steven
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
chore: update IME mode checks
parent
f9942002
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
34 additions
and
50 deletions
+34
-50
index.tsx
web/src/components/MemoEditor/Editor/index.tsx
+34
-4
index.ts
web/src/components/MemoEditor/hooks/index.ts
+0
-3
useAutoComplete.ts
web/src/components/MemoEditor/hooks/useAutoComplete.ts
+0
-43
No files found.
web/src/components/MemoEditor/Editor/index.tsx
View file @
8bba7f70
import
classNames
from
"classnames"
;
import
classNames
from
"classnames"
;
import
{
forwardRef
,
ReactNode
,
useCallback
,
useEffect
,
useImperativeHandle
,
useRef
}
from
"react"
;
import
{
last
}
from
"lodash-es"
;
import
{
useAutoComplete
}
from
"../hooks"
;
import
{
forwardRef
,
ReactNode
,
useCallback
,
useEffect
,
useImperativeHandle
,
useRef
,
useState
}
from
"react"
;
import
{
NodeType
,
OrderedListNode
,
TaskListNode
,
UnorderedListNode
}
from
"@/types/node"
;
import
TagSuggestions
from
"./TagSuggestions"
;
import
TagSuggestions
from
"./TagSuggestions"
;
export
interface
EditorRefActions
{
export
interface
EditorRefActions
{
...
@@ -30,6 +31,7 @@ interface Props {
...
@@ -30,6 +31,7 @@ interface Props {
const
Editor
=
forwardRef
(
function
Editor
(
props
:
Props
,
ref
:
React
.
ForwardedRef
<
EditorRefActions
>
)
{
const
Editor
=
forwardRef
(
function
Editor
(
props
:
Props
,
ref
:
React
.
ForwardedRef
<
EditorRefActions
>
)
{
const
{
className
,
initialContent
,
placeholder
,
onPaste
,
onContentChange
:
handleContentChangeCallback
}
=
props
;
const
{
className
,
initialContent
,
placeholder
,
onPaste
,
onContentChange
:
handleContentChangeCallback
}
=
props
;
const
[
isInIME
,
setIsInIME
]
=
useState
(
false
);
const
editorRef
=
useRef
<
HTMLTextAreaElement
>
(
null
);
const
editorRef
=
useRef
<
HTMLTextAreaElement
>
(
null
);
useEffect
(()
=>
{
useEffect
(()
=>
{
...
@@ -133,8 +135,6 @@ const Editor = forwardRef(function Editor(props: Props, ref: React.ForwardedRef<
...
@@ -133,8 +135,6 @@ const Editor = forwardRef(function Editor(props: Props, ref: React.ForwardedRef<
},
},
};
};
useAutoComplete
(
editorActions
);
useImperativeHandle
(
ref
,
()
=>
editorActions
,
[]);
useImperativeHandle
(
ref
,
()
=>
editorActions
,
[]);
const
updateEditorHeight
=
()
=>
{
const
updateEditorHeight
=
()
=>
{
...
@@ -149,6 +149,33 @@ const Editor = forwardRef(function Editor(props: Props, ref: React.ForwardedRef<
...
@@ -149,6 +149,33 @@ const Editor = forwardRef(function Editor(props: Props, ref: React.ForwardedRef<
updateEditorHeight
();
updateEditorHeight
();
},
[]);
},
[]);
const
handleEditorKeyDown
=
(
event
:
React
.
KeyboardEvent
<
HTMLTextAreaElement
>
)
=>
{
if
(
event
.
key
===
"Enter"
&&
!
isInIME
)
{
const
cursorPosition
=
editorActions
.
getCursorPosition
();
const
prevContent
=
editorActions
.
getContent
().
substring
(
0
,
cursorPosition
);
const
lastNode
=
last
(
window
.
parse
(
prevContent
));
if
(
!
lastNode
)
{
return
;
}
let
insertText
=
""
;
if
(
lastNode
.
type
===
NodeType
.
TASK_LIST
)
{
const
{
complete
}
=
lastNode
.
value
as
TaskListNode
;
insertText
=
complete
?
"- [x] "
:
"- [ ] "
;
}
else
if
(
lastNode
.
type
===
NodeType
.
UNORDERED_LIST
)
{
const
{
symbol
}
=
lastNode
.
value
as
UnorderedListNode
;
insertText
=
`
${
symbol
}
`
;
}
else
if
(
lastNode
.
type
===
NodeType
.
ORDERED_LIST
)
{
const
{
number
}
=
lastNode
.
value
as
OrderedListNode
;
insertText
=
`
${
Number
(
number
)
+
1
}
. `
;
}
if
(
insertText
)
{
editorActions
.
insertText
(
`\n
${
insertText
}
`
);
event
.
preventDefault
();
}
}
};
return
(
return
(
<
div
<
div
className=
{
classNames
(
className=
{
classNames
(
...
@@ -163,6 +190,9 @@ const Editor = forwardRef(function Editor(props: Props, ref: React.ForwardedRef<
...
@@ -163,6 +190,9 @@ const Editor = forwardRef(function Editor(props: Props, ref: React.ForwardedRef<
ref=
{
editorRef
}
ref=
{
editorRef
}
onPaste=
{
onPaste
}
onPaste=
{
onPaste
}
onInput=
{
handleEditorInput
}
onInput=
{
handleEditorInput
}
onKeyDown=
{
handleEditorKeyDown
}
onCompositionStart=
{
()
=>
setIsInIME
(
true
)
}
onCompositionEnd=
{
()
=>
setTimeout
(()
=>
setIsInIME
(
false
))
}
></
textarea
>
></
textarea
>
<
TagSuggestions
editorRef=
{
editorRef
}
editorActions=
{
ref
}
/>
<
TagSuggestions
editorRef=
{
editorRef
}
editorActions=
{
ref
}
/>
</
div
>
</
div
>
...
...
web/src/components/MemoEditor/hooks/index.ts
deleted
100644 → 0
View file @
f9942002
import
useAutoComplete
from
"./useAutoComplete"
;
export
{
useAutoComplete
};
web/src/components/MemoEditor/hooks/useAutoComplete.ts
deleted
100644 → 0
View file @
f9942002
import
{
last
}
from
"lodash-es"
;
import
{
useEffect
}
from
"react"
;
import
{
NodeType
,
OrderedListNode
,
TaskListNode
,
UnorderedListNode
}
from
"@/types/node"
;
import
{
EditorRefActions
}
from
"../Editor"
;
const
useAutoComplete
=
(
actions
:
EditorRefActions
)
=>
{
useEffect
(()
=>
{
const
editor
=
actions
.
getEditor
();
if
(
!
editor
)
return
;
editor
.
addEventListener
(
"keydown"
,
(
event
)
=>
{
if
(
event
.
key
===
"Enter"
)
{
if
(
event
.
isComposing
)
{
return
;
}
const
cursorPosition
=
actions
.
getCursorPosition
();
const
prevContent
=
actions
.
getContent
().
substring
(
0
,
cursorPosition
);
const
lastNode
=
last
(
window
.
parse
(
prevContent
));
if
(
!
lastNode
)
{
return
;
}
let
insertText
=
""
;
if
(
lastNode
.
type
===
NodeType
.
TASK_LIST
)
{
const
{
complete
}
=
lastNode
.
value
as
TaskListNode
;
insertText
=
complete
?
"- [x] "
:
"- [ ] "
;
}
else
if
(
lastNode
.
type
===
NodeType
.
UNORDERED_LIST
)
{
const
{
symbol
}
=
lastNode
.
value
as
UnorderedListNode
;
insertText
=
`
${
symbol
}
`
;
}
else
if
(
lastNode
.
type
===
NodeType
.
ORDERED_LIST
)
{
const
{
number
}
=
lastNode
.
value
as
OrderedListNode
;
insertText
=
`
${
Number
(
number
)
+
1
}
. `
;
}
if
(
insertText
)
{
actions
.
insertText
(
`\n
${
insertText
}
`
);
event
.
preventDefault
();
}
}
});
},
[]);
};
export
default
useAutoComplete
;
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