Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
C
coopwire-hypothesis
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
孙灵跃 Leon Sun
coopwire-hypothesis
Commits
cf98b78f
Commit
cf98b78f
authored
Jan 19, 2023
by
Alejandro Celaya
Committed by
Alejandro Celaya
Jan 23, 2023
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Allow different prefixes per selected line when clicking MarkdownButtons
parent
b1615bcf
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
49 additions
and
16 deletions
+49
-16
MarkdownEditor.tsx
src/sidebar/components/MarkdownEditor.tsx
+3
-1
MarkdownEditor-test.js
src/sidebar/components/test/MarkdownEditor-test.js
+1
-1
markdown-commands.js
src/sidebar/markdown-commands.js
+34
-14
markdown-commands-test.js
src/sidebar/test/markdown-commands-test.js
+11
-0
No files found.
src/sidebar/components/MarkdownEditor.tsx
View file @
cf98b78f
...
@@ -118,7 +118,9 @@ function handleToolbarCommand(
...
@@ -118,7 +118,9 @@ function handleToolbarCommand(
update
(
insertMath
);
update
(
insertMath
);
break
;
break
;
case
'numlist'
:
case
'numlist'
:
update
(
state
=>
toggleBlockStyle
(
state
,
'1. '
));
update
(
state
=>
toggleBlockStyle
(
state
,
lineIndex
=>
`
${
lineIndex
+
1
}
. `
)
);
break
;
break
;
case
'list'
:
case
'list'
:
update
(
state
=>
toggleBlockStyle
(
state
,
'* '
));
update
(
state
=>
toggleBlockStyle
(
state
,
'* '
));
...
...
src/sidebar/components/test/MarkdownEditor-test.js
View file @
cf98b78f
...
@@ -101,7 +101,7 @@ describe('MarkdownEditor', () => {
...
@@ -101,7 +101,7 @@ describe('MarkdownEditor', () => {
{
{
command
:
'Numbered list'
,
command
:
'Numbered list'
,
key
:
'o'
,
key
:
'o'
,
effect
:
[
fakeMarkdownCommands
.
toggleBlockStyle
,
'1. '
],
effect
:
[
fakeMarkdownCommands
.
toggleBlockStyle
,
sinon
.
match
.
func
],
},
},
];
];
commands
.
forEach
(({
command
,
key
,
effect
})
=>
{
commands
.
forEach
(({
command
,
key
,
effect
})
=>
{
...
...
src/sidebar/markdown-commands.js
View file @
cf98b78f
...
@@ -209,19 +209,20 @@ function endOfLine(str, pos) {
...
@@ -209,19 +209,20 @@ function endOfLine(str, pos) {
* @param {EditorState} state - The initial state of the input field
* @param {EditorState} state - The initial state of the input field
* @param {number} start - The start position within the input text
* @param {number} start - The start position within the input text
* @param {number} end - The end position within the input text
* @param {number} end - The end position within the input text
* @param {(s: EditorState, start: number, end: number) => EditorState} callback
* @param {(s: EditorState, start: number, end: number
, lineIndex: number
) => EditorState} callback
* - Callback which is invoked with the current state of the input and
* - Callback which is invoked with the current state of the input and
* the start of the current line and returns the new state of the input.
* the start of the current line and returns the new state of the input.
*/
*/
function
transformLines
(
state
,
start
,
end
,
callback
)
{
function
transformLines
(
state
,
start
,
end
,
callback
)
{
let
lineStart
=
startOfLine
(
state
.
text
,
start
);
let
lineStart
=
startOfLine
(
state
.
text
,
start
);
let
lineEnd
=
endOfLine
(
state
.
text
,
start
);
let
lineEnd
=
endOfLine
(
state
.
text
,
start
);
let
lineIndex
=
0
;
while
(
lineEnd
<=
endOfLine
(
state
.
text
,
end
))
{
while
(
lineEnd
<=
endOfLine
(
state
.
text
,
end
))
{
const
isLastLine
=
lineEnd
===
state
.
text
.
length
;
const
isLastLine
=
lineEnd
===
state
.
text
.
length
;
const
currentLineLength
=
lineEnd
-
lineStart
;
const
currentLineLength
=
lineEnd
-
lineStart
;
state
=
callback
(
state
,
lineStart
,
lineEnd
);
state
=
callback
(
state
,
lineStart
,
lineEnd
,
lineIndex
);
const
newLineLength
=
endOfLine
(
state
.
text
,
lineStart
)
-
lineStart
;
const
newLineLength
=
endOfLine
(
state
.
text
,
lineStart
)
-
lineStart
;
end
+=
newLineLength
-
currentLineLength
;
end
+=
newLineLength
-
currentLineLength
;
...
@@ -231,6 +232,7 @@ function transformLines(state, start, end, callback) {
...
@@ -231,6 +232,7 @@ function transformLines(state, start, end, callback) {
}
}
lineStart
=
lineStart
+
newLineLength
+
1
;
lineStart
=
lineStart
+
newLineLength
+
1
;
lineEnd
=
endOfLine
(
state
.
text
,
lineStart
);
lineEnd
=
endOfLine
(
state
.
text
,
lineStart
);
lineIndex
+=
1
;
}
}
return
state
;
return
state
;
}
}
...
@@ -239,18 +241,24 @@ function transformLines(state, start, end, callback) {
...
@@ -239,18 +241,24 @@ function transformLines(state, start, end, callback) {
* Toggle Markdown-style formatting around a block of text.
* Toggle Markdown-style formatting around a block of text.
*
*
* @param {EditorState} state - The current state of the input field.
* @param {EditorState} state - The current state of the input field.
* @param {string} prefix - The prefix to add or remove before each line
* @param {string|((lineIndex: number) => string)} prefix - The prefix
* of the selection.
* to add or remove before each line of the selection, or a callback
* which returns said prefix for a specific line index (useful for
* multi-line selections that require different prefixes per line).
* @return {EditorState} - The new state of the input field.
* @return {EditorState} - The new state of the input field.
*/
*/
export
function
toggleBlockStyle
(
state
,
prefix
)
{
export
function
toggleBlockStyle
(
state
,
prefix
)
{
const
start
=
state
.
selectionStart
;
const
start
=
state
.
selectionStart
;
const
end
=
state
.
selectionEnd
;
const
end
=
state
.
selectionEnd
;
/** @param {number} lineIndex */
const
prefixToString
=
lineIndex
=>
typeof
prefix
===
'function'
?
prefix
(
lineIndex
)
:
prefix
;
// Test whether all lines in the selected range already have the style
// Test whether all lines in the selected range already have the style
// applied
// applied
let
blockHasStyle
=
true
;
let
blockHasStyle
=
true
;
transformLines
(
state
,
start
,
end
,
(
state
,
lineStart
)
=>
{
transformLines
(
state
,
start
,
end
,
(
state
,
lineStart
,
_lineEnd
,
lineIndex
)
=>
{
const
prefix
=
prefixToString
(
lineIndex
);
if
(
state
.
text
.
slice
(
lineStart
,
lineStart
+
prefix
.
length
)
!==
prefix
)
{
if
(
state
.
text
.
slice
(
lineStart
,
lineStart
+
prefix
.
length
)
!==
prefix
)
{
blockHasStyle
=
false
;
blockHasStyle
=
false
;
}
}
...
@@ -259,17 +267,29 @@ export function toggleBlockStyle(state, prefix) {
...
@@ -259,17 +267,29 @@ export function toggleBlockStyle(state, prefix) {
if
(
blockHasStyle
)
{
if
(
blockHasStyle
)
{
// Remove the formatting.
// Remove the formatting.
return
transformLines
(
state
,
start
,
end
,
(
state
,
lineStart
)
=>
{
return
transformLines
(
state
,
start
,
end
,
(
state
,
lineStart
,
_lineEnd
,
lineIndex
)
=>
{
const
prefix
=
prefixToString
(
lineIndex
);
return
replaceText
(
state
,
lineStart
,
prefix
.
length
,
''
);
return
replaceText
(
state
,
lineStart
,
prefix
.
length
,
''
);
});
}
);
}
else
{
}
else
{
// Add the block style to any lines which do not already have it applied
// Add the block style to any lines which do not already have it applied
return
transformLines
(
state
,
start
,
end
,
(
state
,
lineStart
)
=>
{
return
transformLines
(
state
,
start
,
end
,
(
state
,
lineStart
,
_lineEnd
,
lineIndex
)
=>
{
const
prefix
=
prefixToString
(
lineIndex
);
if
(
state
.
text
.
slice
(
lineStart
,
lineStart
+
prefix
.
length
)
===
prefix
)
{
if
(
state
.
text
.
slice
(
lineStart
,
lineStart
+
prefix
.
length
)
===
prefix
)
{
return
state
;
return
state
;
}
else
{
}
else
{
return
replaceText
(
state
,
lineStart
,
0
,
prefix
);
return
replaceText
(
state
,
lineStart
,
0
,
prefix
);
}
}
});
}
);
}
}
}
}
src/sidebar/test/markdown-commands-test.js
View file @
cf98b78f
...
@@ -111,6 +111,17 @@ describe('markdown commands', () => {
...
@@ -111,6 +111,17 @@ describe('markdown commands', () => {
assert
.
equal
(
formatState
(
output
),
fixture
.
output
);
assert
.
equal
(
formatState
(
output
),
fixture
.
output
);
});
});
});
});
it
(
'adds formatting to multi-line blocks'
,
()
=>
{
const
output
=
commands
.
toggleBlockStyle
(
parseState
(
'one
\
n<sel>two
\
nthree
\
nfour</sel>
\
nfive'
),
lineIndex
=>
`::
${
lineIndex
*
2
}
:: `
);
assert
.
equal
(
formatState
(
output
),
'one
\
n::0:: <sel>two
\
n::2:: three
\
n::4:: four</sel>
\
nfive'
);
});
});
});
describe
(
'link formatting'
,
()
=>
{
describe
(
'link formatting'
,
()
=>
{
...
...
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