mirror of
https://github.com/TriliumNext/Notes.git
synced 2025-08-11 11:02:27 +08:00
Add display toggle
This commit is contained in:
parent
13a10dcfdd
commit
a89cadeac5
@ -2,7 +2,7 @@ import Command from '@ckeditor/ckeditor5-core/src/command';
|
|||||||
import { getSelectedMathModelWidget } from './utils';
|
import { getSelectedMathModelWidget } from './utils';
|
||||||
|
|
||||||
export default class MathCommand extends Command {
|
export default class MathCommand extends Command {
|
||||||
execute( equation ) {
|
execute( equation, display ) {
|
||||||
const model = this.editor.model;
|
const model = this.editor.model;
|
||||||
const selection = model.document.selection;
|
const selection = model.document.selection;
|
||||||
const selectedElement = selection.getSelectedElement();
|
const selectedElement = selection.getSelectedElement();
|
||||||
@ -12,11 +12,10 @@ export default class MathCommand extends Command {
|
|||||||
if ( selectedElement && selectedElement.is( 'mathtex' ) ) {
|
if ( selectedElement && selectedElement.is( 'mathtex' ) ) {
|
||||||
// Update selected element
|
// Update selected element
|
||||||
const mode = selectedElement.getAttribute( 'mode' );
|
const mode = selectedElement.getAttribute( 'mode' );
|
||||||
const display = selectedElement.getAttribute( 'display' );
|
|
||||||
mathtex = writer.createElement( 'mathtex', { equation, mode, display } );
|
mathtex = writer.createElement( 'mathtex', { equation, mode, display } );
|
||||||
} else {
|
} else {
|
||||||
// Create new model element
|
// Create new model element
|
||||||
mathtex = writer.createElement( 'mathtex', { equation, mode: 'script', display: true } );
|
mathtex = writer.createElement( 'mathtex', { equation, mode: 'script', display } );
|
||||||
}
|
}
|
||||||
model.insertContent( mathtex );
|
model.insertContent( mathtex );
|
||||||
writer.setSelection( mathtex, 'on' );
|
writer.setSelection( mathtex, 'on' );
|
||||||
@ -31,6 +30,8 @@ export default class MathCommand extends Command {
|
|||||||
this.isEnabled = isAllowed;
|
this.isEnabled = isAllowed;
|
||||||
|
|
||||||
const selectedEquation = getSelectedMathModelWidget( selection );
|
const selectedEquation = getSelectedMathModelWidget( selection );
|
||||||
|
console.log(selectedEquation);
|
||||||
this.value = selectedEquation ? selectedEquation.getAttribute( 'equation' ) : null;
|
this.value = selectedEquation ? selectedEquation.getAttribute( 'equation' ) : null;
|
||||||
|
this.display = selectedEquation ? selectedEquation.getAttribute( 'display' ) : null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -107,11 +107,13 @@ export default class MathEditing extends Plugin {
|
|||||||
const equation = modelItem.getAttribute( 'equation' );
|
const equation = modelItem.getAttribute( 'equation' );
|
||||||
const display = modelItem.getAttribute( 'display' );
|
const display = modelItem.getAttribute( 'display' );
|
||||||
|
|
||||||
|
const styles = 'user-select: none; ' + (display ? 'display: block;' : 'display: inline-block;');
|
||||||
|
const classes = 'ck-math-tex ' + (display ? 'ck-math-tex-display' : 'ck-math-tex-inline');
|
||||||
|
|
||||||
// CKEngine render multiple times if using span instead of div
|
// CKEngine render multiple times if using span instead of div
|
||||||
const mathtexView = viewWriter.createContainerElement( 'div', {
|
const mathtexView = viewWriter.createContainerElement( 'div', {
|
||||||
style: display ? 'display: block;' : 'display: inline-block;',
|
style: styles,
|
||||||
class: 'mathtex'
|
class: classes
|
||||||
} );
|
} );
|
||||||
|
|
||||||
// Div is formatted as parent container
|
// Div is formatted as parent container
|
||||||
|
@ -62,10 +62,11 @@ export default class MathUI extends Plugin {
|
|||||||
const formView = new MainFormView( editor.locale, engine );
|
const formView = new MainFormView( editor.locale, engine );
|
||||||
|
|
||||||
formView.mathInputView.bind( 'value' ).to( mathCommand, 'value' );
|
formView.mathInputView.bind( 'value' ).to( mathCommand, 'value' );
|
||||||
|
formView.displayButtonView.bind( 'displayIsOn' ).to( mathCommand, 'display');
|
||||||
|
|
||||||
// Listen to 'submit' button click
|
// Listen to submit button click
|
||||||
this.listenTo( formView, 'submit', () => {
|
this.listenTo( formView, 'submit', () => {
|
||||||
editor.execute( 'math', formView.equation );
|
editor.execute( 'math', formView.equation, formView.displayButtonView.isOn );
|
||||||
this._closeFormView();
|
this._closeFormView();
|
||||||
} );
|
} );
|
||||||
|
|
||||||
@ -101,6 +102,7 @@ export default class MathUI extends Plugin {
|
|||||||
}
|
}
|
||||||
|
|
||||||
this._form.equation = mathCommand.value || '';
|
this._form.equation = mathCommand.value || '';
|
||||||
|
this._form.displayButtonView.isOn = mathCommand.display || false;
|
||||||
}
|
}
|
||||||
|
|
||||||
_hideUI() {
|
_hideUI() {
|
||||||
|
@ -2,6 +2,7 @@ import View from '@ckeditor/ckeditor5-ui/src/view';
|
|||||||
import ViewCollection from '@ckeditor/ckeditor5-ui/src/viewcollection';
|
import ViewCollection from '@ckeditor/ckeditor5-ui/src/viewcollection';
|
||||||
|
|
||||||
import ButtonView from '@ckeditor/ckeditor5-ui/src/button/buttonview';
|
import ButtonView from '@ckeditor/ckeditor5-ui/src/button/buttonview';
|
||||||
|
import SwitchButtonView from '@ckeditor/ckeditor5-ui/src/button/buttonview';
|
||||||
import LabeledInputView from '@ckeditor/ckeditor5-ui/src/labeledinput/labeledinputview';
|
import LabeledInputView from '@ckeditor/ckeditor5-ui/src/labeledinput/labeledinputview';
|
||||||
import InputTextView from '@ckeditor/ckeditor5-ui/src/inputtext/inputtextview';
|
import InputTextView from '@ckeditor/ckeditor5-ui/src/inputtext/inputtextview';
|
||||||
import LabelView from '@ckeditor/ckeditor5-ui/src/label/labelview';
|
import LabelView from '@ckeditor/ckeditor5-ui/src/label/labelview';
|
||||||
@ -38,6 +39,9 @@ export default class MainFormView extends View {
|
|||||||
// Math element
|
// Math element
|
||||||
this.mathView = new MathView( engine, locale );
|
this.mathView = new MathView( engine, locale );
|
||||||
|
|
||||||
|
// Display button
|
||||||
|
this.displayButtonView = this._createDisplayButton();
|
||||||
|
|
||||||
// Submit button
|
// Submit button
|
||||||
this.saveButtonView = this._createButton( t( 'Save' ), checkIcon, 'ck-button-save', null );
|
this.saveButtonView = this._createButton( t( 'Save' ), checkIcon, 'ck-button-save', null );
|
||||||
this.saveButtonView.type = 'submit';
|
this.saveButtonView.type = 'submit';
|
||||||
@ -66,6 +70,7 @@ export default class MainFormView extends View {
|
|||||||
},
|
},
|
||||||
children: [
|
children: [
|
||||||
this.mathInputView,
|
this.mathInputView,
|
||||||
|
this.displayButtonView,
|
||||||
this.previewLabel,
|
this.previewLabel,
|
||||||
this.mathView
|
this.mathView
|
||||||
]
|
]
|
||||||
@ -87,6 +92,7 @@ export default class MainFormView extends View {
|
|||||||
// Register form elements to focusable elements
|
// Register form elements to focusable elements
|
||||||
const childViews = [
|
const childViews = [
|
||||||
this.mathInputView,
|
this.mathInputView,
|
||||||
|
this.displayButtonView,
|
||||||
this.saveButtonView,
|
this.saveButtonView,
|
||||||
this.cancelButtonView,
|
this.cancelButtonView,
|
||||||
];
|
];
|
||||||
@ -164,4 +170,33 @@ export default class MainFormView extends View {
|
|||||||
|
|
||||||
return button;
|
return button;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_createDisplayButton() {
|
||||||
|
const t = this.locale.t;
|
||||||
|
|
||||||
|
const switchButton = new SwitchButtonView( this.locale );
|
||||||
|
|
||||||
|
switchButton.set( {
|
||||||
|
label: t( 'Display mode' ),
|
||||||
|
withText: true
|
||||||
|
} );
|
||||||
|
|
||||||
|
switchButton.extendTemplate( {
|
||||||
|
attributes: {
|
||||||
|
class: 'ck-button-display-toggle'
|
||||||
|
}
|
||||||
|
} );
|
||||||
|
|
||||||
|
switchButton.bind( 'isOn' ).to( this, 'displayIsOn' );
|
||||||
|
|
||||||
|
switchButton.on( 'execute', () => {
|
||||||
|
// Toggle state
|
||||||
|
this.set('displayIsOn', !this.displayIsOn);
|
||||||
|
|
||||||
|
// Update preview view
|
||||||
|
this.mathView.display = this.displayIsOn;
|
||||||
|
} );
|
||||||
|
|
||||||
|
return switchButton;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -8,8 +8,9 @@ export default class MathView extends View {
|
|||||||
this.engine = engine;
|
this.engine = engine;
|
||||||
|
|
||||||
this.set( 'value', '' );
|
this.set( 'value', '' );
|
||||||
|
this.set( 'display', false);
|
||||||
|
|
||||||
this.on( 'change:value', () => {
|
this.on( 'change', () => {
|
||||||
this.updateMath();
|
this.updateMath();
|
||||||
} );
|
} );
|
||||||
|
|
||||||
@ -25,7 +26,7 @@ export default class MathView extends View {
|
|||||||
}
|
}
|
||||||
|
|
||||||
updateMath() {
|
updateMath() {
|
||||||
renderEquation( this.value, this.element, this.engine );
|
renderEquation( this.value, this.element, this.engine, this.display );
|
||||||
}
|
}
|
||||||
|
|
||||||
render() {
|
render() {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user