mirror of
https://github.com/naturalcrit/homebrewery.git
synced 2026-03-22 08:58:11 +00:00
Merge pull request #4505 from dbolack-ab/codeMirror-skipExtraKeys
Disable some codeEditor hotkeys if not in a gfm language editor.
This commit is contained in:
@@ -12,6 +12,7 @@ const CodeEditor = createReactClass({
|
|||||||
getDefaultProps : function() {
|
getDefaultProps : function() {
|
||||||
return {
|
return {
|
||||||
language : '',
|
language : '',
|
||||||
|
tab : 'brewText',
|
||||||
value : '',
|
value : '',
|
||||||
wrap : true,
|
wrap : true,
|
||||||
onChange : ()=>{},
|
onChange : ()=>{},
|
||||||
@@ -186,6 +187,22 @@ const CodeEditor = createReactClass({
|
|||||||
this.updateSize();
|
this.updateSize();
|
||||||
},
|
},
|
||||||
|
|
||||||
|
// Use for GFM tabs that use common hot-keys
|
||||||
|
isGFM : function() {
|
||||||
|
if((this.isGFM()) || (this.props.tab === 'brewSnippets')) return true;
|
||||||
|
return false;
|
||||||
|
},
|
||||||
|
|
||||||
|
isBrewText : function() {
|
||||||
|
if(this.isGFM()) return true;
|
||||||
|
return false;
|
||||||
|
},
|
||||||
|
|
||||||
|
isBrewSnippets : function() {
|
||||||
|
if(this.props.tab === 'brewSnippets') return true;
|
||||||
|
return false;
|
||||||
|
},
|
||||||
|
|
||||||
indent : function () {
|
indent : function () {
|
||||||
const cm = this.codeMirror;
|
const cm = this.codeMirror;
|
||||||
if(cm.somethingSelected()) {
|
if(cm.somethingSelected()) {
|
||||||
@@ -200,6 +217,7 @@ const CodeEditor = createReactClass({
|
|||||||
},
|
},
|
||||||
|
|
||||||
makeHeader : function (number) {
|
makeHeader : function (number) {
|
||||||
|
if(!this.isGFM()) return;
|
||||||
const selection = this.codeMirror?.getSelection();
|
const selection = this.codeMirror?.getSelection();
|
||||||
const header = Array(number).fill('#').join('');
|
const header = Array(number).fill('#').join('');
|
||||||
this.codeMirror?.replaceSelection(`${header} ${selection}`, 'around');
|
this.codeMirror?.replaceSelection(`${header} ${selection}`, 'around');
|
||||||
@@ -208,6 +226,7 @@ const CodeEditor = createReactClass({
|
|||||||
},
|
},
|
||||||
|
|
||||||
makeBold : function() {
|
makeBold : function() {
|
||||||
|
if(!this.isGFM()) return;
|
||||||
const selection = this.codeMirror?.getSelection(), t = selection.slice(0, 2) === '**' && selection.slice(-2) === '**';
|
const selection = this.codeMirror?.getSelection(), t = selection.slice(0, 2) === '**' && selection.slice(-2) === '**';
|
||||||
this.codeMirror?.replaceSelection(t ? selection.slice(2, -2) : `**${selection}**`, 'around');
|
this.codeMirror?.replaceSelection(t ? selection.slice(2, -2) : `**${selection}**`, 'around');
|
||||||
if(selection.length === 0){
|
if(selection.length === 0){
|
||||||
@@ -217,7 +236,8 @@ const CodeEditor = createReactClass({
|
|||||||
},
|
},
|
||||||
|
|
||||||
makeItalic : function() {
|
makeItalic : function() {
|
||||||
const selection = this.codeMirror?.getSelection(), t = selection.slice(0, 1) === '*' && selection.slice(-1) === '*';
|
if(!this.isGFM()) return;
|
||||||
|
const selection = this.codeMirror.getSelection(), t = selection.slice(0, 1) === '*' && selection.slice(-1) === '*';
|
||||||
this.codeMirror?.replaceSelection(t ? selection.slice(1, -1) : `*${selection}*`, 'around');
|
this.codeMirror?.replaceSelection(t ? selection.slice(1, -1) : `*${selection}*`, 'around');
|
||||||
if(selection.length === 0){
|
if(selection.length === 0){
|
||||||
const cursor = this.codeMirror?.getCursor();
|
const cursor = this.codeMirror?.getCursor();
|
||||||
@@ -226,7 +246,8 @@ const CodeEditor = createReactClass({
|
|||||||
},
|
},
|
||||||
|
|
||||||
makeSuper : function() {
|
makeSuper : function() {
|
||||||
const selection = this.codeMirror?.getSelection(), t = selection.slice(0, 1) === '^' && selection.slice(-1) === '^';
|
if(!this.isGFM()) return;
|
||||||
|
const selection = this.codeMirror.getSelection(), t = selection.slice(0, 1) === '^' && selection.slice(-1) === '^';
|
||||||
this.codeMirror?.replaceSelection(t ? selection.slice(1, -1) : `^${selection}^`, 'around');
|
this.codeMirror?.replaceSelection(t ? selection.slice(1, -1) : `^${selection}^`, 'around');
|
||||||
if(selection.length === 0){
|
if(selection.length === 0){
|
||||||
const cursor = this.codeMirror?.getCursor();
|
const cursor = this.codeMirror?.getCursor();
|
||||||
@@ -235,7 +256,8 @@ const CodeEditor = createReactClass({
|
|||||||
},
|
},
|
||||||
|
|
||||||
makeSub : function() {
|
makeSub : function() {
|
||||||
const selection = this.codeMirror?.getSelection(), t = selection.slice(0, 2) === '^^' && selection.slice(-2) === '^^';
|
if(!this.isGFM()) return;
|
||||||
|
const selection = this.codeMirror.getSelection(), t = selection.slice(0, 2) === '^^' && selection.slice(-2) === '^^';
|
||||||
this.codeMirror?.replaceSelection(t ? selection.slice(2, -2) : `^^${selection}^^`, 'around');
|
this.codeMirror?.replaceSelection(t ? selection.slice(2, -2) : `^^${selection}^^`, 'around');
|
||||||
if(selection.length === 0){
|
if(selection.length === 0){
|
||||||
const cursor = this.codeMirror?.getCursor();
|
const cursor = this.codeMirror?.getCursor();
|
||||||
@@ -245,10 +267,12 @@ const CodeEditor = createReactClass({
|
|||||||
|
|
||||||
|
|
||||||
makeNbsp : function() {
|
makeNbsp : function() {
|
||||||
|
if(!this.isGFM()) return;
|
||||||
this.codeMirror?.replaceSelection(' ', 'end');
|
this.codeMirror?.replaceSelection(' ', 'end');
|
||||||
},
|
},
|
||||||
|
|
||||||
makeSpace : function() {
|
makeSpace : function() {
|
||||||
|
if(!this.isGFM()) return;
|
||||||
const selection = this.codeMirror?.getSelection();
|
const selection = this.codeMirror?.getSelection();
|
||||||
const t = selection.slice(0, 8) === '{{width:' && selection.slice(0 -4) === '% }}';
|
const t = selection.slice(0, 8) === '{{width:' && selection.slice(0 -4) === '% }}';
|
||||||
if(t){
|
if(t){
|
||||||
@@ -260,6 +284,7 @@ const CodeEditor = createReactClass({
|
|||||||
},
|
},
|
||||||
|
|
||||||
removeSpace : function() {
|
removeSpace : function() {
|
||||||
|
if(!this.isGFM()) return;
|
||||||
const selection = this.codeMirror?.getSelection();
|
const selection = this.codeMirror?.getSelection();
|
||||||
const t = selection.slice(0, 8) === '{{width:' && selection.slice(0 -4) === '% }}';
|
const t = selection.slice(0, 8) === '{{width:' && selection.slice(0 -4) === '% }}';
|
||||||
if(t){
|
if(t){
|
||||||
@@ -269,10 +294,12 @@ const CodeEditor = createReactClass({
|
|||||||
},
|
},
|
||||||
|
|
||||||
newColumn : function() {
|
newColumn : function() {
|
||||||
|
if(!this.isGFM()) return;
|
||||||
this.codeMirror?.replaceSelection('\n\\column\n\n', 'end');
|
this.codeMirror?.replaceSelection('\n\\column\n\n', 'end');
|
||||||
},
|
},
|
||||||
|
|
||||||
newPage : function() {
|
newPage : function() {
|
||||||
|
if(!this.isGFM()) return;
|
||||||
this.codeMirror?.replaceSelection('\n\\page\n\n', 'end');
|
this.codeMirror?.replaceSelection('\n\\page\n\n', 'end');
|
||||||
},
|
},
|
||||||
|
|
||||||
@@ -286,7 +313,8 @@ const CodeEditor = createReactClass({
|
|||||||
},
|
},
|
||||||
|
|
||||||
makeUnderline : function() {
|
makeUnderline : function() {
|
||||||
const selection = this.codeMirror?.getSelection(), t = selection.slice(0, 3) === '<u>' && selection.slice(-4) === '</u>';
|
if(!this.isGFM()) return;
|
||||||
|
const selection = this.codeMirror.getSelection(), t = selection.slice(0, 3) === '<u>' && selection.slice(-4) === '</u>';
|
||||||
this.codeMirror?.replaceSelection(t ? selection.slice(3, -4) : `<u>${selection}</u>`, 'around');
|
this.codeMirror?.replaceSelection(t ? selection.slice(3, -4) : `<u>${selection}</u>`, 'around');
|
||||||
if(selection.length === 0){
|
if(selection.length === 0){
|
||||||
const cursor = this.codeMirror?.getCursor();
|
const cursor = this.codeMirror?.getCursor();
|
||||||
@@ -295,7 +323,8 @@ const CodeEditor = createReactClass({
|
|||||||
},
|
},
|
||||||
|
|
||||||
makeSpan : function() {
|
makeSpan : function() {
|
||||||
const selection = this.codeMirror?.getSelection(), t = selection.slice(0, 2) === '{{' && selection.slice(-2) === '}}';
|
if(!this.isGFM()) return;
|
||||||
|
const selection = this.codeMirror.getSelection(), t = selection.slice(0, 2) === '{{' && selection.slice(-2) === '}}';
|
||||||
this.codeMirror?.replaceSelection(t ? selection.slice(2, -2) : `{{ ${selection}}}`, 'around');
|
this.codeMirror?.replaceSelection(t ? selection.slice(2, -2) : `{{ ${selection}}}`, 'around');
|
||||||
if(selection.length === 0){
|
if(selection.length === 0){
|
||||||
const cursor = this.codeMirror?.getCursor();
|
const cursor = this.codeMirror?.getCursor();
|
||||||
@@ -304,7 +333,8 @@ const CodeEditor = createReactClass({
|
|||||||
},
|
},
|
||||||
|
|
||||||
makeDiv : function() {
|
makeDiv : function() {
|
||||||
const selection = this.codeMirror?.getSelection(), t = selection.slice(0, 2) === '{{' && selection.slice(-2) === '}}';
|
if(!this.isGFM()) return;
|
||||||
|
const selection = this.codeMirror.getSelection(), t = selection.slice(0, 2) === '{{' && selection.slice(-2) === '}}';
|
||||||
this.codeMirror?.replaceSelection(t ? selection.slice(2, -2) : `{{\n${selection}\n}}`, 'around');
|
this.codeMirror?.replaceSelection(t ? selection.slice(2, -2) : `{{\n${selection}\n}}`, 'around');
|
||||||
if(selection.length === 0){
|
if(selection.length === 0){
|
||||||
const cursor = this.codeMirror?.getCursor();
|
const cursor = this.codeMirror?.getCursor();
|
||||||
@@ -317,7 +347,7 @@ const CodeEditor = createReactClass({
|
|||||||
let cursorPos;
|
let cursorPos;
|
||||||
let newComment;
|
let newComment;
|
||||||
const selection = this.codeMirror?.getSelection();
|
const selection = this.codeMirror?.getSelection();
|
||||||
if(this.props.language === 'gfm'){
|
if(this.isGFM()){
|
||||||
regex = /^\s*(<!--\s?)(.*?)(\s?-->)\s*$/gs;
|
regex = /^\s*(<!--\s?)(.*?)(\s?-->)\s*$/gs;
|
||||||
cursorPos = 4;
|
cursorPos = 4;
|
||||||
newComment = `<!-- ${selection} -->`;
|
newComment = `<!-- ${selection} -->`;
|
||||||
@@ -334,6 +364,7 @@ const CodeEditor = createReactClass({
|
|||||||
},
|
},
|
||||||
|
|
||||||
makeLink : function() {
|
makeLink : function() {
|
||||||
|
if(!this.isGFM()) return;
|
||||||
const isLink = /^\[(.*)\]\((.*)\)$/;
|
const isLink = /^\[(.*)\]\((.*)\)$/;
|
||||||
const selection = this.codeMirror?.getSelection().trim();
|
const selection = this.codeMirror?.getSelection().trim();
|
||||||
let match;
|
let match;
|
||||||
@@ -351,7 +382,8 @@ const CodeEditor = createReactClass({
|
|||||||
},
|
},
|
||||||
|
|
||||||
makeList : function(listType) {
|
makeList : function(listType) {
|
||||||
const selectionStart = this.codeMirror?.getCursor('from'), selectionEnd = this.codeMirror?.getCursor('to');
|
if(!this.isGFM()) return;
|
||||||
|
const selectionStart = this.codeMirror.getCursor('from'), selectionEnd = this.codeMirror.getCursor('to');
|
||||||
this.codeMirror?.setSelection(
|
this.codeMirror?.setSelection(
|
||||||
{ line: selectionStart.line, ch: 0 },
|
{ line: selectionStart.line, ch: 0 },
|
||||||
{ line: selectionEnd.line, ch: this.codeMirror?.getLine(selectionEnd.line).length }
|
{ line: selectionEnd.line, ch: this.codeMirror?.getLine(selectionEnd.line).length }
|
||||||
|
|||||||
@@ -86,9 +86,9 @@ const Editor = createReactClass({
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
const snippetBar = document.querySelector('.editor > .snippetBar');
|
const snippetBar = document.querySelector('.editor > .snippetBar');
|
||||||
if (!snippetBar) return;
|
if(!snippetBar) return;
|
||||||
|
|
||||||
this.resizeObserver = new ResizeObserver(entries => {
|
this.resizeObserver = new ResizeObserver(entries=>{
|
||||||
const height = document.querySelector('.editor > .snippetBar').offsetHeight;
|
const height = document.querySelector('.editor > .snippetBar').offsetHeight;
|
||||||
this.setState({ snippetBarHeight: height });
|
this.setState({ snippetBarHeight: height });
|
||||||
});
|
});
|
||||||
@@ -117,7 +117,7 @@ const Editor = createReactClass({
|
|||||||
},
|
},
|
||||||
|
|
||||||
componentWillUnmount() {
|
componentWillUnmount() {
|
||||||
if (this.resizeObserver) this.resizeObserver.disconnect();
|
if(this.resizeObserver) this.resizeObserver.disconnect();
|
||||||
},
|
},
|
||||||
|
|
||||||
handleControlKeys : function(e){
|
handleControlKeys : function(e){
|
||||||
@@ -337,7 +337,7 @@ const Editor = createReactClass({
|
|||||||
const brewRenderer = window.frames['BrewRenderer'].contentDocument.getElementsByClassName('brewRenderer')[0];
|
const brewRenderer = window.frames['BrewRenderer'].contentDocument.getElementsByClassName('brewRenderer')[0];
|
||||||
const currentPos = brewRenderer.scrollTop;
|
const currentPos = brewRenderer.scrollTop;
|
||||||
const targetPos = window.frames['BrewRenderer'].contentDocument.getElementById(`p${targetPage}`).getBoundingClientRect().top;
|
const targetPos = window.frames['BrewRenderer'].contentDocument.getElementById(`p${targetPage}`).getBoundingClientRect().top;
|
||||||
|
|
||||||
let scrollingTimeout;
|
let scrollingTimeout;
|
||||||
const checkIfScrollComplete = ()=>{ // Prevent interrupting a scroll in progress if user clicks multiple times
|
const checkIfScrollComplete = ()=>{ // Prevent interrupting a scroll in progress if user clicks multiple times
|
||||||
clearTimeout(scrollingTimeout); // Reset the timer every time a scroll event occurs
|
clearTimeout(scrollingTimeout); // Reset the timer every time a scroll event occurs
|
||||||
@@ -392,7 +392,7 @@ const Editor = createReactClass({
|
|||||||
|
|
||||||
isJumping = true;
|
isJumping = true;
|
||||||
checkIfScrollComplete();
|
checkIfScrollComplete();
|
||||||
if (this.codeEditor.current?.codeMirror) {
|
if(this.codeEditor.current?.codeMirror) {
|
||||||
this.codeEditor.current.codeMirror?.on('scroll', checkIfScrollComplete);
|
this.codeEditor.current.codeMirror?.on('scroll', checkIfScrollComplete);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -442,6 +442,7 @@ const Editor = createReactClass({
|
|||||||
<CodeEditor key='codeEditor'
|
<CodeEditor key='codeEditor'
|
||||||
ref={this.codeEditor}
|
ref={this.codeEditor}
|
||||||
language='gfm'
|
language='gfm'
|
||||||
|
tab='brewText'
|
||||||
view={this.state.view}
|
view={this.state.view}
|
||||||
value={this.props.brew.text}
|
value={this.props.brew.text}
|
||||||
onChange={this.props.onBrewChange('text')}
|
onChange={this.props.onBrewChange('text')}
|
||||||
@@ -455,6 +456,7 @@ const Editor = createReactClass({
|
|||||||
<CodeEditor key='codeEditor'
|
<CodeEditor key='codeEditor'
|
||||||
ref={this.codeEditor}
|
ref={this.codeEditor}
|
||||||
language='css'
|
language='css'
|
||||||
|
tab='brewStyles'
|
||||||
view={this.state.view}
|
view={this.state.view}
|
||||||
value={this.props.brew.style ?? DEFAULT_STYLE_TEXT}
|
value={this.props.brew.style ?? DEFAULT_STYLE_TEXT}
|
||||||
onChange={this.props.onBrewChange('style')}
|
onChange={this.props.onBrewChange('style')}
|
||||||
@@ -484,6 +486,7 @@ const Editor = createReactClass({
|
|||||||
<CodeEditor key='codeEditor'
|
<CodeEditor key='codeEditor'
|
||||||
ref={this.codeEditor}
|
ref={this.codeEditor}
|
||||||
language='gfm'
|
language='gfm'
|
||||||
|
tab='brewSnippets'
|
||||||
view={this.state.view}
|
view={this.state.view}
|
||||||
value={this.props.brew.snippets}
|
value={this.props.brew.snippets}
|
||||||
onChange={this.props.onBrewChange('snippets')}
|
onChange={this.props.onBrewChange('snippets')}
|
||||||
|
|||||||
Reference in New Issue
Block a user