| 8 | const Splash = require('./splash') |
| 9 | |
| 10 | function Project () { |
| 11 | this.pages = [] |
| 12 | |
| 13 | this.index = 0 |
| 14 | this.original = '' |
| 15 | |
| 16 | this.start = function () { |
| 17 | // Load previous files |
| 18 | if (localStorage.hasOwnProperty('paths')) { |
| 19 | if (isJSON(localStorage.getItem('paths'))) { |
| 20 | const paths = JSON.parse(localStorage.getItem('paths')) |
| 21 | for (const id in paths) { |
| 22 | left.project.add(paths[id]) |
| 23 | } |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | // Add splash |
| 28 | if (this.pages.length === 0) { |
| 29 | left.project.pages.push(new Splash()) |
| 30 | left.go.to_page(0) |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | this.add = function (path = null) { |
| 35 | console.log(`Adding page(${path})`) |
| 36 | |
| 37 | this.remove_splash() |
| 38 | |
| 39 | let page = new Page() |
| 40 | |
| 41 | if (path) { |
| 42 | if (this.paths().indexOf(path) > -1) { console.warn(`Already open(skipped): ${path}`); return } |
| 43 | page = new Page(this.load(path), path) |
| 44 | } |
| 45 | |
| 46 | this.pages.push(page) |
| 47 | left.go.to_page(this.pages.length - 1) |
| 48 | |
| 49 | localStorage.setItem('paths', JSON.stringify(this.paths())) |
| 50 | } |
| 51 | |
| 52 | this.page = function () { |
| 53 | return this.pages[this.index] |
| 54 | } |
| 55 | |
| 56 | this.update = function () { |
| 57 | if (!this.page()) { console.warn('Missing page'); return } |
| 58 | |
| 59 | this.page().commit(left.textarea_el.value) |
| 60 | } |
| 61 | |
| 62 | this.load = function (path) { |
| 63 | console.log(`Load: ${path}`) |
| 64 | |
| 65 | let data |
| 66 | try { |
| 67 | data = fs.readFileSync(path, 'utf-8') |