-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathimageplugindialog.js
97 lines (79 loc) · 3.07 KB
/
imageplugindialog.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/**
* @fileoverview An simple Image dialog plugin.
*
*
*/
goog.provide('goog.editor.plugins.ImageDialogPlugin');
goog.provide('goog.editor.plugins.ImageDialogPlugin.Command');
goog.require('goog.editor.plugins.ImageDialog');
goog.require('goog.dom.TagName');
goog.require('goog.editor.plugins.AbstractDialogPlugin');
goog.require('goog.editor.range');
goog.require('goog.functions');
goog.require('goog.ui.editor.AbstractDialog.EventType');
// *** Public interface ***************************************************** //
/**
* A plugin that opens the Image dialog.
* @constructor
* @extends {goog.editor.plugins.AbstractDialogPlugin}
*/
goog.editor.plugins.ImageDialogPlugin = function() {
goog.editor.plugins.AbstractDialogPlugin.call(this,
goog.editor.plugins.ImageDialogPlugin.Command.IMAGE_PLUGIN_DIALOG);
};
goog.inherits(goog.editor.plugins.ImageDialogPlugin,
goog.editor.plugins.AbstractDialogPlugin);
/**
* Commands implemented by this plugin.
* @enum {string}
*/
goog.editor.plugins.ImageDialogPlugin.Command = {
IMAGE_PLUGIN_DIALOG: 'ImageDialog'
};
/** @inheritDoc */
goog.editor.plugins.ImageDialogPlugin.prototype.getTrogClassId =
goog.functions.constant('ImageDialog');
// *** Protected interface ************************************************** //
/**
* Creates a new instance of the dialog and registers for the relevant events.
* @param {goog.dom.DomHelper} dialogDomHelper The dom helper to be used to
* create the dialog.
* @return {goog.editor.plugins.ImageDialog} The dialog.
* @override
* @protected
*/
goog.editor.plugins.ImageDialogPlugin.prototype.createDialog = function(
dialogDomHelper) {
var dialog = new goog.editor.plugins.ImageDialog(dialogDomHelper);
dialog.addEventListener(goog.ui.editor.AbstractDialog.EventType.OK,
this.handleOk_,
false,
this);
return dialog;
};
// *** Private implementation *********************************************** //
/**
* Handles the OK event from the dialog by inserting the Image
* into the field.
* @param {goog.editor.plugins.ImageDialog.OkEvent} e OK event object.
* @private
*/
goog.editor.plugins.ImageDialogPlugin.prototype.handleOk_ = function(e) {
// Notify the editor that we are about to make changes.
this.fieldObject.dispatchBeforeChange();
// Create the image to insert.
var image = this.getFieldDomHelper().createElement(goog.dom.TagName.IMG);
// Grab the url of the image off of the event.
image.src = e.message;
// We want to insert the image in place of the user's selection.
// So we restore it first, and then use it for insertion.
this.restoreOriginalSelection();
var range = this.fieldObject.getRange();
image = range.replaceContentsWithNode(image);
// Done making changes, notify the editor.
this.fieldObject.dispatchChange();
// Put the user's selection right after the newly inserted image.
goog.editor.range.placeCursorNextTo(image, false);
// Dispatch selection change event since we just moved the selection.
this.fieldObject.dispatchSelectionChangeEvent();
};