Skip to content

Commit 7c17a40

Browse files
committed
Adds modern js pipeline with latest editor
feat(js)
1 parent f4eeed8 commit 7c17a40

17 files changed

+18093
-9677
lines changed

.babelrc

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"presets": [
3+
[
4+
"@babel/preset-env",
5+
{
6+
"targets": {
7+
"browsers": ["last 2 versions", "ie >= 11"]
8+
},
9+
"forceAllTransforms": true
10+
}
11+
]
12+
]
13+
}

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.cache
2+
js/globals**.js**
3+
node_modules

.nvmrc

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
lts/erbium

gulpfile.js

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
const gulp = require('gulp');
2+
const babelify = require('babelify');
3+
const browserify = require("browserify");
4+
const buffer = require('vinyl-buffer');
5+
const source = require('vinyl-source-stream');
6+
7+
const gulpConfig = {
8+
src: './js/**es6.js',
9+
dist: './js',
10+
main: 'patternkit.jsoneditor.es6.js',
11+
lib: [
12+
'./node_modules/@json-editor/json-editor/dist/jsoneditor.js',
13+
'./node_modules/@json-editor/json-editor/dist/nonmin/**',
14+
'./node_modules/ajv/dist/ajv.min.js',
15+
'./node_modules/ajv/dist/ajv.min.js.map'
16+
]
17+
};
18+
19+
gulp.task('compile:es6', function () {
20+
return browserify(gulpConfig.dist + '/' + gulpConfig.main)
21+
.transform(babelify)
22+
.bundle()
23+
.pipe(source(gulpConfig.main.replace('.es6', '')))
24+
.pipe(buffer())
25+
.pipe(gulp.dest(gulpConfig.dist));
26+
});
27+
28+
gulp.task('copy:lib', function() {
29+
return gulp.src(gulpConfig.lib).pipe(gulp.dest(gulpConfig.dist));
30+
});
31+
32+
gulp.task('watch', function() {
33+
return gulp.watch(gulpConfig.src, 'compile:es6');
34+
});
35+
36+
gulp.task('default', gulp.parallel(['compile:es6', 'copy:lib']));

js/DrupalImageEditor.es6.js

+179
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
/*globals JSONEditor:false */
2+
/**
3+
* @file DrupalImageEditor class.
4+
*
5+
* @external Drupal
6+
*
7+
* @external JSONEditor
8+
*/
9+
10+
export var DrupalImageEditor = JSONEditor.AbstractEditor.extend({
11+
getNumColumns: function () {
12+
return 4;
13+
},
14+
15+
build: function () {
16+
this.title = this.header = this.label = this.theme.getFormInputLabel(this.getTitle(), this.isRequired());
17+
// Don't show uploader if this is readonly
18+
if (!this.schema.readOnly && !this.schema.readonly) {
19+
this.urlfield = this.theme.getFormInputField('text');
20+
let media_library_opener_parameters = {
21+
field_widget_id: this.urlfield.id
22+
};
23+
let opener_encoded = encodeURIComponent(JSON.stringify(media_library_opener_parameters));
24+
this.urlfield.addEventListener('change', function (e) {
25+
e.preventDefault();
26+
e.stopPropagation();
27+
Drupal.dialog(jQuery('<div>', {id: 'patternkit_jsonlibrary_image_dialog'})
28+
.append(jQuery('<span>', {id: 'patternkit_image_dialog_loading'})), {
29+
title: Drupal.t('Choose Image'),
30+
width: 900,
31+
height: 900
32+
}).showModal();
33+
Drupal.ajax({
34+
url: settings.imageUrl + "&media_library_opener_parameters=" + opener_encoded,
35+
base: 'patternkit_jsonlibrary_image_dialog',
36+
wrapper: 'patternkit_image_dialog_loading'
37+
}).execute({});
38+
});
39+
}
40+
let description = this.schema.description || '';
41+
this.preview = this.theme.getFormInputDescription(description);
42+
this.container.appendChild(this.preview);
43+
this.control = this.theme.getFormControl(this.label, this.urlfield || this.input, this.preview);
44+
this.container.appendChild(this.control);
45+
window.requestAnimationFrame(() => {
46+
if (this.value) {
47+
let img = document.createElement('img');
48+
img.style.maxWidth = '100%';
49+
img.style.maxHeight = '100px';
50+
img.onload = (event) => {
51+
this.preview.appendChild(img);
52+
};
53+
img.onerror = (error) => {
54+
console.error('upload error', error);
55+
};
56+
img.src = this.container.querySelector('a').href;
57+
}
58+
});
59+
},
60+
61+
refreshPreview: function() {
62+
if (this.last_preview === this.preview_value) {
63+
return;
64+
}
65+
this.last_preview = this.preview_value;
66+
this.preview.innerHTML = '';
67+
if (!this.preview_value) {
68+
return;
69+
}
70+
let mime = this.preview_value.match(/^data:([^;,]+)[;,]/);
71+
if (mime) {
72+
mime = mime[1];
73+
}
74+
else {
75+
mime = 'unknown';
76+
}
77+
let file = this.urlfield.files[0];
78+
this.preview.innerHTML = '<strong>Type:</strong> ' + mime + ', <strong>Size:</strong> ' + file.size + ' bytes';
79+
if (mime.substr(0, 5) === "image") {
80+
this.preview.innerHTML += '<br>';
81+
let img = document.createElement('img');
82+
img.style.maxWidth = '100%';
83+
img.style.maxHeight = '100px';
84+
img.src = this.preview_value;
85+
this.preview.appendChild(img);
86+
}
87+
this.preview.innerHTML += '<br>';
88+
let uploadButton = this.getButton('Upload', 'upload', 'Upload');
89+
this.preview.appendChild(uploadButton);
90+
uploadButton.addEventListener('click', (event) => {
91+
event.preventDefault();
92+
uploadButton.setAttribute("disabled", "disabled");
93+
this.theme.removeInputError(this.uploader);
94+
if (this.theme.getProgressBar) {
95+
this.progressBar = this.theme.getProgressBar();
96+
this.preview.appendChild(this.progressBar);
97+
}
98+
this.jsoneditor.options.upload(this.path, file, {
99+
success: (url) => {
100+
this.setValue(url);
101+
if (this.parent) {
102+
this.parent.onChildEditorChange(this);
103+
}
104+
else {
105+
this.jsoneditor.onChange();
106+
}
107+
if (this.progressBar) {
108+
this.preview.removeChild(this.progressBar);
109+
}
110+
uploadButton.removeAttribute("disabled");
111+
},
112+
failure: (error) => {
113+
this.theme.addInputError(this.uploader, error);
114+
if (this.progressBar) {
115+
this.preview.removeChild(this.progressBar);
116+
}
117+
uploadButton.removeAttribute("disabled");
118+
},
119+
updateProgress: (progress) => {
120+
if (this.progressBar) {
121+
if (progress) {
122+
this.theme.updateProgressBar(this.progressBar, progress);
123+
}
124+
else {
125+
this.theme.updateProgressBarUnknown(this.progressBar);
126+
}
127+
}
128+
}
129+
});
130+
});
131+
if (this.jsoneditor.options.auto_upload || this.schema.options.auto_upload) {
132+
uploadButton.dispatchEvent(new MouseEvent('click'));
133+
this.preview.removeChild(uploadButton);
134+
}
135+
},
136+
137+
enable: function () {
138+
if (!this.always_disabled) {
139+
if (this.urlfield) {
140+
this.urlfield.disabled = false;
141+
}
142+
this._super();
143+
}
144+
},
145+
146+
disable: function (always_disabled) {
147+
if (always_disabled) {
148+
this.always_disabled = true;
149+
}
150+
if (this.urlfield) {
151+
this.urlfield.disabled = true;
152+
}
153+
this._super();
154+
},
155+
156+
setValue: function (val) {
157+
if (this.value !== val) {
158+
this.value = val;
159+
this.urlfield.value = this.value;
160+
this.onChange();
161+
}
162+
},
163+
164+
destroy: function () {
165+
if (this.preview && this.preview.parentNode) {
166+
this.preview.parentNode.removeChild(this.preview);
167+
}
168+
if (this.title && this.title.parentNode) {
169+
this.title.parentNode.removeChild(this.title);
170+
}
171+
if (this.input && this.input.parentNode) {
172+
this.input.parentNode.removeChild(this.input);
173+
}
174+
if (this.urlfield && this.urlfield.parentNode) {
175+
this.urlfield.parentNode.removeChild(this.urlfield);
176+
}
177+
this._super();
178+
}
179+
});

js/DrupalImageEditor.es6.js.map

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

js/ajv.min.js

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

js/ajv.min.js.map

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)