Editor
- Responder
- Editor
Click in the input field for the color. Choose a color. Press Enter or click outside the selector to validate the color you have chosen. The plate changes of color.
The Undo button is enabled.
Choose another color. Directly enter a value, e.g. #CB6
.
Click the Undo button.
The previous color is displayed.
The Redo button is enabled.
Click on the Redo button to go back to the last selected color.
Reload the page. The color of the plate is saved.
In the console of the browser, lock the whole interface by blocking the editor:
editor.disable()
Unlock the interface:
editor.enable()
Display the value of the model:
model.get()
Change the value of the model:
model.set({ color: "#E02" })
The interface is updated. Check if the value of the model is normalized:
model.get()
Object { color: "#ee0022" }
Disable the automatic synchronization:
model.disableSync()
Select another color. Reload the page. The modification isn't saved.
- function Editor(model, view, inspectors, panel = false) {
- Responder.call(this);
- model.addListener(this);
- this._model = model;
- if (view)
- view.addListener(this);
- this._view = view;
- for (let p in inspectors)
- inspectors[p].addNextResponder(this);
- this._inspectors = inspectors;
- if (panel) {
- panel.addNextResponder(this);
- model.enableUndo();
- }
- this._panel = panel;
- }
- Editor.prototype = Object.create(Responder.prototype);
- Object.defineProperty(Editor.prototype, 'constructor', { value: Editor, enumerable: false, writable: true });
- Editor.prototype.modelSet = function(sender) {
- let options = sender.get();
- if (this._view)
- this._view.set(options);
- let inspectors = this._inspectors;
- for (let p in inspectors)
- inspectors[p].set(options[p]);
- };
- Editor.prototype.modelValueChanged = function(sender, prop, val) {
- if (this._view)
- this._view.setValue(prop, val);
- this._inspectors[prop].set(val);
- };
- Editor.prototype.modelUndoChanged = function(sender) {
- if (this._panel) {
- if (this._model.canUndo())
- this._panel.enableUndo();
- else
- this._panel.disableUndo();
- if (this._model.canRedo())
- this._panel.enableRedo();
- else
- this._panel.disableRedo();
- }
- };
- Editor.prototype.undo = function(sender) {
- this._model.undo();
- return true;
- };
- Editor.prototype.redo = function(sender) {
- this._model.redo();
- return true;
- };
- Editor.prototype.inspectorValueChanged = function(sender) {
- let inspectors = this._inspectors;
- for (let p in inspectors) {
- if (inspectors[p] === sender) {
- this._model.setValue(p, sender.get());
- break;
- }
- }
- return true;
- };
- Editor.prototype.disable = function() {
- let inspectors = this._inspectors;
- for (let p in inspectors)
- inspectors[p].disable();
- if (this._panel)
- this._panel.disable();
- return this;
- };
- Editor.prototype.enable = function() {
- let inspectors = this._inspectors;
- for (let p in inspectors)
- inspectors[p].enable();
- if (this._panel) {
- if (this._model.canUndo())
- this._panel.enableUndo();
- if (this._model.canRedo())
- this._panel.enableRedo();
- }
- return this;
- };
- <?php $text='Objective.js'; ?>
- <?php $font='Slackey'; ?>
- <?php $fontSize=24; ?>
- <?php $color='#cccccc'; ?>
- <?php head('font', $font); ?>
- <?php head('javascript', 'jquery.minicolors'); ?>
- <?php head('stylesheet', 'jquery.minicolors', 'screen'); ?>
- <?php $id=uniqid('id'); ?>
- .test_display {
- width: 240px;
- height: 135px;
- display: flex;
- margin-bottom: 10px;
- border-radius: 3px;
- }
- <div id="<?php echo $id; ?>" class="noprint">
- <div class="test_display">
- <canvas width="240" height="135"></canvas>
- </div>
- <div class="ojs">
- <div>
- <div class="control_panel">
- <button type="submit" class="ojs_button narrow" disabled><i class="fas fa-undo"></i></button>
- <button type="submit" class="ojs_button narrow" disabled><i class="fas fa-redo"></i></button>
- </div>
- <span class="color_panel"></span>
- </div>
- </div>
- </div>
- <?php head('javascript', '/objectivejs/Objective.js'); ?>
- <?php head('javascript', '/objectivejs/Validator.js'); ?>
- <?php head('javascript', '/objectivejs/Responder.js'); ?>
- <?php head('javascript', '/objectivejs/View.js'); ?>
- <?php head('javascript', '/objectivejs/Inspector.js'); ?>
- <?php head('javascript', '/objectivejs/ColorInspector.js'); ?>
- <?php head('javascript', '/objectivejs/Model.js'); ?>
- <?php head('javascript', '/objectivejs/Undo.js'); ?>
- <?php head('javascript', '/objectivejs/Panel.js'); ?>
- <?php head('javascript', '/objectivejs/UndoPanel.js'); ?>
- <?php head('javascript', '/objectivejs/Editor.js'); ?>
- <?php head('javascript', '/objectivejs/ModelStorageDelegate.js'); ?>
- function ColorModel(name) {
- Model.call(this, name);
- this._value = {
- color: ColorModel.defaultColor
- };
- }
- ColorModel.prototype = Object.create(Model.prototype);
- Object.defineProperty(ColorModel.prototype, 'constructor', { value: ColorModel, enumerable: false, writable: true });
- ColorModel.defaultColor = '<?php echo $color; ?>';
- ColorModel.prototype.validateValue = function(prop, val) {
- if (prop == 'color')
- return Validator.validateColor(val);
- return false;
- }
- ColorModel.prototype.normalizeValue = function(prop, val) {
- if (prop == 'color')
- val = Validator.normalizeColor(val);
- return val;
- }
- function ColorView() {
- View.call(this);
- this._color = ColorModel.defaultColor;
- this._canvas = null;
- }
- ColorView.prototype = Object.create(View.prototype);
- Object.defineProperty(ColorView.prototype, 'constructor', { value: ColorView, enumerable: false, writable: true });
- ColorView.prototype.set = function(options) {
- const {color} = options;
- this.setColor(color);
- return this;
- }
- ColorView.prototype.setValue = function(prop, val) {
- if (prop == 'color')
- this.setColor(val);
- return this;
- }
- ColorView.prototype.setColor = function(color) {
- if (this._color != color) {
- this._color = color;
- if (this._widget)
- this._widget.style.backgroundColor = color;
- }
- return this;
- }
- ColorView.prototype.resetWidget = function() {
- if (this._widget)
- this._widget.style.backgroundColor = this._color;
- return this;
- }
- ColorView.prototype.setWidget = function(w) {
- const canvas = w.querySelector('canvas');
- if (canvas === null)
- throw new TypeError();
- this._canvas = canvas;
- View.prototype.setWidget.call(this, w);
- return this;
- }
- ColorView.prototype.drawWidget = function(font, fontSize, text) {
- if (this._canvas) {
- let canvas = this._canvas;
- let ctx = canvas.getContext('2d');
- ctx.font = `bold ${fontSize}px "${font}"`;
- ctx.fillStyle = 'white';
- ctx.textAlign = 'center';
- ctx.textBaseline = 'middle';
- ctx.fillText('', canvas.width / 2, canvas.height / 2);
- ctx.beginPath();
- ctx.arc(20, 20, 10, 0, 2 * Math.PI);
- ctx.fill();
- ctx.beginPath();
- ctx.arc(canvas.width-20, 20, 10, 0, 2 * Math.PI);
- ctx.fill();
- ctx.beginPath();
- ctx.arc(canvas.width-20, canvas.height-20, 10, 0, 2 * Math.PI);
- ctx.fill();
- ctx.beginPath();
- ctx.arc(20, canvas.height-20, 10, 0, 2 * Math.PI);
- ctx.fill();
- document.fonts.ready.then(() => {
- ctx.fillText(text, canvas.width / 2, canvas.height / 2);
- });
- }
- return this;
- }
- const model = new ColorModel('color');
- const container = document.querySelector('#<?php echo $id; ?>');
- const panel = new UndoPanel();
- panel.setManagedWidget(container.querySelector('.control_panel')).resetWidget();
- const view = new ColorView();
- view.setWidget(container.querySelector('.test_display')).resetWidget();
- view.drawWidget('<?php echo $font; ?>', <?php echo $fontSize; ?>, '<?php echo $text; ?>');
- const colorInspector = new ColorInspector(model.getValue('color'));
- colorInspector.createManagedWidget(container.querySelector('.color_panel'));
- const inspectors = {
- color: colorInspector
- }
- const editor = new Editor(model, view, inspectors, panel);
- model.setDelegate(new ModelStorageDelegate());
- model.readIn();
- model.enableSync();
SEE ALSO
Model, Responder, ColorInspector, ClipEditor, Architecture of an editor
Comments