BigW Consortium Gitlab
Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
G
gitlab-ce
Project
Overview
Details
Activity
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
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
Forest Godfrey
gitlab-ce
Commits
df7eaa85
Commit
df7eaa85
authored
Nov 06, 2017
by
Filipa Lacerda
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'es-module-copy_as_gfm' into 'master'
Convert copy_as_gfm into ES module and move to behaviors See merge request gitlab-org/gitlab-ce!14859
parents
24353036
5ae6fff5
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
58 additions
and
50 deletions
+58
-50
copy_as_gfm.js
app/assets/javascripts/behaviors/copy_as_gfm.js
+12
-6
index.js
app/assets/javascripts/behaviors/index.js
+2
-0
main.js
app/assets/javascripts/main.js
+0
-1
shortcuts_issuable.js
app/assets/javascripts/shortcuts_issuable.js
+3
-2
copy_as_gfm_spec.rb
spec/features/copy_as_gfm_spec.rb
+2
-2
copy_as_gfm_spec.js
spec/javascripts/behaviors/copy_as_gfm_spec.js
+36
-38
shortcuts_issuable_spec.js
spec/javascripts/shortcuts_issuable_spec.js
+3
-1
No files found.
app/assets/javascripts/copy_as_gfm.js
→
app/assets/javascripts/
behaviors/
copy_as_gfm.js
View file @
df7eaa85
/* eslint-disable class-methods-use-this, object-shorthand, no-unused-vars, no-use-before-define, no-new, max-len, no-restricted-syntax, guard-for-in, no-continue */
/* eslint-disable class-methods-use-this, object-shorthand, no-unused-vars, no-use-before-define, no-new, max-len, no-restricted-syntax, guard-for-in, no-continue */
import
_
from
'underscore'
;
import
_
from
'underscore'
;
import
{
insertText
,
getSelectedFragment
,
nodeMatchesSelector
}
from
'./lib/utils/common_utils'
;
import
{
insertText
,
getSelectedFragment
,
nodeMatchesSelector
}
from
'.
.
/lib/utils/common_utils'
;
import
{
placeholderImage
}
from
'./lazy_loader'
;
import
{
placeholderImage
}
from
'.
.
/lazy_loader'
;
const
gfmRules
=
{
const
gfmRules
=
{
// The filters referenced in lib/banzai/pipeline/gfm_pipeline.rb convert
// The filters referenced in lib/banzai/pipeline/gfm_pipeline.rb convert
...
@@ -284,7 +285,7 @@ const gfmRules = {
...
@@ -284,7 +285,7 @@ const gfmRules = {
},
},
};
};
class
CopyAsGFM
{
export
class
CopyAsGFM
{
constructor
()
{
constructor
()
{
$
(
document
).
on
(
'copy'
,
'.md, .wiki'
,
(
e
)
=>
{
CopyAsGFM
.
copyAsGFM
(
e
,
CopyAsGFM
.
transformGFMSelection
);
});
$
(
document
).
on
(
'copy'
,
'.md, .wiki'
,
(
e
)
=>
{
CopyAsGFM
.
copyAsGFM
(
e
,
CopyAsGFM
.
transformGFMSelection
);
});
$
(
document
).
on
(
'copy'
,
'pre.code.highlight, .diff-content .line_content'
,
(
e
)
=>
{
CopyAsGFM
.
copyAsGFM
(
e
,
CopyAsGFM
.
transformCodeSelection
);
});
$
(
document
).
on
(
'copy'
,
'pre.code.highlight, .diff-content .line_content'
,
(
e
)
=>
{
CopyAsGFM
.
copyAsGFM
(
e
,
CopyAsGFM
.
transformCodeSelection
);
});
...
@@ -469,7 +470,12 @@ class CopyAsGFM {
...
@@ -469,7 +470,12 @@ class CopyAsGFM {
}
}
}
}
window
.
gl
=
window
.
gl
||
{};
// Export CopyAsGFM as a global for rspec to access
window
.
gl
.
CopyAsGFM
=
CopyAsGFM
;
// see /spec/features/copy_as_gfm_spec.rb
if
(
process
.
env
.
NODE_ENV
!==
'production'
)
{
window
.
CopyAsGFM
=
CopyAsGFM
;
}
new
CopyAsGFM
();
export
default
function
initCopyAsGFM
()
{
return
new
CopyAsGFM
();
}
app/assets/javascripts/behaviors/index.js
View file @
df7eaa85
import
'./autosize'
;
import
'./autosize'
;
import
'./bind_in_out'
;
import
'./bind_in_out'
;
import
initCopyAsGFM
from
'./copy_as_gfm'
;
import
'./details_behavior'
;
import
'./details_behavior'
;
import
installGlEmojiElement
from
'./gl_emoji'
;
import
installGlEmojiElement
from
'./gl_emoji'
;
import
'./quick_submit'
;
import
'./quick_submit'
;
...
@@ -7,3 +8,4 @@ import './requires_input';
...
@@ -7,3 +8,4 @@ import './requires_input';
import
'./toggler_behavior'
;
import
'./toggler_behavior'
;
installGlEmojiElement
();
installGlEmojiElement
();
initCopyAsGFM
();
app/assets/javascripts/main.js
View file @
df7eaa85
...
@@ -46,7 +46,6 @@ import './commits';
...
@@ -46,7 +46,6 @@ import './commits';
import
'./compare'
;
import
'./compare'
;
import
'./compare_autocomplete'
;
import
'./compare_autocomplete'
;
import
'./confirm_danger_modal'
;
import
'./confirm_danger_modal'
;
import
'./copy_as_gfm'
;
import
'./copy_to_clipboard'
;
import
'./copy_to_clipboard'
;
import
Flash
,
{
removeFlashClickListener
}
from
'./flash'
;
import
Flash
,
{
removeFlashClickListener
}
from
'./flash'
;
import
'./gl_dropdown'
;
import
'./gl_dropdown'
;
...
...
app/assets/javascripts/shortcuts_issuable.js
View file @
df7eaa85
...
@@ -4,6 +4,7 @@
...
@@ -4,6 +4,7 @@
import
_
from
'underscore'
;
import
_
from
'underscore'
;
import
'mousetrap'
;
import
'mousetrap'
;
import
ShortcutsNavigation
from
'./shortcuts_navigation'
;
import
ShortcutsNavigation
from
'./shortcuts_navigation'
;
import
{
CopyAsGFM
}
from
'./behaviors/copy_as_gfm'
;
export
default
class
ShortcutsIssuable
extends
ShortcutsNavigation
{
export
default
class
ShortcutsIssuable
extends
ShortcutsNavigation
{
constructor
(
isMergeRequest
)
{
constructor
(
isMergeRequest
)
{
...
@@ -33,8 +34,8 @@ export default class ShortcutsIssuable extends ShortcutsNavigation {
...
@@ -33,8 +34,8 @@ export default class ShortcutsIssuable extends ShortcutsNavigation {
return
false
;
return
false
;
}
}
const
el
=
window
.
gl
.
CopyAsGFM
.
transformGFMSelection
(
documentFragment
.
cloneNode
(
true
));
const
el
=
CopyAsGFM
.
transformGFMSelection
(
documentFragment
.
cloneNode
(
true
));
const
selected
=
window
.
gl
.
CopyAsGFM
.
nodeToGFM
(
el
);
const
selected
=
CopyAsGFM
.
nodeToGFM
(
el
);
if
(
selected
.
trim
()
===
''
)
{
if
(
selected
.
trim
()
===
''
)
{
return
false
;
return
false
;
...
...
spec/features/copy_as_gfm_spec.rb
View file @
df7eaa85
...
@@ -664,7 +664,7 @@ describe 'Copy as GFM', :js do
...
@@ -664,7 +664,7 @@ describe 'Copy as GFM', :js do
def
html_to_gfm
(
html
,
transformer
=
'transformGFMSelection'
,
target:
nil
)
def
html_to_gfm
(
html
,
transformer
=
'transformGFMSelection'
,
target:
nil
)
js
=
<<-
JS
.
strip_heredoc
js
=
<<-
JS
.
strip_heredoc
(function(html) {
(function(html) {
var transformer = window.
gl.
CopyAsGFM[
#{
transformer
.
inspect
}
];
var transformer = window.CopyAsGFM[
#{
transformer
.
inspect
}
];
var node = document.createElement('div');
var node = document.createElement('div');
$(html).each(function() { node.appendChild(this) });
$(html).each(function() { node.appendChild(this) });
...
@@ -678,7 +678,7 @@ describe 'Copy as GFM', :js do
...
@@ -678,7 +678,7 @@ describe 'Copy as GFM', :js do
node = transformer(node, target);
node = transformer(node, target);
if (!node) return null;
if (!node) return null;
return window.
gl.
CopyAsGFM.nodeToGFM(node);
return window.CopyAsGFM.nodeToGFM(node);
})("
#{
escape_javascript
(
html
)
}
")
})("
#{
escape_javascript
(
html
)
}
")
JS
JS
page
.
evaluate_script
(
js
)
page
.
evaluate_script
(
js
)
...
...
spec/javascripts/copy_as_gfm_spec.js
→
spec/javascripts/
behaviors/
copy_as_gfm_spec.js
View file @
df7eaa85
import
'~
/copy_as_gfm'
;
import
{
CopyAsGFM
}
from
'~/behaviors
/copy_as_gfm'
;
(()
=>
{
describe
(
'CopyAsGFM'
,
()
=>
{
describe
(
'gl.CopyAsGFM'
,
()
=>
{
describe
(
'CopyAsGFM.pasteGFM'
,
()
=>
{
describe
(
'gl.CopyAsGFM.pasteGFM'
,
()
=>
{
function
callPasteGFM
()
{
function
callPasteGFM
()
{
const
e
=
{
const
e
=
{
originalEvent
:
{
originalEvent
:
{
clipboardData
:
{
clipboardData
:
{
getData
(
mimeType
)
{
getData
(
mimeType
)
{
// When GFM code is copied, we put the regular plain text
// When GFM code is copied, we put the regular plain text
// on the clipboard as `text/plain`, and the GFM as `text/x-gfm`.
// on the clipboard as `text/plain`, and the GFM as `text/x-gfm`.
// This emulates the behavior of `getData` with that data.
// This emulates the behavior of `getData` with that data.
if
(
mimeType
===
'text/plain'
)
{
if
(
mimeType
===
'text/plain'
)
{
return
'code'
;
return
'code'
;
}
}
if
(
mimeType
===
'text/x-gfm'
)
{
if
(
mimeType
===
'text/x-gfm'
)
{
return
'`code`'
;
return
'`code`'
;
}
}
return
null
;
return
null
;
},
},
},
},
},
preventDefault
()
{},
},
};
preventDefault
()
{},
};
window
.
gl
.
CopyAsGFM
.
pasteGFM
(
e
);
CopyAsGFM
.
pasteGFM
(
e
);
}
}
it
(
'wraps pasted code when not already in code tags'
,
()
=>
{
it
(
'wraps pasted code when not already in code tags'
,
()
=>
{
spyOn
(
window
.
gl
.
utils
,
'insertText'
).
and
.
callFake
((
el
,
textFunc
)
=>
{
spyOn
(
window
.
gl
.
utils
,
'insertText'
).
and
.
callFake
((
el
,
textFunc
)
=>
{
const
insertedText
=
textFunc
(
'This is code: '
,
''
);
const
insertedText
=
textFunc
(
'This is code: '
,
''
);
expect
(
insertedText
).
toEqual
(
'`code`'
);
expect
(
insertedText
).
toEqual
(
'`code`'
);
});
callPasteGFM
();
});
});
it
(
'does not wrap pasted code when already in code tags'
,
()
=>
{
callPasteGFM
();
spyOn
(
window
.
gl
.
utils
,
'insertText'
).
and
.
callFake
((
el
,
textFunc
)
=>
{
});
const
insertedText
=
textFunc
(
'This is code: `'
,
'`'
);
expect
(
insertedText
).
toEqual
(
'code'
);
});
callPasteGFM
();
it
(
'does not wrap pasted code when already in code tags'
,
()
=>
{
spyOn
(
window
.
gl
.
utils
,
'insertText'
).
and
.
callFake
((
el
,
textFunc
)
=>
{
const
insertedText
=
textFunc
(
'This is code: `'
,
'`'
);
expect
(
insertedText
).
toEqual
(
'code'
);
});
});
callPasteGFM
();
});
});
});
});
})
()
;
});
spec/javascripts/shortcuts_issuable_spec.js
View file @
df7eaa85
import
'~
/copy_as_gfm'
;
import
initCopyAsGFM
from
'~/behaviors
/copy_as_gfm'
;
import
ShortcutsIssuable
from
'~/shortcuts_issuable'
;
import
ShortcutsIssuable
from
'~/shortcuts_issuable'
;
initCopyAsGFM
();
describe
(
'ShortcutsIssuable'
,
()
=>
{
describe
(
'ShortcutsIssuable'
,
()
=>
{
const
fixtureName
=
'merge_requests/diff_comment.html.raw'
;
const
fixtureName
=
'merge_requests/diff_comment.html.raw'
;
preloadFixtures
(
fixtureName
);
preloadFixtures
(
fixtureName
);
...
...
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