0
0
mirror of https://github.com/naturalcrit/homebrewery.git synced 2026-03-22 08:58:11 +00:00

Wrapper functions

This commit is contained in:
David Bolack
2025-12-29 13:24:31 -06:00
parent c0c0984c5c
commit 6b57dcf052
2 changed files with 36 additions and 16 deletions

View File

@@ -50,6 +50,7 @@ const CodeEditor = createClass({
getDefaultProps : function() {
return {
language : '',
tab : 'brewText',
value : '',
wrap : true,
onChange : ()=>{},
@@ -193,6 +194,22 @@ const CodeEditor = createClass({
this.updateSize();
},
// Use for GFM tabs that use common hotkeys
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 () {
const cm = this.codeMirror;
if(cm.somethingSelected()) {
@@ -207,7 +224,7 @@ const CodeEditor = createClass({
},
makeHeader : function (number) {
if(this.props.language !== 'gfm') return;
if(!this.isGFM()) return;
const selection = this.codeMirror.getSelection();
const header = Array(number).fill('#').join('');
this.codeMirror.replaceSelection(`${header} ${selection}`, 'around');
@@ -216,7 +233,7 @@ const CodeEditor = createClass({
},
makeBold : function() {
if(this.props.language !== 'gfm') return;
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');
if(selection.length === 0){
@@ -226,7 +243,7 @@ const CodeEditor = createClass({
},
makeItalic : function() {
if(this.props.language !== 'gfm') return;
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');
if(selection.length === 0){
@@ -236,7 +253,7 @@ const CodeEditor = createClass({
},
makeSuper : function() {
if(this.props.language !== 'gfm') return;
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');
if(selection.length === 0){
@@ -246,7 +263,7 @@ const CodeEditor = createClass({
},
makeSub : function() {
if(this.props.language !== 'gfm') return;
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');
if(selection.length === 0){
@@ -257,12 +274,12 @@ const CodeEditor = createClass({
makeNbsp : function() {
if(this.props.language !== 'gfm') return;
if(!this.isGFM()) return;
this.codeMirror.replaceSelection(' ', 'end');
},
makeSpace : function() {
if(this.props.language !== 'gfm') return;
if(!this.isGFM()) return;
const selection = this.codeMirror.getSelection();
const t = selection.slice(0, 8) === '{{width:' && selection.slice(0 -4) === '% }}';
if(t){
@@ -274,7 +291,7 @@ const CodeEditor = createClass({
},
removeSpace : function() {
if(this.props.language !== 'gfm') return;
if(!this.isGFM()) return;
const selection = this.codeMirror.getSelection();
const t = selection.slice(0, 8) === '{{width:' && selection.slice(0 -4) === '% }}';
if(t){
@@ -284,12 +301,12 @@ const CodeEditor = createClass({
},
newColumn : function() {
if(this.props.language !== 'gfm') return;
if(!this.isGFM()) return;
this.codeMirror.replaceSelection('\n\\column\n\n', 'end');
},
newPage : function() {
if(this.props.language !== 'gfm') return;
if(!this.isGFM()) return;
this.codeMirror.replaceSelection('\n\\page\n\n', 'end');
},
@@ -303,7 +320,7 @@ const CodeEditor = createClass({
},
makeUnderline : function() {
if(this.props.language !== 'gfm') return;
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');
if(selection.length === 0){
@@ -313,7 +330,7 @@ const CodeEditor = createClass({
},
makeSpan : function() {
if(this.props.language !== 'gfm') return;
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');
if(selection.length === 0){
@@ -323,7 +340,7 @@ const CodeEditor = createClass({
},
makeDiv : function() {
if(this.props.language !== 'gfm') return;
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');
if(selection.length === 0){
@@ -337,7 +354,7 @@ const CodeEditor = createClass({
let cursorPos;
let newComment;
const selection = this.codeMirror.getSelection();
if(this.props.language === 'gfm'){
if(this.isGFM()){
regex = /^\s*(<!--\s?)(.*?)(\s?-->)\s*$/gs;
cursorPos = 4;
newComment = `<!-- ${selection} -->`;
@@ -354,7 +371,7 @@ const CodeEditor = createClass({
},
makeLink : function() {
if(this.props.language !== 'gfm') return;
if(!this.isGFM()) return;
const isLink = /^\[(.*)\]\((.*)\)$/;
const selection = this.codeMirror.getSelection().trim();
let match;
@@ -372,7 +389,7 @@ const CodeEditor = createClass({
},
makeList : function(listType) {
if(this.props.language !== 'gfm') return;
if(!this.isGFM()) return;
const selectionStart = this.codeMirror.getCursor('from'), selectionEnd = this.codeMirror.getCursor('to');
this.codeMirror.setSelection(
{ line: selectionStart.line, ch: 0 },

View File

@@ -440,6 +440,7 @@ const Editor = createClass({
<CodeEditor key='codeEditor'
ref={this.codeEditor}
language='gfm'
tab='brewText'
view={this.state.view}
value={this.props.brew.text}
onChange={this.props.onBrewChange('text')}
@@ -453,6 +454,7 @@ const Editor = createClass({
<CodeEditor key='codeEditor'
ref={this.codeEditor}
language='css'
tab='brewStyles'
view={this.state.view}
value={this.props.brew.style ?? DEFAULT_STYLE_TEXT}
onChange={this.props.onBrewChange('style')}
@@ -482,6 +484,7 @@ const Editor = createClass({
<CodeEditor key='codeEditor'
ref={this.codeEditor}
language='gfm'
tab='brewSnippets'
view={this.state.view}
value={this.props.brew.snippets}
onChange={this.props.onBrewChange('snippets')}