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
74622098
Commit
74622098
authored
Aug 12, 2021
by
Denys Mishunov
Committed by
Brandon Labuschagne
Aug 12, 2021
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Moved Source Editor's theming into utilities
parent
82685f9a
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
111 additions
and
27 deletions
+111
-27
app/assets/javascripts/editor/source_editor.js
app/assets/javascripts/editor/source_editor.js
+5
-24
app/assets/javascripts/editor/utils.js
app/assets/javascripts/editor/utils.js
+22
-3
spec/frontend/editor/utils_spec.js
spec/frontend/editor/utils_spec.js
+84
-0
No files found.
app/assets/javascripts/editor/source_editor.js
View file @
74622098
import
{
editor
as
monacoEditor
,
languages
as
monacoLanguages
,
Uri
}
from
'
monaco-editor
'
;
import
{
editor
as
monacoEditor
,
Uri
}
from
'
monaco-editor
'
;
import
{
defaultEditorOptions
}
from
'
~/ide/lib/editor_options
'
;
import
languages
from
'
~/ide/lib/languages
'
;
import
{
DEFAULT_THEME
,
themes
}
from
'
~/ide/lib/themes
'
;
import
{
registerLanguages
}
from
'
~/ide/utils
'
;
import
{
joinPaths
}
from
'
~/lib/utils/url_utility
'
;
import
{
uuids
}
from
'
~/lib/utils/uuids
'
;
...
...
@@ -11,7 +10,7 @@ import {
EDITOR_READY_EVENT
,
EDITOR_TYPE_DIFF
,
}
from
'
./constants
'
;
import
{
clearDomElement
}
from
'
./utils
'
;
import
{
clearDomElement
,
setupEditorTheme
,
getBlobLanguage
}
from
'
./utils
'
;
export
default
class
SourceEditor
{
constructor
(
options
=
{})
{
...
...
@@ -22,26 +21,11 @@ export default class SourceEditor {
...
options
,
};
SourceEditor
.
setupMonaco
Theme
();
setupEditor
Theme
();
registerLanguages
(...
languages
);
}
static
setupMonacoTheme
()
{
const
themeName
=
window
.
gon
?.
user_color_scheme
||
DEFAULT_THEME
;
const
theme
=
themes
.
find
((
t
)
=>
t
.
name
===
themeName
);
if
(
theme
)
monacoEditor
.
defineTheme
(
themeName
,
theme
.
data
);
monacoEditor
.
setTheme
(
theme
?
themeName
:
DEFAULT_THEME
);
}
static
getModelLanguage
(
path
)
{
const
ext
=
`.
${
path
.
split
(
'
.
'
).
pop
()}
`
;
const
language
=
monacoLanguages
.
getLanguages
()
.
find
((
lang
)
=>
lang
.
extensions
.
indexOf
(
ext
)
!==
-
1
);
return
language
?
language
.
id
:
'
plaintext
'
;
}
static
pushToImportsArray
(
arr
,
toImport
)
{
arr
.
push
(
import
(
toImport
));
}
...
...
@@ -124,10 +108,7 @@ export default class SourceEditor {
return
model
;
}
const
diffModel
=
{
original
:
monacoEditor
.
createModel
(
blobOriginalContent
,
SourceEditor
.
getModelLanguage
(
model
.
uri
.
path
),
),
original
:
monacoEditor
.
createModel
(
blobOriginalContent
,
getBlobLanguage
(
model
.
uri
.
path
)),
modified
:
model
,
};
instance
.
setModel
(
diffModel
);
...
...
@@ -155,7 +136,7 @@ export default class SourceEditor {
};
static
instanceUpdateLanguage
(
inst
,
path
)
{
const
lang
=
SourceEditor
.
getModel
Language
(
path
);
const
lang
=
getBlob
Language
(
path
);
const
model
=
inst
.
getModel
();
return
monacoEditor
.
setModelLanguage
(
model
,
lang
);
}
...
...
app/assets/javascripts/editor/utils.js
View file @
74622098
import
{
editor
as
monacoEditor
,
languages
as
monacoLanguages
}
from
'
monaco-editor
'
;
import
{
DEFAULT_THEME
,
themes
}
from
'
~/ide/lib/themes
'
;
export
const
clearDomElement
=
(
el
)
=>
{
if
(
!
el
||
!
el
.
firstChild
)
return
;
...
...
@@ -6,6 +9,22 @@ export const clearDomElement = (el) => {
}
};
export
default
()
=>
({
clearDomElement
,
});
export
const
setupEditorTheme
=
()
=>
{
const
themeName
=
window
.
gon
?.
user_color_scheme
||
DEFAULT_THEME
;
const
theme
=
themes
.
find
((
t
)
=>
t
.
name
===
themeName
);
if
(
theme
)
monacoEditor
.
defineTheme
(
themeName
,
theme
.
data
);
monacoEditor
.
setTheme
(
theme
?
themeName
:
DEFAULT_THEME
);
};
export
const
getBlobLanguage
=
(
path
)
=>
{
const
ext
=
`.
${
path
.
split
(
'
.
'
).
pop
()}
`
;
const
language
=
monacoLanguages
.
getLanguages
()
.
find
((
lang
)
=>
lang
.
extensions
.
indexOf
(
ext
)
!==
-
1
);
return
language
?
language
.
id
:
'
plaintext
'
;
};
export
const
setupCodeSnippet
=
(
el
)
=>
{
monacoEditor
.
colorizeElement
(
el
);
setupEditorTheme
();
};
spec/frontend/editor/utils_spec.js
0 → 100644
View file @
74622098
import
{
editor
as
monacoEditor
}
from
'
monaco-editor
'
;
import
*
as
utils
from
'
~/editor/utils
'
;
import
{
DEFAULT_THEME
}
from
'
~/ide/lib/themes
'
;
describe
(
'
Source Editor utils
'
,
()
=>
{
let
el
;
const
stubUserColorScheme
=
(
value
)
=>
{
if
(
window
.
gon
==
null
)
{
window
.
gon
=
{};
}
window
.
gon
.
user_color_scheme
=
value
;
};
describe
(
'
clearDomElement
'
,
()
=>
{
beforeEach
(()
=>
{
setFixtures
(
'
<div id="foo"><div id="bar">Foo</div></div>
'
);
el
=
document
.
getElementById
(
'
foo
'
);
});
it
(
'
removes all child nodes from an element
'
,
()
=>
{
expect
(
el
.
children
.
length
).
toBe
(
1
);
utils
.
clearDomElement
(
el
);
expect
(
el
.
children
.
length
).
toBe
(
0
);
});
});
describe
(
'
setupEditorTheme
'
,
()
=>
{
beforeEach
(()
=>
{
jest
.
spyOn
(
monacoEditor
,
'
defineTheme
'
).
mockImplementation
();
jest
.
spyOn
(
monacoEditor
,
'
setTheme
'
).
mockImplementation
();
});
it
.
each
`
themeName | expectedThemeName
${
'
solarized-light
'
}
|
${
'
solarized-light
'
}
${
DEFAULT_THEME
}
|
${
DEFAULT_THEME
}
${
'
non-existent
'
}
|
${
DEFAULT_THEME
}
`
(
'
sets the $expectedThemeName theme when $themeName is set in the user preference
'
,
({
themeName
,
expectedThemeName
})
=>
{
stubUserColorScheme
(
themeName
);
utils
.
setupEditorTheme
();
expect
(
monacoEditor
.
setTheme
).
toHaveBeenCalledWith
(
expectedThemeName
);
},
);
});
describe
(
'
getBlobLanguage
'
,
()
=>
{
it
.
each
`
path | expectedLanguage
${
'
foo.js
'
}
|
${
'
javascript
'
}
${
'
foo.js.rb
'
}
|
${
'
ruby
'
}
${
'
foo.bar
'
}
|
${
'
plaintext
'
}
`
(
'
sets the $expectedThemeName theme when $themeName is set in the user preference
'
,
({
path
,
expectedLanguage
})
=>
{
const
language
=
utils
.
getBlobLanguage
(
path
);
expect
(
language
).
toEqual
(
expectedLanguage
);
},
);
});
describe
(
'
setupCodeSnipet
'
,
()
=>
{
beforeEach
(()
=>
{
jest
.
spyOn
(
monacoEditor
,
'
colorizeElement
'
).
mockImplementation
();
jest
.
spyOn
(
monacoEditor
,
'
setTheme
'
).
mockImplementation
();
setFixtures
(
'
<pre id="foo"></pre>
'
);
el
=
document
.
getElementById
(
'
foo
'
);
});
it
(
'
colorizes the element and applies the preference theme
'
,
()
=>
{
expect
(
monacoEditor
.
colorizeElement
).
not
.
toHaveBeenCalled
();
expect
(
monacoEditor
.
setTheme
).
not
.
toHaveBeenCalled
();
utils
.
setupCodeSnippet
(
el
);
expect
(
monacoEditor
.
colorizeElement
).
toHaveBeenCalledWith
(
el
);
expect
(
monacoEditor
.
setTheme
).
toHaveBeenCalled
();
});
});
});
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