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 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 | 401× 364× 1× 1× 364× 364× 6931× 6931× 6931× 6931× 7677× 7677× 753× 753× 7677× 7677× 726× 726× 5154× 5154× 5154× 5154× 2939× 2939× 2939× 2939× 1472× 1472× 1472× 2935× 2935× 2935× 2935× 1835× 2935× 2935× 1094× 2935× 2935× 2935× 2935× 2939× 2939× 13× 13× 2926× 2938× 2938× 2938× 2938× 2938× 6589× 6589× 13178× 758× 13178× 1827× 364× 364× 1103× 1103× 364× 206× 3712× 3712× 363× 363× 363× 363× 22× 22× 22× 30× 30× 30× 205× 205× 205× 2870× 205× 206× 1640× 263× 263× 263× 524× 263× 263× 206× 205× 205× 206× 206× 206× 206× 206× 206× 206× 206× | import { forEach, map, isString, Registry } from '../util' import { DocumentSchema, EditingBehavior } from '../model' import ComponentRegistry from './ComponentRegistry' import FontAwesomeIconProvider from './FontAwesomeIconProvider' import LabelProvider from './DefaultLabelProvider' import ToolGroup from '../packages/toolbar/ToolGroup' import SaveHandlerStub from '../packages/persistence/SaveHandlerStub' /** Default Configurator for Substance editors. It provides an API for adding nodes to the schema, components, commands and tools etc. @class @example ```js let configurator = new Configurator() configurator.addNode(Heading) configurator.addComponent('heading', HeadingComponent) ``` To modularize configuration, package definitions can be imported. ```js configurator.import(ParagraphPackage) ``` You can create your own extensions that way. ```js const AlienPackage = { name: 'alien' configure: function(config) { config.addNode(AlienNode) config.addComponent('alien', AlienComponent) config.addCommand('add-alien', AddAlienCommand) config.addTool('add-alien', AddAlienTool) } } ``` From within a package, another package can be imported. This provides a simple mechanism to model dependencies between packages. Just make sure you don't run into cyclic dependencies as there is no checking for that at the moment. */ class Configurator { constructor() { this.config = { schema: {}, nodes: {}, components: {}, converters: {}, importers: {}, exporters: {}, fileProxies: [], commands: {}, tools: new Map(), toolGroups: new Map(), textTypes: [], editingBehaviors: [], macros: [], dropHandlers: [], keyboardShortcuts: [], icons: {}, labels: {}, lang: 'en_US', SaveHandlerClass: null } } // Record phase API // ------------------------ /** Defines the document schema for this configuration. @param {DocumentSchema} schema A schema to be used for articles created from this configuration. */ defineSchema(schema) { if (schema.ArticleClass) { console.warn('DEPRECATED: schema.ArticleClass is now called schema.DocumentClass') schema.DocumentClass = schema.ArticleClass } Iif (!schema.DocumentClass) { throw new Error('schema.DocumentClass is mandatory') } this.config.schema = schema } /** Adds a node to this configuration. Later, when you use {@link Configurator#getSchema()}, this node will be added to that schema. Usually, used within a package to add its own nodes to the schema. @param {Node} NodeClass */ addNode(NodeClass) { var type = NodeClass.type Iif (!type) { throw new Error('A NodeClass must have a type.') } Iif (this.config.nodes[type]) { throw new Error('NodeClass with this type name is already registered: ' + type) } this.config.nodes[type] = NodeClass } /** Adds a converter for a conversion format. @param {string} type a conversion format type, eg. 'html', 'xml', 'json' @param {Object} converter a converter for that format. */ addConverter(type, converter) { var converters = this.config.converters[type] if (!converters) { converters = {} this.config.converters[type] = converters } Iif (!converter.type) { throw new Error('A converter needs an associated type.') } converters[converter.type] = converter } /** Add importer for a conversion format. @param {string} type a conversion format type. eg. 'html', 'xml' @param {Object} ImporterClass an importer for the conversion format. */ addImporter(type, ImporterClass) { this.config.importers[type] = ImporterClass } /** Add exporter for a conversion format. @param {string} type a conversion format type. eg. 'html', 'xml' @param {Object} ExporterClass an exporter for the conversion format. */ addExporter(type, ExporterClass) { this.config.exporters[type] = ExporterClass } /** Add a component for a node type. Components ({@link Component}) are the ui representation of a node for rendering and manipulation. This is usually used within a package to add representations for nodes added by that package. A component can be added once per nodeType. If you provide two components for the same node type, Substance can't figure out which one to use. @param {String} nodeType the type attribute of the node for which this component is to be used. @param {Class} ComponentClass A subclass of {@link Component} for nodes of nodeType. */ addComponent(nodeType, ComponentClass, force) { Iif (!force && this.config.components[nodeType]) { throw new Error(nodeType+' already registered') } Iif (!ComponentClass) { throw new Error('Provided nil for component '+nodeType) } Iif (!ComponentClass.prototype._isComponent) { throw new Error('ComponentClass must be a subclass of ui/Component.') } this.config.components[nodeType] = ComponentClass } addCommand(name, CommandClass, options) { Iif (!isString(name)) { throw new Error("Expecting 'name' to be a String") } Iif (!CommandClass) { throw new Error('Provided nil for command '+name) } Iif (!CommandClass.prototype._isCommand) { throw new Error("Expecting 'CommandClass' to be of type ui/Command.") } this.config.commands[name] = { name: name, CommandClass: CommandClass, options: options || {} } } addToolGroup(name, ToolGroupClass, options) { options = options || {} ToolGroupClass = ToolGroupClass || ToolGroup this.config.toolGroups.set(name, { name: name, tools: new Map(), Class: ToolGroupClass, options: options }) } addTool(name, ToolClass, options) { options = options || {} Iif (options.target) { console.warn('DEPRECATED: please use `toolGroup` instead of `target`', name) } let toolGroupNames = options.toolGroup || options.target if (isString(toolGroupNames)) { toolGroupNames = [ toolGroupNames ] } Iif (!toolGroupNames && options.overlay) { toolGroupNames = [ 'overlay' ] } else if (!toolGroupNames) { toolGroupNames = [ 'default' ] } Iif (!isString(name)) { throw new Error("Expecting 'name' to be a String") } Iif (!ToolClass) { throw new Error('Provided nil for tool '+name) } Iif (!ToolClass || !ToolClass.prototype._isTool) { throw new Error("Expecting 'ToolClass' to be of type ui/Tool. name:") } toolGroupNames.forEach((toolGroupName) => { let toolGroup = this.config.toolGroups.get(toolGroupName) if (!toolGroup) { console.error(`No toolGroup registered with name: ${toolGroupName}`) return } toolGroup.tools.set(name, { name: name, Class: ToolClass, options: options || {} }) }) } /** Adds an icon to the configuration which can be later retrieved via the iconProvider. @param {string} iconName name or key for retrieving the icon @param {Object} options your custom method of representing the icon as a JSON object. Enables plugging in your own IconProvider. */ addIcon(iconName, options) { var iconConfig = this.config.icons[iconName] Eif (!iconConfig) { iconConfig = {} this.config.icons[iconName] = iconConfig } Object.assign(iconConfig, options) } /** Define a new label Label is either a string or a hash with translations. If string is provided 'en' is used as the language. @param {String} labelName name of label. @param {String} label label. @example ``` // Using english only. config.addLabel('paragraph.content', 'Paragraph') // Using multiple languages config.addLabel('superscript', { en: 'Superscript', de: 'Hochgestellt' }) . . // Usage within other code let labels = this.context.labelProvider $$('span').append(labels.getLabel('superscript')) ``` */ addLabel(labelName, label) { Iif (isString(label)) { if(!this.config.labels['en']) { this.config.labels['en'] = {} } this.config.labels['en'][labelName] = label } else { forEach(label, function(label, lang) { if (!this.config.labels[lang]) { this.config.labels[lang] = {} } this.config.labels[lang][labelName] = label }.bind(this)) } } /** Replaces the seed function for this configuration. Use a seed function to create the empty state for your document. This should be used only once per configuration. You shouldn't call this within package config methods. You can use {@link Configurator#getSeed} method to get this seed and apply it on your document {@link Document} class. @param {function} seed A transaction function that creates the seed document from an empty document. @example ```js var seedFn = function(tx) { var body = tx.get('body') tx.create({ id: 'p1', type: 'paragraph', content: 'This is your new paragraph!' }) body.show('p1') } config.addSeed(seedFn) ``` */ addSeed(seed) { this.config.seed = seed } addTextType(textType, options) { this.config.textTypes.push({ spec: textType, options: options || {} }) } /** Adds an editing behavior to this configuration. {@link EditingBehavior} for more. @param {EditingBehavior} editingBehavior. */ addEditingBehavior(editingBehavior) { this.config.editingBehaviors.push(editingBehavior) } addMacro(macro) { this.config.macros.push(macro) } addDragAndDrop(DragAndDropHandlerClass) { // we deprecated this after it became more clear what // we actually needed to solve console.warn('DEPRECATED: Use addDropHandler() instead') if (!DragAndDropHandlerClass.prototype._isDragAndDropHandler) { throw new Error('Only instances of DragAndDropHandler are allowed.') } this.addDropHandler(new DragAndDropHandlerClass()) } addDropHandler(dropHandler) { // legacy Iif (dropHandler._isDragAndDropHandler) { dropHandler.type = dropHandler.type || 'drop-asset' } this.config.dropHandlers.push(dropHandler) } addKeyboardShortcut(combo, spec) { let entry = { key: combo, spec: spec } this.config.keyboardShortcuts.push(entry) } addFileProxy(FileProxyClass) { this.config.fileProxies.push(FileProxyClass) } getFileAdapters() { return this.config.fileProxies.slice(0) } /** Configure this instance of configuration for provided package. @param {Object} pkg Object should contain a `configure` method that takes a Configurator instance as the first method. @param {Object} options Additional options to pass to the package.`configure` method @return {configurator} returns the configurator instance to make it easy to chain calls to import. */ import(pkg, options) { pkg.configure(this, options || {}) return this } // Config Interpreter APIs // ------------------------ getConfig() { return this.config } getStyles() { return this.config.styles } getSchema() { Eif (!this.schema) { this.schema = new DocumentSchema(this.config.schema) this.schema.addNodes(this.config.nodes) } return this.schema } getDocumentClass() { return this.config.schema.DocumentClass } createArticle(seed) { const schema = this.getSchema() const DocumentClass = schema.getDocumentClass() let doc = new DocumentClass(schema) if (seed) { seed(doc) } return doc } createImporter(type, context, options = {}) { var ImporterClass = this.config.importers[type] var config = Object.assign({ schema: this.getSchema(), converters: this.getConverterRegistry().get(type).values(), }, options) return new ImporterClass(config, context) } createExporter(type, context, options = {}) { var ExporterClass = this.config.exporters[type] var config = Object.assign({ schema: this.getSchema(), converters: this.getConverterRegistry().get(type).values() }, options) return new ExporterClass(config, context) } getToolGroups() { return this.config.toolGroups } getTools(toolGroupName) { return this.config.toolGroups.get(toolGroupName).tools } getComponentRegistry() { var componentRegistry = new ComponentRegistry() forEach(this.config.components, function(ComponentClass, name) { componentRegistry.add(name, ComponentClass) }) return componentRegistry } getCommands() { return map(this.config.commands, function(item, name) { return new item.CommandClass(Object.assign({name: name}, item.options)) }) } getSurfaceCommandNames() { var commands = this.getCommands() var commandNames = commands.map(function(C) { return C.type }) return commandNames } /* A converter registry is a registry by file type and then by node type `configurator.getConverterRegistry().get('html').get('paragraph')` provides a HTML converter for Paragraphs. */ getConverterRegistry() { Eif (!this.converterRegistry) { var converterRegistry = new Registry() forEach(this.config.converters, function(converters, name) { converterRegistry.add(name, new Registry(converters)) }) this.converterRegistry = converterRegistry } return this.converterRegistry } getDropHandlers() { return this.config.dropHandlers.slice(0) } getSeed() { return this.config.seed } getTextTypes() { return this.config.textTypes.map(function(t) { return t.spec }) } getIconProvider() { return new FontAwesomeIconProvider(this.config.icons) } getLabelProvider() { return new LabelProvider(this.config.labels) } getEditingBehavior() { var editingBehavior = new EditingBehavior() this.config.editingBehaviors.forEach(function(behavior) { behavior.register(editingBehavior) }) return editingBehavior } getMacros() { return this.config.macros } getKeyboardShortcuts() { return this.config.keyboardShortcuts } setDefaultLanguage(lang) { this.config.lang = lang } getDefaultLanguage() { return this.config.lang || 'en_US' } setSaveHandlerClass(SaveHandlerClass) { this.config.SaveHandlerClass = SaveHandlerClass } getSaveHandler() { let SaveHandler = this.config.SaveHandlerClass || SaveHandlerStub return new SaveHandler() } } export default Configurator |