forked from lblod/ember-rdfa-editor-plugin-generator
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
initial blueprint for the editor-plugin generator
- Loading branch information
Showing
11 changed files
with
270 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
package-lock=false | ||
package-lock=false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
blueprints/editor-plugin/files/addon/components/editor-plugins/__name__-card.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { reads } from '@ember/object/computed'; | ||
import Component from '@ember/component'; | ||
import layout from '../../templates/components/editor-plugins/<%= dasherizedModuleName %>-card'; | ||
|
||
/** | ||
* Card displaying a hint of the Date plugin | ||
* | ||
* @module editor-<%= dasherizedModuleName %>-plugin | ||
* @class <%= classifiedModuleName %>Card | ||
* @extends Ember.Component | ||
*/ | ||
export default Component.extend({ | ||
layout, | ||
|
||
/** | ||
* Region on which the card applies | ||
* @property location | ||
* @type [number,number] | ||
* @private | ||
*/ | ||
location: reads('info.location'), | ||
|
||
/** | ||
* Unique identifier of the event in the hints registry | ||
* @property hrId | ||
* @type Object | ||
* @private | ||
*/ | ||
hrId: reads('info.hrId'), | ||
|
||
/** | ||
* The RDFa editor instance | ||
* @property editor | ||
* @type RdfaEditor | ||
* @private | ||
*/ | ||
editor: reads('info.editor'), | ||
|
||
/** | ||
* Hints registry storing the cards | ||
* @property hintsRegistry | ||
* @type HintsRegistry | ||
* @private | ||
*/ | ||
hintsRegistry: reads('info.hintsRegistry'), | ||
|
||
actions: { | ||
insert(){ | ||
let mappedLocation = this.get('hintsRegistry').updateLocationToCurrentIndex(this.get('hrId'), this.get('location')); | ||
this.get('hintsRegistry').removeHintsAtLocation(this.get('location'), this.get('hrId'), 'editor-plugins/date-card'); | ||
this.get('editor').replaceTextWithHTML(...mappedLocation, this.get('info').value); | ||
} | ||
} | ||
}); |
130 changes: 130 additions & 0 deletions
130
blueprints/editor-plugin/files/addon/services/rdfa-editor-__name__-plugin.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
import { getOwner } from '@ember/application'; | ||
import Service from '@ember/service'; | ||
import EmberObject, { computed } from '@ember/object'; | ||
import moment from 'moment'; | ||
import { task } from 'ember-concurrency'; | ||
|
||
/** | ||
* Service responsible for correct annotation of dates | ||
* | ||
* @module editor-<%= dasherizedModuleName %>-plugin | ||
* @class RdfaEditor<%= classifiedModuleName %>Plugin | ||
* @constructor | ||
* @extends EmberService | ||
*/ | ||
const RdfaEditor<%= classifiedModuleName %>Plugin = Service.extend({ | ||
|
||
init(){ | ||
this._super(...arguments); | ||
const config = getOwner(this).resolveRegistration('config:environment'); | ||
}, | ||
|
||
/** | ||
* Restartable task to handle the incoming events from the editor dispatcher | ||
* | ||
* @method execute | ||
* | ||
* @param {string} hrId Unique identifier of the event in the hintsRegistry | ||
* @param {Array} contexts RDFa contexts of the text snippets the event applies on | ||
* @param {Object} hintsRegistry Registry of hints in the editor | ||
* @param {Object} editor The RDFa editor instance | ||
* | ||
* @public | ||
*/ | ||
execute: task(function * (hrId, contexts, hintsRegistry, editor) { | ||
if (contexts.length === 0) return []; | ||
|
||
const hints = contexts.filter((context) => detectRelevantContext).forEach((context) => { | ||
return generateHintsForContext(context); | ||
}); | ||
|
||
|
||
return hints.forEach( (hint) => generateCard(hint)); | ||
}).restartable(), | ||
|
||
/** | ||
* Given context object, tries to detect a context the plugin can work on | ||
* | ||
* @method detectRelevantContext | ||
* | ||
* @param {Object} context Text snippet at a specific location with an RDFa context | ||
* | ||
* @return {String} URI of context if found, else empty string. | ||
* | ||
* @private | ||
*/ | ||
detectRelevantContext(context){ | ||
return true; | ||
}, | ||
|
||
|
||
|
||
/** | ||
* Maps location of substring back within reference location | ||
* | ||
* @method normalizeLocation | ||
* | ||
* @param {[int,int]} [start, end] Location withing string | ||
* @param {[int,int]} [start, end] reference location | ||
* | ||
* @return {[int,int]} [start, end] absolute location | ||
* | ||
* @private | ||
*/ | ||
normalizeLocation(location, reference){ | ||
return [location[0] + reference[0], location[1] + reference[0]]; | ||
}, | ||
|
||
/** | ||
* Generates a card given a hint | ||
* | ||
* @method generateCard | ||
* | ||
* @param {string} hrId Unique identifier of the event in the hintsRegistry | ||
* @param {Object} rdfaAnnotation object | ||
* @param {Object} hintsRegistry Registry of hints in the editor | ||
* @param {Object} editor The RDFa editor instance | ||
* @param {Object} hint containing the hinted string and the location of this string | ||
* | ||
* @return {Object} The card to hint for a given template | ||
* | ||
* @private | ||
*/ | ||
generateCard(hrId, rdfaAnnotation, hintsRegistry, editor, hint){ | ||
return EmberObject.create({ | ||
info: { | ||
label: this.get('who'), | ||
plainValue: hint.text, | ||
location: hint.location, | ||
hrId, hintsRegistry, editor | ||
}, | ||
location: hint.location, | ||
card: this.get('who') | ||
}); | ||
}, | ||
|
||
/** | ||
* Generates a hint, given a context | ||
* | ||
* @method generateHintsForContext | ||
* | ||
* @param {Object} context Text snippet at a specific location with an RDFa context | ||
* | ||
* @return {Object} [{dateString, location}] | ||
* | ||
* @private | ||
*/ | ||
generateHintsForContext(context){ | ||
const hints = []; | ||
const text = context.text; | ||
const location = this.normalizeLocation([0, stringToScan.length], context.region); | ||
hints.push({text, location}) | ||
return hints; | ||
} | ||
}); | ||
|
||
RdfaEditor<%= classifiedModuleName %>Plugin.reopen({ | ||
who: 'editor-plugins/<%= dasherizedModuleName %>-card', | ||
|
||
}); | ||
export default RdfaEditor<%= classifiedModuleName %>Plugin; |
10 changes: 10 additions & 0 deletions
10
blueprints/editor-plugin/files/addon/templates/components/editor-plugins/__name__-card.hbs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<p class="u-spacer--tiny">{{info.label}} <br> | ||
<strong>{{info.plainValue}}</strong> | ||
</p> | ||
|
||
{{#wu-button-group}} | ||
{{wu-button label="Juist" size="small" isNarrow=true command="ctrl + j" commandLocation="below" onClick=(action "insert")}} | ||
{{wu-button label="Negeer" size="tiny" isLink=true isDark=true isNarrow=true command="esc" commandLocation="below" onClick=null}} | ||
{{/wu-button-group}} | ||
|
||
{{yield}} |
1 change: 1 addition & 0 deletions
1
blueprints/editor-plugin/files/app/components/editor-plugins/__name__-card.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default } from '@lblod/ember-rdfa-editor-<%= dasherizedModuleName %>-plugin/components/editor-plugins/<%= dasherizedModuleName %>-card'; |
1 change: 1 addition & 0 deletions
1
blueprints/editor-plugin/files/app/services/rdfa-editor-__name__-plugin.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default } from '@lblod/ember-rdfa-editor<%= dasherizedModuleName %>-plugin/services/rdfa-editor-<%= dasherizedModuleName %>-plugin'; |
42 changes: 42 additions & 0 deletions
42
blueprints/editor-plugin/files/blueprints/@lblod/ember-rdfa-editor-__name__-plugin/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* eslint-env node */ | ||
const existsSync = require('exists-sync'); | ||
|
||
const profilesFile = 'app/config/editor-profiles.js'; | ||
|
||
module.exports = { | ||
description: 'Adds the plugin to the default and all editor-profiles', | ||
|
||
normalizeEntityName() { }, | ||
|
||
insertPluginNameAtKey( key, pluginName, afterContents="" ){ | ||
return this.insertIntoFile( | ||
profilesFile, | ||
` "${pluginName}",${afterContents}`, | ||
{ after: ` ${key}: \\[\n` }); | ||
}, | ||
|
||
async afterInstall(options) { | ||
const pluginName = options.originBlueprintName.substr('ember-'.length); | ||
|
||
if( existsSync(profilesFile) ){ | ||
try { | ||
await this.insertPluginNameAtKey("all", pluginName); | ||
await this.insertPluginNameAtKey("default", pluginName, " "); /* the extra space here, | ||
makes the line different | ||
from the inserted line | ||
above. This is makes | ||
insertIntoFile consider | ||
the lines to be different, | ||
and hence insert the | ||
contents. Sorry for the | ||
somewhat uglier generated | ||
files. */ | ||
} catch (err) { | ||
throw 'Failed to insert all contents ' + err; | ||
} | ||
} else { | ||
throw 'Could not insert into "all" profile'; | ||
} | ||
return; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
/* eslint-env node */ | ||
module.exports = { | ||
description: 'Generates the scaffold for a ember-rdfa-editor plugin', | ||
|
||
afterInstall(options) { | ||
// Perform extra work here. | ||
this.addAddonToProject('ember-cli-release'); | ||
return this.addAddonToProject('ember-concurrency'); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
'use strict'; | ||
|
||
module.exports = { | ||
name: 'ember-rdfa-editor-plugin-generator' | ||
name: '@lblod/ember-rdfa-editor-plugin-generator' | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters