Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

updated click on/off and listeners #12

Merged
merged 2 commits into from
Oct 30, 2019
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
2 changes: 1 addition & 1 deletion src/chrome/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"name": "Contentful Sidekick",
"short_name": "Contentful Sidekick",
"description": "Chrome Extension that enables inline editing for websites created in Contentful",
"version": "0.0.7",
"version": "0.0.8",
"permissions": [
"activeTab",
"cookies",
Expand Down
7 changes: 0 additions & 7 deletions src/shared/js/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,6 @@ const toggleActive = () => {
}, (items) => {
const curEnabled = !items.sideKickEnabled;
chrome.storage.sync.set({ sideKickEnabled: curEnabled });

chrome.tabs.query({ active: true, currentWindow: true },
(tabs) => {
const activeTab = tabs[0];
chrome.tabs.sendMessage(activeTab.id,
{ message: curEnabled ? 'enable' : 'disable' });
});
});
};

Expand Down
78 changes: 31 additions & 47 deletions src/shared/js/content.js
Original file line number Diff line number Diff line change
@@ -1,54 +1,41 @@
/* global chrome */
const clickEvent = (e) => {
const CONTENTFUL_CURRENT_SPACE_ID = $('[name="contentful_space"]').attr('content');
const CONTENTFUL_ENVIRONMENT = $('[name="contentful_environment"]').attr('content');
const getContentfulUrl = elementId => `https://app.contentful.com/spaces/${CONTENTFUL_CURRENT_SPACE_ID}/environments/${CONTENTFUL_ENVIRONMENT}/entries/${elementId}`;

const setupDom = () => {
$('body').attr('data-init-csk', true);
if (!$('#csk-overlay').length) {
$('body').append($('<div>').attr('id', 'csk-overlay'));
}
if (!$(e.target).attr('data-csk-entry-id')) return;
window.open(getContentfulUrl($(e.target).attr('data-csk-entry-id')));
return true;
};

const resetDom = () => {
$('body').removeAttr('data-init-csk');
$('#csk-overlay').remove();
$('body').off('click', '[data-csk-entry-id]', clickEvent);
};

const getState = new Promise((resolve, reject) => {
chrome.storage.sync.get({
sideKickEnabled: false,
}, (items) => {
resolve(items.sideKickEnabled);
});
});


const init = () => {
const $els = $('[data-csk-entry-id]');
const CONTENTFUL_CURRENT_SPACE_ID = $('[name="contentful_space"]').attr('content');
const CONTENTFUL_ENVIRONMENT = $('[name="contentful_environment"]').attr('content');
const getContentfulUrl = elementId => `https://app.contentful.com/spaces/${CONTENTFUL_CURRENT_SPACE_ID}/environments/${CONTENTFUL_ENVIRONMENT}/entries/${elementId}`;

const $els = $('[data-csk-entry-id]');
if ($els.length && CONTENTFUL_CURRENT_SPACE_ID && CONTENTFUL_ENVIRONMENT) {
$.each($els, (index, el) => {
const elementId = $(el).data('csk-entry-id');
$('body').on('click', '[data-csk-entry-id]', clickEvent);

$.each($els, (index, el) => {
let inheritedBgColor;
$(el)
.off('click')
.on('click', (e) => {
if ($(e.target).data('csk-entry-id') !== elementId) return;
e.stopPropagation();
window.open(getContentfulUrl(elementId));
})
.filter(() => {
const bgImageUrl = $(el).css('background-image');
const bgColor = $(el).css('background-color');
if (bgImageUrl !== 'none' || bgColor.indexOf('rgba') === -1 || bgColor.replace(/^.*,(.+)\)/, '$1').trim() !== '0') {
$(el).attr('data-csk-init-bg', true);
return false;
}

return true;
})
$(el).filter(() => {
const bgImageUrl = $(el).css('background-image');
const bgColor = $(el).css('background-color');
if (bgImageUrl !== 'none' || bgColor.indexOf('rgba') === -1 || bgColor.replace(/^.*,(.+)\)/, '$1').trim() !== '0') {
$(el).attr('data-csk-init-bg', true);
return false;
}

return true;
})
.parents()
.each((i, v) => {
const bgImageUrl = $(v).css('background-image');
Expand Down Expand Up @@ -85,25 +72,22 @@ const init = () => {
.on('mouseenter', () => { $('#csk-overlay').addClass('show'); })
.on('mouseleave', () => { $('#csk-overlay').removeClass('show'); });

getState.then((active) => {
if (active && $els && $els.length) {
setupDom();
} else {
resetDom();
}
});
$('body').attr('data-init-csk', true);
if (!$('#csk-overlay').length) {
$('body').append($('<div>').attr('id', 'csk-overlay'));
}
};

$(() => {
setTimeout(init, 2000);
});

chrome.runtime.onMessage.addListener(
(request, sender, sendResponse) => {
if (request.message === 'enable') {
chrome.storage.onChanged.addListener((changes) => {
if (changes.sideKickEnabled) {
if (changes.sideKickEnabled.newValue === true) {
init();
} else if (request.message === 'disable') {
} else {
resetDom();
}
},
);
}
});