forked from adobe/brackets
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCSSShapesEditorProvider.js
More file actions
155 lines (126 loc) · 5.39 KB
/
CSSShapesEditorProvider.js
File metadata and controls
155 lines (126 loc) · 5.39 KB
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/*
* Copyright (c) 2013 Adobe Systems Incorporated.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
/*jslint vars: true, plusplus: true, devel: true, nomen: true, regexp: true, indent: 4, maxerr: 50, browser: true */
/*global window, CSSShapesEditor */
/*
Register CSSShapesEditor as a provider for editing shape properties.
A provider is just a wrapper over custom editors to provide a common interface.
All providers MUST implement at a minimum this interface:
{
// turns on editor on specified target HTMLElement.
// picks-up necessary setup args from model
setup: function (target, model) {},
// update the editor state given the provided model
update: function (model) {},
// turn off the editor and remove any scaffolding
remove: function () {},
// sets a callback to be called with the new value
onValueChange: function (callback) {}
}
*/
(function () {
"use strict";
if (!window._LD_CSS_EDITOR || typeof window._LD_CSS_EDITOR.registerProvider !== "function") {
throw new Error("Missing LiveEditorRemoteDriver from Brackets");
}
if (!window.CSSShapesEditor) {
throw new Error("Missing CSSShapesEditor");
}
var _onKeydown;
function Provider() {}
Provider.prototype.setup = function (target, model) {
var scope = this,
options = {};
switch (model.property) {
case "shape-inside":
case "-webkit-shape-inside":
options.defaultRefBox = "content-box";
break;
case "clip-path":
case "-webkit-clip-path":
options.defaultRefBox = "border-box";
break;
default:
options.defaultRefBox = "margin-box";
}
/*
@private
Handle keydown events when a polygon editor is active.
Declared globally within module so it can be removed by Provider.remove()
Defined here so it can access scope.inst
T key toggles the free transform editor (scale/rotate)
Esc key turns off free transform editor; quietly ignored if editor was never turned on.
@param {Event} e keydown event
*/
_onKeydown = function (e) {
// only handle cases for polygon editors
if (scope.inst.type !== "polygon") {
return;
}
// T key toggles rotate/scale editor
if (e.keyIdentifier === "U+0054") {
scope.inst.toggleFreeTransform();
}
// escape key turns off rotate/scale editor
if (e.keyCode === 27) {
scope.inst.turnOffFreeTransform();
}
};
scope.inst = new CSSShapesEditor(target, model.value, options);
scope.inst.on("shapechange", function () {
if (scope.callback) {
scope.callback.call(scope.inst, scope.inst.getCSSValue());
}
});
document.addEventListener("keydown", _onKeydown);
};
Provider.prototype.update = function (model) {
if (!model || typeof model !== "object") {
throw new TypeError("Invalid input model. Expected object, got " + model);
}
if (!model.value || typeof model.value !== "string") {
throw new TypeError("Invalid update value. Expected string, got " + model.value);
}
this.inst.update(model.value);
};
Provider.prototype.onValueChange = function (fn) {
if (typeof fn !== "function") {
throw new TypeError("Invalid callback. Expected function, got " + fn);
}
this.callback = fn;
};
Provider.prototype.remove = function () {
this.inst.remove();
this.inst = null;
this.callback = undefined;
document.removeEventListener("keydown", _onKeydown);
};
var properties = ["shape-inside", "-webkit-shape-inside", "shape-outside", "-webkit-shape-outside", "clip-path", "-webkit-clip-path"];
properties.forEach(function (property) {
// expose an editor only when the property is supported by the browser
if (document.body.style[property] !== undefined) {
window._LD_CSS_EDITOR.registerProvider(property, Provider);
} else {
console.warn(property + " is not supported by this browser.\n Perhaps it is not enabled. See html.adobe.com/webplatform/enable");
}
});
}());