|
| 1 | +import md5 from 'md5' |
| 2 | +import guid from './support/guid' |
| 3 | +import torchlight from './torchlight' |
| 4 | + |
| 5 | +export default function Block (opts = {}) { |
| 6 | + opts = { |
| 7 | + id: guid(), |
| 8 | + theme: torchlight.config('theme', 'nord'), |
| 9 | + ...opts |
| 10 | + } |
| 11 | + |
| 12 | + this.id = opts.id |
| 13 | + this.code = opts.code |
| 14 | + this.language = opts.language |
| 15 | + this.theme = opts.theme |
| 16 | + |
| 17 | + this.highlighted = null |
| 18 | + this.classes = null |
| 19 | + this.styles = null |
| 20 | +} |
| 21 | + |
| 22 | +Block.prototype.hash = function () { |
| 23 | + return md5('' + |
| 24 | + this.language + |
| 25 | + this.theme + |
| 26 | + this.code + |
| 27 | + torchlight.config('bust', 0) + |
| 28 | + JSON.stringify(torchlight.config('options')) |
| 29 | + ) |
| 30 | +} |
| 31 | + |
| 32 | +Block.prototype.code = function (code) { |
| 33 | + this.code = code |
| 34 | + |
| 35 | + return this |
| 36 | +} |
| 37 | + |
| 38 | +Block.prototype.language = function (language) { |
| 39 | + this.language = language |
| 40 | + |
| 41 | + return this |
| 42 | +} |
| 43 | + |
| 44 | +Block.prototype.theme = function (theme) { |
| 45 | + this.theme = theme |
| 46 | + |
| 47 | + return this |
| 48 | +} |
| 49 | + |
| 50 | +Block.prototype.placeholder = function (extra = '') { |
| 51 | + if (extra) { |
| 52 | + extra = `-${extra}` |
| 53 | + } |
| 54 | + |
| 55 | + return `__torchlight-block-[${this.id}]${extra}__` |
| 56 | +} |
| 57 | + |
| 58 | +Block.prototype.setResponseData = function (data) { |
| 59 | + if (data) { |
| 60 | + this.highlighted = data.highlighted |
| 61 | + this.classes = data.classes |
| 62 | + this.styles = data.styles |
| 63 | + } |
| 64 | + |
| 65 | + return this |
| 66 | +} |
| 67 | + |
| 68 | +Block.prototype.setResponseDataFromCache = function () { |
| 69 | + const data = this.getResponseDataFromCache() |
| 70 | + |
| 71 | + this.setResponseData(data) |
| 72 | + |
| 73 | + return this |
| 74 | +} |
| 75 | + |
| 76 | +Block.prototype.getResponseDataFromCache = function () { |
| 77 | + return torchlight.cache.get(this.hash(), {}) |
| 78 | +} |
| 79 | + |
| 80 | +Block.prototype.populateCache = function (data) { |
| 81 | + torchlight.cache.set(this.hash(), data) |
| 82 | + |
| 83 | + return this |
| 84 | +} |
| 85 | + |
| 86 | +Block.prototype.toRequestParams = function () { |
| 87 | + return { |
| 88 | + id: this.id, |
| 89 | + hash: this.hash(), |
| 90 | + language: this.language, |
| 91 | + theme: this.theme, |
| 92 | + code: this.code |
| 93 | + } |
| 94 | +} |
0 commit comments