1
0
mirror of https://github.com/chinchang/web-maker.git synced 2025-07-17 12:01:13 +02:00

refactor how codemirror options apply from settings.

Now Usercodemirror component handles each own option
updation.
This commit is contained in:
Kushagra Gour
2018-10-07 17:49:58 +05:30
parent abdb287b1c
commit ca77303d68
5 changed files with 62 additions and 72 deletions

View File

@@ -288,50 +288,25 @@ export default class ContentWrap extends Component {
]).then(() => this.setPreviewContent(true)); ]).then(() => this.setPreviewContent(true));
} }
applyCodemirrorSettings(prefs) { applyCodemirrorSettings(prefs) {
if (!this.cm) { if (window.consoleEl) {
return; window.consoleEl.querySelector(
'.CodeMirror'
).style.fontSize = `${parseInt(prefs.fontSize, 10)}px`;
} }
htmlCodeEl.querySelector(
'.CodeMirror'
).style.fontSize = cssCodeEl.querySelector(
'.CodeMirror'
).style.fontSize = jsCodeEl.querySelector(
'.CodeMirror'
).style.fontSize = `${parseInt(prefs.fontSize, 10)}px`;
window.consoleEl.querySelector('.CodeMirror').style.fontSize = `${parseInt(
prefs.fontSize,
10
)}px`;
// Replace correct css file in LINK tags's href // Replace correct css file in LINK tags's href
window.editorThemeLinkTag.href = `lib/codemirror/theme/${ if (prefs.editorTheme) {
prefs.editorTheme window.editorThemeLinkTag.href = `lib/codemirror/theme/${
}.css`; prefs.editorTheme
}.css`;
}
window.fontStyleTag.textContent = window.fontStyleTemplate.textContent.replace( window.fontStyleTag.textContent = window.fontStyleTemplate.textContent.replace(
/fontname/g, /fontname/g,
(prefs.editorFont === 'other' (prefs.editorFont === 'other'
? prefs.editorCustomFont ? prefs.editorCustomFont
: prefs.editorFont) || 'FiraCode' : prefs.editorFont) || 'FiraCode'
); );
// window.customEditorFontInput.classList[
// prefs.editorFont === 'other' ? 'remove' : 'add'
// ]('hide');
this.consoleCm.setOption('theme', prefs.editorTheme);
['html', 'js', 'css'].forEach(type => {
this.cm[type].setOption('indentWithTabs', prefs.indentWith !== 'spaces');
this.cm[type].setOption(
'blastCode',
prefs.isCodeBlastOn ? { effect: 2, shake: false } : false
);
this.cm[type].setOption('indentUnit', +prefs.indentSize);
this.cm[type].setOption('tabSize', +prefs.indentSize);
this.cm[type].setOption('theme', prefs.editorTheme);
this.cm[type].setOption('keyMap', prefs.keymap);
this.cm[type].setOption('lineWrapping', prefs.lineWrap);
this.cm[type].refresh();
});
} }
// Check all the code wrap if they are minimized or maximized // Check all the code wrap if they are minimized or maximized

View File

@@ -185,7 +185,7 @@ export default class ContentWrapFiles extends Component {
} }
if (!this.props.prefs.preserveConsoleLogs) { if (!this.props.prefs.preserveConsoleLogs) {
// this.clearConsole(); this.clearConsole();
} }
this.cleanupErrors(); this.cleanupErrors();
@@ -221,7 +221,7 @@ export default class ContentWrapFiles extends Component {
this.cm.refresh(); this.cm.refresh();
window.cm = this.cm; window.cm = this.cm;
// this.clearConsole(); this.clearConsole();
// Set preview only when all modes are updated so that preview doesn't generate on partially // Set preview only when all modes are updated so that preview doesn't generate on partially
// correct modes and also doesn't happen 3 times. // correct modes and also doesn't happen 3 times.
@@ -230,41 +230,25 @@ export default class ContentWrapFiles extends Component {
); );
} }
applyCodemirrorSettings(prefs) { applyCodemirrorSettings(prefs) {
if (!this.cm) { if (window.consoleEl) {
return; window.consoleEl.querySelector(
'.CodeMirror'
).style.fontSize = `${parseInt(prefs.fontSize, 10)}px`;
} }
htmlCodeEl.querySelector('.CodeMirror').style.fontSize = `${parseInt(
prefs.fontSize,
10
)}px`;
// Replace correct css file in LINK tags's href // Replace correct css file in LINK tags's href
window.editorThemeLinkTag.href = `lib/codemirror/theme/${ if (prefs.editorTheme) {
prefs.editorTheme window.editorThemeLinkTag.href = `lib/codemirror/theme/${
}.css`; prefs.editorTheme
}.css`;
}
window.fontStyleTag.textContent = window.fontStyleTemplate.textContent.replace( window.fontStyleTag.textContent = window.fontStyleTemplate.textContent.replace(
/fontname/g, /fontname/g,
(prefs.editorFont === 'other' (prefs.editorFont === 'other'
? prefs.editorCustomFont ? prefs.editorCustomFont
: prefs.editorFont) || 'FiraCode' : prefs.editorFont) || 'FiraCode'
); );
// window.customEditorFontInput.classList[
// prefs.editorFont === 'other' ? 'remove' : 'add'
// ]('hide');
// this.consoleCm.setOption('theme', prefs.editorTheme);
this.cm.setOption('indentWithTabs', prefs.indentWith !== 'spaces');
this.cm.setOption(
'blastCode',
prefs.isCodeBlastOn ? { effect: 2, shake: false } : false
);
this.cm.setOption('indentUnit', +prefs.indentSize);
this.cm.setOption('tabSize', +prefs.indentSize);
this.cm.setOption('theme', prefs.editorTheme);
this.cm.setOption('keyMap', prefs.keymap);
this.cm.setOption('lineWrapping', prefs.lineWrap);
this.cm.refresh();
} }
// Check all the code wrap if they are minimized or maximized // Check all the code wrap if they are minimized or maximized

