Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
1
Merge Requests
1
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
nexedi
gitlab-ce
Commits
fe1aa985
Commit
fe1aa985
authored
Dec 06, 2021
by
Denys Mishunov
Committed by
Natalia Tepluhina
Dec 06, 2021
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Branch off the Live Preview into a separate extension
parent
437f277f
Changes
4
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
555 additions
and
515 deletions
+555
-515
app/assets/javascripts/editor/extensions/source_editor_markdown_ext.js
...vascripts/editor/extensions/source_editor_markdown_ext.js
+2
-153
app/assets/javascripts/editor/extensions/source_editor_markdown_livepreview_ext.js
...itor/extensions/source_editor_markdown_livepreview_ext.js
+154
-0
spec/frontend/editor/source_editor_markdown_ext_spec.js
spec/frontend/editor/source_editor_markdown_ext_spec.js
+1
-362
spec/frontend/editor/source_editor_markdown_livepreview_ext_spec.js
...end/editor/source_editor_markdown_livepreview_ext_spec.js
+398
-0
No files found.
app/assets/javascripts/editor/extensions/source_editor_markdown_ext.js
View file @
fe1aa985
import
{
debounce
}
from
'
lodash
'
;
import
{
BLOB_PREVIEW_ERROR
}
from
'
~/blob_edit/constants
'
;
import
createFlash
from
'
~/flash
'
;
import
{
sanitize
}
from
'
~/lib/dompurify
'
;
import
axios
from
'
~/lib/utils/axios_utils
'
;
import
{
__
}
from
'
~/locale
'
;
import
syntaxHighlight
from
'
~/syntax_highlight
'
;
import
{
EXTENSION_MARKDOWN_PREVIEW_PANEL_CLASS
,
EXTENSION_MARKDOWN_PREVIEW_ACTION_ID
,
EXTENSION_MARKDOWN_PREVIEW_PANEL_WIDTH
,
EXTENSION_MARKDOWN_PREVIEW_PANEL_PARENT_CLASS
,
EXTENSION_MARKDOWN_PREVIEW_UPDATE_DELAY
,
}
from
'
../constants
'
;
import
{
SourceEditorExtension
}
from
'
./source_editor_extension_base
'
;
const
getPreview
=
(
text
,
previewMarkdownPath
)
=>
{
return
axios
.
post
(
previewMarkdownPath
,
{
text
,
})
.
then
(({
data
})
=>
{
return
data
.
body
;
});
};
const
setupDomElement
=
({
injectToEl
=
null
}
=
{})
=>
{
const
previewEl
=
document
.
createElement
(
'
div
'
);
previewEl
.
classList
.
add
(
EXTENSION_MARKDOWN_PREVIEW_PANEL_CLASS
);
previewEl
.
style
.
display
=
'
none
'
;
if
(
injectToEl
)
{
injectToEl
.
appendChild
(
previewEl
);
}
return
previewEl
;
};
export
class
EditorMarkdownExtension
extends
SourceEditorExtension
{
constructor
({
instance
,
previewMarkdownPath
,
...
args
}
=
{})
{
super
({
instance
,
...
args
});
Object
.
assign
(
instance
,
{
previewMarkdownPath
,
preview
:
{
el
:
undefined
,
action
:
undefined
,
shown
:
false
,
modelChangeListener
:
undefined
,
},
});
this
.
setupPreviewAction
.
call
(
instance
);
instance
.
getModel
().
onDidChangeLanguage
(({
newLanguage
,
oldLanguage
}
=
{})
=>
{
if
(
newLanguage
===
'
markdown
'
&&
oldLanguage
!==
newLanguage
)
{
instance
.
setupPreviewAction
();
}
else
{
instance
.
cleanup
();
}
});
instance
.
onDidChangeModel
(()
=>
{
const
model
=
instance
.
getModel
();
if
(
model
)
{
const
{
language
}
=
model
.
getLanguageIdentifier
();
instance
.
cleanup
();
if
(
language
===
'
markdown
'
)
{
instance
.
setupPreviewAction
();
}
}
});
}
static
togglePreviewLayout
()
{
const
{
width
,
height
}
=
this
.
getLayoutInfo
();
const
newWidth
=
this
.
preview
.
shown
?
width
/
EXTENSION_MARKDOWN_PREVIEW_PANEL_WIDTH
:
width
*
EXTENSION_MARKDOWN_PREVIEW_PANEL_WIDTH
;
this
.
layout
({
width
:
newWidth
,
height
});
}
static
togglePreviewPanel
()
{
const
parentEl
=
this
.
getDomNode
().
parentElement
;
const
{
el
:
previewEl
}
=
this
.
preview
;
parentEl
.
classList
.
toggle
(
EXTENSION_MARKDOWN_PREVIEW_PANEL_PARENT_CLASS
);
if
(
previewEl
.
style
.
display
===
'
none
'
)
{
// Show the preview panel
this
.
fetchPreview
();
}
else
{
// Hide the preview panel
previewEl
.
style
.
display
=
'
none
'
;
}
}
cleanup
()
{
if
(
this
.
preview
.
modelChangeListener
)
{
this
.
preview
.
modelChangeListener
.
dispose
();
}
this
.
preview
.
action
.
dispose
();
if
(
this
.
preview
.
shown
)
{
EditorMarkdownExtension
.
togglePreviewPanel
.
call
(
this
);
EditorMarkdownExtension
.
togglePreviewLayout
.
call
(
this
);
}
this
.
preview
.
shown
=
false
;
}
fetchPreview
()
{
const
{
el
:
previewEl
}
=
this
.
preview
;
getPreview
(
this
.
getValue
(),
this
.
previewMarkdownPath
)
.
then
((
data
)
=>
{
previewEl
.
innerHTML
=
sanitize
(
data
);
syntaxHighlight
(
previewEl
.
querySelectorAll
(
'
.js-syntax-highlight
'
));
previewEl
.
style
.
display
=
'
block
'
;
})
.
catch
(()
=>
createFlash
(
BLOB_PREVIEW_ERROR
));
}
setupPreviewAction
()
{
if
(
this
.
getAction
(
EXTENSION_MARKDOWN_PREVIEW_ACTION_ID
))
return
;
this
.
preview
.
action
=
this
.
addAction
({
id
:
EXTENSION_MARKDOWN_PREVIEW_ACTION_ID
,
label
:
__
(
'
Preview Markdown
'
),
keybindings
:
[
// eslint-disable-next-line no-bitwise,no-undef
monaco
.
KeyMod
.
chord
(
monaco
.
KeyMod
.
CtrlCmd
|
monaco
.
KeyMod
.
Shift
|
monaco
.
KeyCode
.
KEY_P
),
],
contextMenuGroupId
:
'
navigation
'
,
contextMenuOrder
:
1.5
,
// Method that will be executed when the action is triggered.
// @param ed The editor instance is passed in as a convenience
run
(
instance
)
{
instance
.
togglePreview
();
},
});
}
togglePreview
()
{
if
(
!
this
.
preview
?.
el
)
{
this
.
preview
.
el
=
setupDomElement
({
injectToEl
:
this
.
getDomNode
().
parentElement
});
}
EditorMarkdownExtension
.
togglePreviewLayout
.
call
(
this
);
EditorMarkdownExtension
.
togglePreviewPanel
.
call
(
this
);
if
(
!
this
.
preview
?.
shown
)
{
this
.
preview
.
modelChangeListener
=
this
.
onDidChangeModelContent
(
debounce
(
this
.
fetchPreview
.
bind
(
this
),
EXTENSION_MARKDOWN_PREVIEW_UPDATE_DELAY
),
);
}
else
{
this
.
preview
.
modelChangeListener
.
dispose
();
}
this
.
preview
.
shown
=
!
this
.
preview
?.
shown
;
}
import
{
EditorMarkdownPreviewExtension
}
from
'
~/editor/extensions/source_editor_markdown_livepreview_ext
'
;
export
class
EditorMarkdownExtension
extends
EditorMarkdownPreviewExtension
{
getSelectedText
(
selection
=
this
.
getSelection
())
{
const
{
startLineNumber
,
endLineNumber
,
startColumn
,
endColumn
}
=
selection
;
const
valArray
=
this
.
getValue
().
split
(
'
\n
'
);
...
...
app/assets/javascripts/editor/extensions/source_editor_markdown_livepreview_ext.js
0 → 100644
View file @
fe1aa985
import
{
debounce
}
from
'
lodash
'
;
import
{
BLOB_PREVIEW_ERROR
}
from
'
~/blob_edit/constants
'
;
import
createFlash
from
'
~/flash
'
;
import
{
sanitize
}
from
'
~/lib/dompurify
'
;
import
axios
from
'
~/lib/utils/axios_utils
'
;
import
{
__
}
from
'
~/locale
'
;
import
syntaxHighlight
from
'
~/syntax_highlight
'
;
import
{
EXTENSION_MARKDOWN_PREVIEW_PANEL_CLASS
,
EXTENSION_MARKDOWN_PREVIEW_ACTION_ID
,
EXTENSION_MARKDOWN_PREVIEW_PANEL_WIDTH
,
EXTENSION_MARKDOWN_PREVIEW_PANEL_PARENT_CLASS
,
EXTENSION_MARKDOWN_PREVIEW_UPDATE_DELAY
,
}
from
'
../constants
'
;
import
{
SourceEditorExtension
}
from
'
./source_editor_extension_base
'
;
const
getPreview
=
(
text
,
previewMarkdownPath
)
=>
{
return
axios
.
post
(
previewMarkdownPath
,
{
text
,
})
.
then
(({
data
})
=>
{
return
data
.
body
;
});
};
const
setupDomElement
=
({
injectToEl
=
null
}
=
{})
=>
{
const
previewEl
=
document
.
createElement
(
'
div
'
);
previewEl
.
classList
.
add
(
EXTENSION_MARKDOWN_PREVIEW_PANEL_CLASS
);
previewEl
.
style
.
display
=
'
none
'
;
if
(
injectToEl
)
{
injectToEl
.
appendChild
(
previewEl
);
}
return
previewEl
;
};
export
class
EditorMarkdownPreviewExtension
extends
SourceEditorExtension
{
constructor
({
instance
,
previewMarkdownPath
,
...
args
}
=
{})
{
super
({
instance
,
...
args
});
Object
.
assign
(
instance
,
{
previewMarkdownPath
,
preview
:
{
el
:
undefined
,
action
:
undefined
,
shown
:
false
,
modelChangeListener
:
undefined
,
},
});
this
.
setupPreviewAction
.
call
(
instance
);
instance
.
getModel
().
onDidChangeLanguage
(({
newLanguage
,
oldLanguage
}
=
{})
=>
{
if
(
newLanguage
===
'
markdown
'
&&
oldLanguage
!==
newLanguage
)
{
instance
.
setupPreviewAction
();
}
else
{
instance
.
cleanup
();
}
});
instance
.
onDidChangeModel
(()
=>
{
const
model
=
instance
.
getModel
();
if
(
model
)
{
const
{
language
}
=
model
.
getLanguageIdentifier
();
instance
.
cleanup
();
if
(
language
===
'
markdown
'
)
{
instance
.
setupPreviewAction
();
}
}
});
}
static
togglePreviewLayout
()
{
const
{
width
,
height
}
=
this
.
getLayoutInfo
();
const
newWidth
=
this
.
preview
.
shown
?
width
/
EXTENSION_MARKDOWN_PREVIEW_PANEL_WIDTH
:
width
*
EXTENSION_MARKDOWN_PREVIEW_PANEL_WIDTH
;
this
.
layout
({
width
:
newWidth
,
height
});
}
static
togglePreviewPanel
()
{
const
parentEl
=
this
.
getDomNode
().
parentElement
;
const
{
el
:
previewEl
}
=
this
.
preview
;
parentEl
.
classList
.
toggle
(
EXTENSION_MARKDOWN_PREVIEW_PANEL_PARENT_CLASS
);
if
(
previewEl
.
style
.
display
===
'
none
'
)
{
// Show the preview panel
this
.
fetchPreview
();
}
else
{
// Hide the preview panel
previewEl
.
style
.
display
=
'
none
'
;
}
}
cleanup
()
{
if
(
this
.
preview
.
modelChangeListener
)
{
this
.
preview
.
modelChangeListener
.
dispose
();
}
this
.
preview
.
action
.
dispose
();
if
(
this
.
preview
.
shown
)
{
EditorMarkdownPreviewExtension
.
togglePreviewPanel
.
call
(
this
);
EditorMarkdownPreviewExtension
.
togglePreviewLayout
.
call
(
this
);
}
this
.
preview
.
shown
=
false
;
}
fetchPreview
()
{
const
{
el
:
previewEl
}
=
this
.
preview
;
getPreview
(
this
.
getValue
(),
this
.
previewMarkdownPath
)
.
then
((
data
)
=>
{
previewEl
.
innerHTML
=
sanitize
(
data
);
syntaxHighlight
(
previewEl
.
querySelectorAll
(
'
.js-syntax-highlight
'
));
previewEl
.
style
.
display
=
'
block
'
;
})
.
catch
(()
=>
createFlash
(
BLOB_PREVIEW_ERROR
));
}
setupPreviewAction
()
{
if
(
this
.
getAction
(
EXTENSION_MARKDOWN_PREVIEW_ACTION_ID
))
return
;
this
.
preview
.
action
=
this
.
addAction
({
id
:
EXTENSION_MARKDOWN_PREVIEW_ACTION_ID
,
label
:
__
(
'
Preview Markdown
'
),
keybindings
:
[
// eslint-disable-next-line no-bitwise,no-undef
monaco
.
KeyMod
.
chord
(
monaco
.
KeyMod
.
CtrlCmd
|
monaco
.
KeyMod
.
Shift
|
monaco
.
KeyCode
.
KEY_P
),
],
contextMenuGroupId
:
'
navigation
'
,
contextMenuOrder
:
1.5
,
// Method that will be executed when the action is triggered.
// @param ed The editor instance is passed in as a convenience
run
(
instance
)
{
instance
.
togglePreview
();
},
});
}
togglePreview
()
{
if
(
!
this
.
preview
?.
el
)
{
this
.
preview
.
el
=
setupDomElement
({
injectToEl
:
this
.
getDomNode
().
parentElement
});
}
EditorMarkdownPreviewExtension
.
togglePreviewLayout
.
call
(
this
);
EditorMarkdownPreviewExtension
.
togglePreviewPanel
.
call
(
this
);
if
(
!
this
.
preview
?.
shown
)
{
this
.
preview
.
modelChangeListener
=
this
.
onDidChangeModelContent
(
debounce
(
this
.
fetchPreview
.
bind
(
this
),
EXTENSION_MARKDOWN_PREVIEW_UPDATE_DELAY
),
);
}
else
{
this
.
preview
.
modelChangeListener
.
dispose
();
}
this
.
preview
.
shown
=
!
this
.
preview
?.
shown
;
}
}
spec/frontend/editor/source_editor_markdown_ext_spec.js
View file @
fe1aa985
This diff is collapsed.
Click to expand it.
spec/frontend/editor/source_editor_markdown_livepreview_ext_spec.js
0 → 100644
View file @
fe1aa985
This diff is collapsed.
Click to expand it.
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