Skip to content
This repository has been archived by the owner on Jun 22, 2022. It is now read-only.

Fetch up to date emojilib via unpkg.com #105

Merged
merged 3 commits into from
Aug 23, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 35 additions & 2 deletions app/search.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
var emojilib = require('emojilib').lib
var emojikeys = require('emojilib').ordered
var emojilib = JSON.parse(localStorage.getItem('emojilib')) || require('emojilib').lib
var emojikeys = JSON.parse(localStorage.getItem('emojikeys')) || require('emojilib').ordered
var clipboard = require('electron').clipboard
var ipc = require('electron').ipcRenderer
var index = buildIndex()
Expand All @@ -14,6 +14,35 @@ var directions = {
40: 'down'
}

function fetchAndUpdateLocalCache () {
if (!navigator.onLine) return
var expireTime = localStorage.getItem('emojilibExpireTime')
if (expireTime && Number(expireTime) > new Date().getTime()) return
var version = '^2.0.0'
var emojilibLib = `https://unpkg.com/emojilib@${version}/emojis.json`
var emojilibOrdered = `https://unpkg.com/emojilib@${version}/ordered.json`

fetch(emojilibLib).then(function (res) { return checkIfNewVersion(res) }).then(function(newData) {
// Fetch only once per day
localStorage.setItem('emojilibExpireTime', new Date().getTime() + 1000 * 60 * 60 * 24)
if (!newData) return
localStorage.setItem('emojilib', JSON.stringify(newData))

fetch(emojilibOrdered).then(function (res) { return res.json() }).then(function(newData) {
localStorage.setItem('emojikeys', JSON.stringify(newData))
location.reload()
})
})

function checkIfNewVersion (res) {
var fetchedVersion = res.url.match(/@([\d.]+)/)[1]
if (fetchedVersion !== localStorage.getItem('emojilibVersion')) {
localStorage.setItem('emojilibVersion', fetchedVersion)
return res.json()
}
}
}

searchInput.dataset.isSearchInput = true
searchInput.focus()
search('')
Expand All @@ -25,6 +54,10 @@ ipc.on('show', function (event, message) {
searchInput.focus()
})

ipc.on('fetch', function (event, message) {
fetchAndUpdateLocalCache()
})

document.addEventListener('mousewheel', function (e) {
if (e.deltaY % 1 !== 0) {
e.preventDefault()
Expand Down
2 changes: 2 additions & 0 deletions app/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ var togglePreferencePanel = function () {
html += '</div>'
})
html += '<label></label><button type="submit">Save</button>'
html += `<code class="version">mojibar@${require('../package.json').version}</code>`
if (localStorage.getItem('emojilibVersion')) html += `<code class="version">emojilib@${localStorage.getItem('emojilibVersion')}</code>`
html += '</form>'
panel.innerHTML += html

Expand Down
6 changes: 6 additions & 0 deletions app/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -133,3 +133,9 @@ button {
background: #efefef;
border-right: 1px solid #cfcfcf;
}

.version {
font-family: menlo, monospace;
margin-left: 15px;
font-size: 10px;
}
4 changes: 4 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,10 @@ mb.on('ready', function ready () {
// Build default menu for text editing and devtools. (gone since electron 0.25.2)
var menu = Menu.buildFromTemplate(template)
Menu.setApplicationMenu(menu)

mb.window.on('hide', function () {
mb.window.webContents.send('fetch')
})
})

// Register a shortcut listener.
Expand Down