View File

@@ -6,7 +6,6 @@ function FileIcon({ fileName }) {
if (!fileName) fileName = 'sd.sd'; if (!fileName) fileName = 'sd.sd';
const type = fileName.match(/.(\w+)$/)[1]; const type = fileName.match(/.(\w+)$/)[1];
console.log(type);
switch (type) { switch (type) {
case 'html': case 'html':
return ( return (

View File

@@ -38,27 +38,59 @@ emmet(CodeMirror);
export default class UserCodeMirror extends Component { export default class UserCodeMirror extends Component {
componentDidMount() { componentDidMount() {
this.initEditor(); this.initEditor();
this.textarea.parentNode.querySelector(
'.CodeMirror'
).style.fontSize = `${parseInt(this.props.prefs.fontSize, 10)}px`;
} }
shouldComponentUpdate() { shouldComponentUpdate(nextProps) {
if (nextProps.prefs !== this.props.prefs) {
const { prefs } = nextProps;
console.log('updating', nextProps.options.mode);
this.cm.setOption('indentWithTabs', prefs.indentWith !== 'spaces');
this.cm.setOption(
'blastCode',
prefs.isCodeBlastOn ? { effect: 2, shake: false } : false
);
this.cm.setOption('theme', prefs.editorTheme);
this.cm.setOption('indentUnit', +prefs.indentSize);
this.cm.setOption('tabSize', +prefs.indentSize);
this.cm.setOption('keyMap', prefs.keymap);
this.cm.setOption('lineWrapping', prefs.lineWrap);
if (this.textarea) {
this.textarea.parentNode.querySelector(
'.CodeMirror'
).style.fontSize = `${parseInt(prefs.fontSize, 10)}px`;
}
this.cm.refresh();
}
return false; return false;
} }
initEditor() { initEditor() {
const options = this.props.options; const { options, prefs } = this.props;
console.log(100, prefs.lineWrap);
this.cm = CodeMirror.fromTextArea(this.textarea, { this.cm = CodeMirror.fromTextArea(this.textarea, {
mode: options.mode, mode: options.mode,
lineNumbers: true, lineNumbers: true,
lineWrapping: true, lineWrapping: !!prefs.lineWrap,
autofocus: options.autofocus || false, autofocus: options.autofocus || false,
autoCloseBrackets: true, autoCloseBrackets: true,
autoCloseTags: true, autoCloseTags: true,
matchBrackets: true, matchBrackets: true,
matchTags: options.matchTags || false, matchTags: options.matchTags || false,
tabMode: 'indent', tabMode: 'indent',
keyMap: 'sublime', keyMap: prefs.keyMap || 'sublime',
theme: 'monokai', theme: prefs.editorTheme || 'monokai',
lint: !!options.lint, lint: !!options.lint,
tabSize: 2, tabSize: +prefs.indentSize || 2,
indentWithTabs: prefs.indentWith !== 'spaces',
indentUnit: +prefs.indentSize,
foldGutter: true, foldGutter: true,
styleActiveLine: true, styleActiveLine: true,
gutters: options.gutters || [], gutters: options.gutters || [],
@@ -91,7 +123,7 @@ export default class UserCodeMirror extends Component {
const input = $('[data-setting=indentWith]:checked'); const input = $('[data-setting=indentWith]:checked');
if ( if (
!editor.somethingSelected() && !editor.somethingSelected() &&
(!input || input.value === 'spaces') (!prefs.indentWith || prefs.indentWith === 'spaces')
) { ) {
// softtabs adds spaces. This is required because by default tab key will put tab, but we want // softtabs adds spaces. This is required because by default tab key will put tab, but we want
// to indent with spaces if `spaces` is preferred mode of indentation. // to indent with spaces if `spaces` is preferred mode of indentation.

View File

@@ -195,7 +195,7 @@ export default class App extends Component {
this.createNewItem(); this.createNewItem();
} }
Object.assign(this.state.prefs, result); Object.assign(this.state.prefs, result);
this.setState({ prefs: this.state.prefs }); this.setState({ prefs: { ...this.state.prefs } });
this.updateSetting(); this.updateSetting();
}); });