diff options
Diffstat (limited to 'packages/gitbook-core/src/models')
-rw-r--r-- | packages/gitbook-core/src/models/Context.js | 58 | ||||
-rw-r--r-- | packages/gitbook-core/src/models/File.js | 54 | ||||
-rw-r--r-- | packages/gitbook-core/src/models/Language.js | 12 | ||||
-rw-r--r-- | packages/gitbook-core/src/models/Languages.js | 40 | ||||
-rw-r--r-- | packages/gitbook-core/src/models/Location.js | 78 | ||||
-rw-r--r-- | packages/gitbook-core/src/models/Page.js | 24 | ||||
-rw-r--r-- | packages/gitbook-core/src/models/Plugin.js | 21 | ||||
-rw-r--r-- | packages/gitbook-core/src/models/Readme.js | 21 | ||||
-rw-r--r-- | packages/gitbook-core/src/models/SummaryArticle.js | 32 | ||||
-rw-r--r-- | packages/gitbook-core/src/models/SummaryPart.js | 17 |
10 files changed, 357 insertions, 0 deletions
diff --git a/packages/gitbook-core/src/models/Context.js b/packages/gitbook-core/src/models/Context.js new file mode 100644 index 0000000..f4b0d4c --- /dev/null +++ b/packages/gitbook-core/src/models/Context.js @@ -0,0 +1,58 @@ +const { Record, List } = require('immutable'); + +const DEFAULTS = { + store: null, + actions: {}, + plugins: List() +}; + +class Context extends Record(DEFAULTS) { + constructor(...args) { + super(...args); + + this.dispatch = this.dispatch.bind(this); + this.getState = this.getState.bind(this); + } + + /** + * Return current state + * @return {Object} + */ + getState() { + const { store } = this; + return store.getState(); + } + + /** + * Dispatch an action + * @param {Action} action + */ + dispatch(action) { + const { store } = this; + return store.dispatch(action); + } + + /** + * Deactivate the context, cleanup resources from plugins. + */ + deactivate() { + const { plugins, actions } = this; + + plugins.forEach(plugin => { + plugin.deactivate(this.dispatch, this.getState, actions); + }); + } + + /** + * Activate the context and the plugins. + */ + activate() { + const { plugins, actions } = this; + + plugins.forEach(plugin => { + plugin.activate(this.dispatch, this.getState, actions); + }); + } +} + +module.exports = Context; diff --git a/packages/gitbook-core/src/models/File.js b/packages/gitbook-core/src/models/File.js new file mode 100644 index 0000000..efc4f11 --- /dev/null +++ b/packages/gitbook-core/src/models/File.js @@ -0,0 +1,54 @@ +const path = require('path'); +const { Record } = require('immutable'); + +const DEFAULTS = { + type: '', + mtime: new Date(), + path: '', + url: '' +}; + +class File extends Record(DEFAULTS) { + constructor(file = {}) { + if (typeof file === 'string') { + file = { path: file, url: file }; + } + + super({ + ...file, + mtime: new Date(file.mtime) + }); + } + + /** + * @param {String} to Absolute path + * @return {String} The same path, but relative to this file + */ + relative(to) { + return path.relative( + path.dirname(this.path), + to + ) || './'; + } + + /** + * Return true if file is an instance of File + * @param {Mixed} file + * @return {Boolean} isFile + */ + static is(file) { + return (file instanceof File); + } + + /** + * Create a file instance + * @param {Mixed|File} file + * @return {File} file + */ + static create(file) { + return File.is(file) ? + file : new File(file); + } +} + +module.exports = File; diff --git a/packages/gitbook-core/src/models/Language.js b/packages/gitbook-core/src/models/Language.js new file mode 100644 index 0000000..20fc237 --- /dev/null +++ b/packages/gitbook-core/src/models/Language.js @@ -0,0 +1,12 @@ +const { Record } = require('immutable'); + +const DEFAULTS = { + id: null, + title: null +}; + +class Language extends Record(DEFAULTS) { + +} + +module.exports = Language; diff --git a/packages/gitbook-core/src/models/Languages.js b/packages/gitbook-core/src/models/Languages.js new file mode 100644 index 0000000..b698d14 --- /dev/null +++ b/packages/gitbook-core/src/models/Languages.js @@ -0,0 +1,40 @@ +const { Record, List } = require('immutable'); +const Language = require('./Language'); +const File = require('./File'); + +const DEFAULTS = { + current: String(), + file: new File(), + list: List() +}; + +class Languages extends Record(DEFAULTS) { + constructor(spec = {}) { + super({ + ...spec, + file: File.create(spec.file), + list: List(spec.list).map(lang => new Language(lang)) + }); + } + + /** + * Return true if file is an instance of Languages + * @param {Mixed} langs + * @return {Boolean} + */ + static is(langs) { + return (langs instanceof Languages); + } + + /** + * Create a Languages instance + * @param {Mixed|Languages} langs + * @return {Languages} + */ + static create(langs) { + return Languages.is(langs) ? + langs : new Languages(langs); + } +} + +module.exports = Languages; diff --git a/packages/gitbook-core/src/models/Location.js b/packages/gitbook-core/src/models/Location.js new file mode 100644 index 0000000..cdfea2d --- /dev/null +++ b/packages/gitbook-core/src/models/Location.js @@ -0,0 +1,78 @@ +const { Record, Map } = require('immutable'); +const querystring = require('querystring'); + +const DEFAULTS = { + pathname: String(''), + // Hash without the # + hash: String(''), + // If query is a non empty map + query: Map() +}; + +class Location extends Record(DEFAULTS) { + + /** + * Return search query as a string + * @return {String} + */ + get search() { + const { query } = this; + return query.size === 0 ? + '' : + '?' + querystring.stringify(query.toJS()); + } + + /** + * Convert this location to a string. + * @return {String} + */ + toString() { + + } + + /** + * Convert this immutable instance to an object + * for "history". + * @return {Object} + */ + toNative() { + return { + pathname: this.pathname, + hash: this.hash ? `#${this.hash}` : '', + search: this.search + }; + } + + /** + * Convert an instance from "history" to Location. + * @param {Object|String} location + * @return {Location} + */ + static fromNative(location) { + if (typeof location === 'string') { + location = { pathname: location }; + } + + const pathname = location.pathname; + let hash = location.hash || ''; + let search = location.search || ''; + let query = location.query; + + hash = hash[0] === '#' ? hash.slice(1) : hash; + search = search[0] === '?' ? search.slice(1) : search; + + if (query) { + query = Map(query); + } else { + query = Map(querystring.parse(search)); + } + + return new Location({ + pathname, + hash, + query + }); + } +} + +module.exports = Location; diff --git a/packages/gitbook-core/src/models/Page.js b/packages/gitbook-core/src/models/Page.js new file mode 100644 index 0000000..e3c4a96 --- /dev/null +++ b/packages/gitbook-core/src/models/Page.js @@ -0,0 +1,24 @@ +const { Record, Map, fromJS } = require('immutable'); + +const DEFAULTS = { + title: '', + content: '', + dir: 'ltr', + depth: 1, + level: '', + previous: null, + next: null, + attributes: Map() +}; + +class Page extends Record(DEFAULTS) { + static create(state) { + return state instanceof Page ? + state : new Page({ + ...state, + attributes: fromJS(state.attributes) + }); + } +} + +module.exports = Page; diff --git a/packages/gitbook-core/src/models/Plugin.js b/packages/gitbook-core/src/models/Plugin.js new file mode 100644 index 0000000..07b1976 --- /dev/null +++ b/packages/gitbook-core/src/models/Plugin.js @@ -0,0 +1,21 @@ +const { Record } = require('immutable'); + +const DEFAULTS = { + activate: ((dispatch, getState) => {}), + deactivate: ((dispatch, getState) => {}), + reduce: ((state, action) => state), + actions: {} +}; + +class Plugin extends Record(DEFAULTS) { + constructor(plugin) { + super({ + activate: plugin.activate || DEFAULTS.activate, + deactivate: plugin.deactivate || DEFAULTS.deactivate, + reduce: plugin.reduce || DEFAULTS.reduce, + actions: plugin.actions || DEFAULTS.actions + }); + } +} + +module.exports = Plugin; diff --git a/packages/gitbook-core/src/models/Readme.js b/packages/gitbook-core/src/models/Readme.js new file mode 100644 index 0000000..f275ca2 --- /dev/null +++ b/packages/gitbook-core/src/models/Readme.js @@ -0,0 +1,21 @@ +const { Record } = require('immutable'); +const File = require('./File'); + +const DEFAULTS = { + file: new File() +}; + +class Readme extends Record(DEFAULTS) { + constructor(state = {}) { + super({ + file: File.create(state.file) + }); + } + + static create(state) { + return state instanceof Readme ? + state : new Readme(state); + } +} + +module.exports = Readme; diff --git a/packages/gitbook-core/src/models/SummaryArticle.js b/packages/gitbook-core/src/models/SummaryArticle.js new file mode 100644 index 0000000..3651c8a --- /dev/null +++ b/packages/gitbook-core/src/models/SummaryArticle.js @@ -0,0 +1,32 @@ +const { Record, List } = require('immutable'); + +const DEFAULTS = { + title: '', + depth: 0, + path: '', + url: '', + ref: '', + level: '', + articles: List() +}; + +class SummaryArticle extends Record(DEFAULTS) { + constructor(article) { + super({ + ...article, + articles: (new List(article.articles)) + .map(art => new SummaryArticle(art)) + }); + } + + /** + * Return true if article is an instance of SummaryArticle + * @param {Mixed} article + * @return {Boolean} + */ + static is(article) { + return (article instanceof SummaryArticle); + } +} + +module.exports = SummaryArticle; diff --git a/packages/gitbook-core/src/models/SummaryPart.js b/packages/gitbook-core/src/models/SummaryPart.js new file mode 100644 index 0000000..89c76d4 --- /dev/null +++ b/packages/gitbook-core/src/models/SummaryPart.js @@ -0,0 +1,17 @@ +const { Record, List } = require('immutable'); +const SummaryArticle = require('./SummaryArticle'); + +class SummaryPart extends Record({ + title: '', + articles: List() +}) { + constructor(state) { + super({ + ...state, + articles: (new List(state.articles)) + .map(article => new SummaryArticle(article)) + }); + } +} + +module.exports = SummaryPart; |