forked from froala/ember-froala-editor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
221 lines (159 loc) · 6.29 KB
/
index.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
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
'use strict';
// Module requirements
var fs = require('fs');
var path = require('path');
var VersionChecker = require('ember-cli-version-checker');
// Resolve the froala-editor node path once..
var froalaPath = path.dirname(
require.resolve( 'froala-editor/package.json' )
);
module.exports = {
name: require('./package').name,
// Addon build option defaults
defaultOptions: {
languages : false,
plugins : false,
themes : false
},
// https://github.com/ember-cli/ember-cli-version-checker#forember
init() {
this._super.init.apply( this, arguments );
let checker = new VersionChecker( this );
checker.for('ember-cli').assertAbove(
'2.15.0',
'To use ember-froala-editor you must have ember-cli 2.15.0 or later!'
); // https://emberjs.com/blog/2017/09/01/ember-2-15-released.html#toc_app-import-files-within-node_modules
checker.forEmber().assertAbove(
'2.15.0',
'To use ember-froala-editor you must have ember 2.15.0 or later!'
); // https://emberjs.com/blog/2017/09/01/ember-2-15-released.html#toc_public-router-service-phase-1
}, // init()
included( app ) {
// https://ember-cli.com/extending/#addon-entry-point
this._super.included.apply( this, arguments );
// Do not import anything if in "fastboot mode"
if ( typeof FastBoot !== 'undefined' ) {
return;
}
// For nested usage, build the options up through the entire tree,
// with priority going up the tree and the "root" app always overriding
let appOptions = {};
let current = this;
do {
app = current.app || app;
if ( app.options && app.options[ this.name ] ) {
appOptions = Object.assign(appOptions, app.options[ this.name ]);
}
} while (current.parent.parent && (current = current.parent));
// Plugins default has recently changed, warn users
if ( !appOptions.hasOwnProperty('plugins') ) {
this.ui.writeDeprecateLine(
`${this.name}: The default value for the 'plugins' option has change from 'true' to 'false'. ` +
`Please update '${this.name}.plugins' in 'ember-cli-build.js' to indicate which plugin(s) you need; ` +
'string = one plugin name, array = multiple plugin names, true = all plugins, false = no plugins.'
);
}
// Build options by merging default options
// with the apps ember-cli-build.js options
let options = Object.assign(
{},
this.defaultOptions,
appOptions
);
// When importing files, import from node_modules
let nodePath = path.join( 'node_modules', 'froala-editor');
// Import the base Froala Editor files
this.import( path.join( nodePath, 'js', 'froala_editor.min.js' ) );
this.import( path.join( nodePath, 'css', 'froala_editor.css' ) );
this.import( path.join( nodePath, 'css', 'froala_style.css' ) );
// Bucket for import list / details
let additionalAssets = [];
// Import the other Froala Editor files (when requested)
if ( options.plugins && options.plugins !== [] ) {
additionalAssets.push({
label : 'Plugin(s)',
paths : [path.join( 'js', 'plugins' ), path.join( 'js', 'third_party' )],
files : options.plugins,
extension : '.min.js'
});
additionalAssets.push({
label : 'Plugin CSS',
paths : [path.join( 'css', 'plugins' ), path.join( 'css', 'third_party' )],
files : options.plugins,
extension : '.css',
optional : true
});
}
if ( options.languages && options.languages !== [] ) {
additionalAssets.push({
label : 'Language(s)',
paths : [path.join( 'js', 'languages' )],
files : options.languages,
extension : '.js'
});
}
if ( options.themes && options.themes !== [] ) {
additionalAssets.push({
label : 'Themes(s)',
paths : [path.join( 'css', 'themes' )],
files : options.themes,
extension : '.css'
});
}
// Common logic to import plugins / languages / themes
additionalAssets.forEach( asset => {
// List of files for the given path(s)
let pathFiles = {}; // key = filename, value = relative path with filename
// Build complete list of files in all paths
asset.paths.forEach( assetPath => {
fs.readdirSync(
path.join( froalaPath, assetPath )
).forEach( fileName => {
pathFiles[ fileName ] = path.join( assetPath, fileName );
});
});
// Bucket for missing files
let missingFiles = [];
// Convert the option value to an array,
// depending on the option type
if ( typeof asset.files === 'boolean' ) {
// Generate a list of _all_ the available files
asset.files = Object.keys(pathFiles).map(function( file ){
return file.split('.')[0]; // remove extensions
}).reduce(function( files, file ){
if ( !files.includes( file ) ) files.push( file );
return files; // return a unique list
}, []);
} else if ( typeof asset.files === 'string' ) {
asset.files = [ asset.files ];
} else if ( !Array.isArray( asset.files ) ) {
throw new Error(
`${this.name}: ${asset.label} ` +
'option in ember-cli-build.js is an invalid type, ' +
'ensure it is either a boolean (all or none), ' +
'string (just one), or array (specific list)'
);
}
// Loop through each requested file
asset.files.forEach( file => {
// Make sure the requested file exists
if ( !pathFiles.hasOwnProperty( file + asset.extension ) ) {
missingFiles.push( file );
return; // continue;
}
// Import the asset file
this.import(
path.join( nodePath, pathFiles[ file + asset.extension ] )
);
}); // files.forEach()
// Display an error message if any required files are missing
if ( missingFiles.length > 0 && !asset.optional ) {
throw new Error(
`${this.name}: ${asset.label} ` +
'specified in ember-cli-build.js are missing, ' +
`make sure they are spelled correctly (${missingFiles.join(', ')})`
);
}
}); // additionalAssets.forEach()
} // included()
}; // module.exports