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
6d10251c
Commit
6d10251c
authored
Apr 08, 2024
by
Steven
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat: impl list syntax auto complete to editor
parent
436a6cb0
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
135 additions
and
87 deletions
+135
-87
index.tsx
web/src/components/MemoEditor/Editor/index.tsx
+92
-87
handlers.ts
web/src/components/MemoEditor/handlers.ts
+0
-0
index.ts
web/src/components/MemoEditor/hooks/index.ts
+3
-0
useAutoComplete.ts
web/src/components/MemoEditor/hooks/useAutoComplete.ts
+40
-0
No files found.
web/src/components/MemoEditor/Editor/index.tsx
View file @
6d10251c
import
classNames
from
"classnames"
;
import
classNames
from
"classnames"
;
import
{
forwardRef
,
ReactNode
,
useCallback
,
useEffect
,
useImperativeHandle
,
useRef
}
from
"react"
;
import
{
forwardRef
,
ReactNode
,
useCallback
,
useEffect
,
useImperativeHandle
,
useRef
}
from
"react"
;
import
{
useAutoComplete
}
from
"../hooks"
;
import
TagSuggestions
from
"./TagSuggestions"
;
import
TagSuggestions
from
"./TagSuggestions"
;
export
interface
EditorRefActions
{
export
interface
EditorRefActions
{
getEditor
:
()
=>
HTMLTextAreaElement
|
null
;
focus
:
FunctionType
;
focus
:
FunctionType
;
scrollToCursor
:
FunctionType
;
scrollToCursor
:
FunctionType
;
insertText
:
(
text
:
string
,
prefix
?:
string
,
suffix
?:
string
)
=>
void
;
insertText
:
(
text
:
string
,
prefix
?:
string
,
suffix
?:
string
)
=>
void
;
...
@@ -43,16 +45,10 @@ const Editor = forwardRef(function Editor(props: Props, ref: React.ForwardedRef<
...
@@ -43,16 +45,10 @@ const Editor = forwardRef(function Editor(props: Props, ref: React.ForwardedRef<
}
}
},
[
editorRef
.
current
?.
value
]);
},
[
editorRef
.
current
?.
value
]);
const
updateEditorHeight
=
()
=>
{
const
editorActions
=
{
if
(
editorRef
.
current
)
{
getEditor
:
()
=>
{
editorRef
.
current
.
style
.
height
=
"auto"
;
return
editorRef
.
current
;
editorRef
.
current
.
style
.
height
=
(
editorRef
.
current
.
scrollHeight
??
0
)
+
"px"
;
},
}
};
useImperativeHandle
(
ref
,
()
=>
({
focus
:
()
=>
{
focus
:
()
=>
{
editorRef
.
current
?.
focus
();
editorRef
.
current
?.
focus
();
},
},
...
@@ -135,9 +131,18 @@ const Editor = forwardRef(function Editor(props: Props, ref: React.ForwardedRef<
...
@@ -135,9 +131,18 @@ const Editor = forwardRef(function Editor(props: Props, ref: React.ForwardedRef<
updateEditorHeight
();
updateEditorHeight
();
}
}
},
},
}),
};
[],
);
useAutoComplete
(
editorActions
);
useImperativeHandle
(
ref
,
()
=>
editorActions
,
[]);
const
updateEditorHeight
=
()
=>
{
if
(
editorRef
.
current
)
{
editorRef
.
current
.
style
.
height
=
"auto"
;
editorRef
.
current
.
style
.
height
=
(
editorRef
.
current
.
scrollHeight
??
0
)
+
"px"
;
}
};
const
handleEditorInput
=
useCallback
(()
=>
{
const
handleEditorInput
=
useCallback
(()
=>
{
handleContentChangeCallback
(
editorRef
.
current
?.
value
??
""
);
handleContentChangeCallback
(
editorRef
.
current
?.
value
??
""
);
...
...
web/src/components/MemoEditor/handlers.ts
x
→
web/src/components/MemoEditor/handlers.ts
View file @
6d10251c
File moved
web/src/components/MemoEditor/hooks/index.ts
0 → 100644
View file @
6d10251c
import
useAutoComplete
from
"./useAutoComplete"
;
export
{
useAutoComplete
};
web/src/components/MemoEditor/hooks/useAutoComplete.ts
0 → 100644
View file @
6d10251c
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"
)
{
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