From 7de83c6d5bb6fd470dc5f3fd6ce6d6cd52f2f9bc Mon Sep 17 00:00:00 2001 From: RostiMelk Date: Mon, 30 Mar 2020 16:37:02 +0200 Subject: [PATCH 1/9] Added the ability to copy children together with group and repeater fields. As of now you can only copy on level of child items and indentation is a bit broken --- manifest.json | 2 +- scripts/fields.js | 37 ++++---- scripts/scripts.js | 216 ++++++++++++++++++++++++++++++++------------- 3 files changed, 173 insertions(+), 82 deletions(-) diff --git a/manifest.json b/manifest.json index a2a4801..64125a5 100755 --- a/manifest.json +++ b/manifest.json @@ -1,7 +1,7 @@ { "manifest_version": 2, "name": "ACF Tools", - "version": "2.2.3", + "version": "2.3.0", "description": "This is a Chrome extension for Advanced Custom Fields to help with developing sites faster.", "icons": { "16": "icons/icon16.png", diff --git a/scripts/fields.js b/scripts/fields.js index 3a8d4c4..51ebec5 100644 --- a/scripts/fields.js +++ b/scripts/fields.js @@ -1,4 +1,4 @@ -function acf_field(fieldName, typeOfField, returnType, seniority, place) { +function acf_field(fieldName, typeOfField, returnType, seniority, place, subFields) { switch (typeOfField) { // Basic case "text": @@ -188,11 +188,11 @@ function acf_field(fieldName, typeOfField, returnType, seniority, place) { // Layout case "group": - var fieldCode = "\n" + "\t\n" + "\t\t\n" + "\t\n" + ""; + var fieldCode = "\n" + "\t\n" + "\t\t" + subFields + "\n" + "\t\n" + ""; break; case "repeater": - var fieldCode = "\n" + "\t\n" + "\t\t\n" + "\t\n" + ""; + var fieldCode = "\n" + "\t\n" + "\t\t" + subFields + "\n" + "\t\n" + ""; break; case "flexible_content": @@ -204,23 +204,22 @@ function acf_field(fieldName, typeOfField, returnType, seniority, place) { break; default: - fieldError() - + fieldError(); } - if (typeof typeOfField != "undefined") { - // Change to sub field if sub field - if (seniority == 'sub') { - fieldCode = fieldCode.replace('get_field', 'get_sub_field'); - fieldCode = fieldCode.replace('the_field', 'the_sub_field'); - } - // Add options if options page - if (place == 'options_page') { - var fieldNameRe = new RegExp("'" + fieldName + "'", 'g'); - fieldCode = fieldCode.replace(fieldNameRe, "'" + fieldName + "', 'options'"); - } - - // Copy to clipboard - copyCodeToClipboard(fieldCode); + // Change to sub field if sub field + if (seniority == 'sub') { + fieldCode = fieldCode.replace('get_field', 'get_sub_field'); + fieldCode = fieldCode.replace('the_field', 'the_sub_field'); + } + // Add options if options page + if (place == 'options_page') { + var fieldNameRe = new RegExp("'" + fieldName + "'", 'g'); + fieldCode = fieldCode.replace(fieldNameRe, "'" + fieldName + "', 'options'"); } + + // Copy to clipboard + sessionStorage.removeItem('fieldcode'); + sessionStorage.setItem('fieldcode', '\n' + fieldCode + '\n'); + copyCodeToClipboard(fieldCode); } diff --git a/scripts/scripts.js b/scripts/scripts.js index 6e0d2e6..0957567 100755 --- a/scripts/scripts.js +++ b/scripts/scripts.js @@ -1,14 +1,15 @@ -$(document).ready(function () { - var debug = false; - +$(document).ready(function() { copyFieldName(); openDocs(); - copyFieldCode(debug); + copyFieldCode(); + appendCopyCodeBtns(); }); function copyMessage() { - $("body").append('
Copied to clipboard!
'); - setTimeout(function () { + $("body").append( + '
Copied to clipboard!
' + ); + setTimeout(function() { $(".acftools-message").remove(); }, 3000); } @@ -16,98 +17,189 @@ function copyMessage() { function copyToClipboard(element) { var temp = $(""); $("body").append(temp); - temp.val($(element).text().trim()).select(); + temp + .val( + $(element) + .text() + .trim() + ) + .select(); document.execCommand("copy"); temp.remove(); copyMessage(); } function copyCodeToClipboard(element) { - var temp = $('').val(element).appendTo('body').select() - document.execCommand('copy') + var temp = $("") + .val(element) + .appendTo("body") + .select(); + document.execCommand("copy"); copyMessage(); } function copyFieldName() { - $("body").on("click", ".li-field-name", function () { + $("body").on("click", ".li-field-name", function() { copyToClipboard(this); }); } function openDocs() { - $("body").on("click", ".li-field-type", function () { - var type = $(this).closest(".acf-field-object").attr("data-type"); + $("body").on("click", ".li-field-type", function() { + var type = $(this) + .closest(".acf-field-object") + .attr("data-type"); var url = type.replace("_", "-"); - - window.open("https://www.advancedcustomfields.com/resources/" + url, "_blank"); + window.open( + "https://www.advancedcustomfields.com/resources/" + url, + "_blank" + ); }); } function fieldError() { - alert("Unsupported field, submit an issue on: \nhttps://github.com/RostiMelk/ACF-Tools"); + alert( + "Unsupported field, submit an issue on: \nhttps://github.com/RostiMelk/ACF-Tools" + ); + throw new Error( + "Unsupported field, submit an issue on: \nhttps://github.com/RostiMelk/ACF-Tools" + ); } -function copyFieldCode(debug) { +function appendCopyCodeBtns() { // Append copy field code button function appendCopyCodeBtn() { - $('.acf-field-object:not([data-type="accordion"], [data-type="message"], [data-type="tab"]) .row-options').append( - 'Copy code' + $( + '.acf-field-object:not([data-type="accordion"], [data-type="message"], [data-type="tab"]) .row-options' + ).append( + 'Copy code' ); } appendCopyCodeBtn(); // Append buttons to new fields - $('body').on('click', '.add-field', function () { - $('.copy-field-code').remove(); - setTimeout(function () { + $("body").on("click", ".add-field", function() { + $(".copy-field-code").remove(); + setTimeout(function() { appendCopyCodeBtn(); }, 10); - }) + }); + // Append copy all fields code button + function appendCopyAllCodeBtn() { + $( + '.acf-field-object[data-type="repeater"], .acf-field-object[data-type="group"]' + ) + .children(".handle") + .find(".row-options") + .append( + 'Copy with sub fields' + ); + } + appendCopyAllCodeBtn(); + // Append copy all fields code button new fields + $("body").on("click", ".add-field", function() { + $(".copy-field-code").remove(); + setTimeout(function() { + appendCopyAllCodeBtn(); + }, 10); + }); + $("body").on("change", ".acf-field-setting-type .field-type", function() { + $(".copy-field-code.include-sub-fields").remove(); + setTimeout(function() { + appendCopyAllCodeBtn(); + }, 1000); + }); +} - // Copy field code - $("body").on("click", ".copy-field-code", function () { - // Get type of field - var typeOfField = $(this) +function getTypeOfField(thisField) { + // Get type of field + return thisField.closest(".acf-field-object").attr("data-type"); +} + +function getFieldName(thisField) { + // Get field name + return thisField + .closest(".handle") + .find(".li-field-name") + .text() + .trim(); +} + +function getReturnType(thisField, typeOfField) { + // Output type for supported fields (This is used if a field has multiple types of output methods, such as Array, ID or URL) + if ( + typeOfField == "image" || + typeOfField == "gallery" || + typeOfField == "link" || + typeOfField == "taxonomy" || + typeOfField == "user" || + typeOfField == "file" + ) { + return thisField .closest(".acf-field-object") - .attr("data-type"); - debug ? console.log('typeOfField: ' + typeOfField) : null; + .find(".acf-field-setting-return_format li label.selected input") + .val(); + } else { + return null; + } +} - // Get field name - var fieldName = $(this) - .closest(".handle") - .find(".li-field-name") - .text() - .trim(); - debug ? console.log('fieldName: ' + fieldName) : null; +function getSeniority(thisField) { + // Check if sub element or not + if ( + thisField + .closest(".acf-field-object") + .parents(".acf-field-setting-fc_layout").length || + thisField + .closest(".acf-field-object") + .parents(".acf-field-setting-sub_fields").length + ) { + return "sub"; + } else { + return "parent"; + } +} - // Output type for supported fields (This is used if a field has multiple types of output methods, such as Array, ID or URL) - if (typeOfField == 'image' || - typeOfField == 'gallery' || - typeOfField == 'link' || - typeOfField == 'taxonomy' || - typeOfField == 'user' || - typeOfField == 'file') { - var returnType = $(this) - .closest(".acf-field-object") - .find(".acf-field-setting-return_format li label.selected input") - .val(); - } else { - var returnType = null; - } - debug ? console.log('returnType: ' + returnType) : null; +function getPlace() { + // Get field group place (basically checks if field is in options page or not) + return $('.refresh-location-rule option[selected="selected"]').val(); +} - // Check if sub element or not - if ($(this).closest(".acf-field-object").parents('.acf-field-setting-fc_layout').length || - $(this).closest(".acf-field-object").parents('.acf-field-setting-sub_fields').length) { - var seniority = 'sub'; - } else { - var seniority = 'parent'; - } - debug ? console.log('seniority: ' + seniority) : null; +function copyFieldCode() { + // Copy field code + $("body").on("click", ".copy-field-code", function() { + var thisField = $(this), + fieldName = getFieldName(thisField), + typeOfField = getTypeOfField(thisField), + returnType = getReturnType(thisField, typeOfField), + seniority = getSeniority(thisField), + place = getPlace(thisField), + subFields = ""; + if ($(this).hasClass("include-sub-fields")) { + $(this) + .closest(".acf-field-object") + .children(".settings") + .children(".acf-table") + .children(".acf-field-settings") + .children(".acf-field-setting-sub_fields") + .children(".acf-input") + .children(".acf-field-list-wrap") + .children(".acf-field-list") + .children(".acf-field-object") + .children(".handle") + .find(".copy-field-code.exclude-sub-fields") + .each(function() { + var thisField = $(this), + fieldName = getFieldName(thisField), + typeOfField = getTypeOfField(thisField), + returnType = getReturnType(thisField, typeOfField), + seniority = getSeniority(thisField), + place = getPlace(thisField); - // Get field group place (basically checks if field is in options page or not) - var place = $('.refresh-location-rule option[selected="selected"]').val(); - debug ? console.log('place: ' + place) : null; + acf_field(fieldName, typeOfField, returnType, seniority, place, ''); - acf_field(fieldName, typeOfField, returnType, seniority, place); + subFields += sessionStorage.getItem("fieldcode"); + }); + } + acf_field(fieldName, typeOfField, returnType, seniority, place, subFields); }); } From 20d98c112c9b34d60af2a8f58f67245f3d39a461 Mon Sep 17 00:00:00 2001 From: RostiMelk Date: Mon, 30 Mar 2020 17:11:36 +0200 Subject: [PATCH 2/9] Fixed a bug where copyCodeToClipboard function would get ran for each sub field --- scripts/fields.js | 7 +- scripts/scripts.js | 210 ++++++++++++++++++++++----------------------- 2 files changed, 110 insertions(+), 107 deletions(-) diff --git a/scripts/fields.js b/scripts/fields.js index 51ebec5..270ab79 100644 --- a/scripts/fields.js +++ b/scripts/fields.js @@ -1,4 +1,4 @@ -function acf_field(fieldName, typeOfField, returnType, seniority, place, subFields) { +function acf_field(copyCode, fieldName, typeOfField, returnType, seniority, place, subFields) { switch (typeOfField) { // Basic case "text": @@ -221,5 +221,8 @@ function acf_field(fieldName, typeOfField, returnType, seniority, place, subFiel // Copy to clipboard sessionStorage.removeItem('fieldcode'); sessionStorage.setItem('fieldcode', '\n' + fieldCode + '\n'); - copyCodeToClipboard(fieldCode); + + if(copyCode) { + copyCodeToClipboard(fieldCode); + } } diff --git a/scripts/scripts.js b/scripts/scripts.js index 0957567..2d63777 100755 --- a/scripts/scripts.js +++ b/scripts/scripts.js @@ -7,10 +7,10 @@ $(document).ready(function() { function copyMessage() { $("body").append( - '
Copied to clipboard!
' + '
Copied to clipboard!
' ); setTimeout(function() { - $(".acftools-message").remove(); + $(".acftools-message").remove(); }, 3000); } @@ -18,12 +18,12 @@ function copyToClipboard(element) { var temp = $(""); $("body").append(temp); temp - .val( - $(element) - .text() - .trim() - ) - .select(); + .val( + $(element) + .text() + .trim() + ) + .select(); document.execCommand("copy"); temp.remove(); copyMessage(); @@ -31,82 +31,82 @@ function copyToClipboard(element) { function copyCodeToClipboard(element) { var temp = $("") - .val(element) - .appendTo("body") - .select(); + .val(element) + .appendTo("body") + .select(); document.execCommand("copy"); copyMessage(); } function copyFieldName() { $("body").on("click", ".li-field-name", function() { - copyToClipboard(this); + copyToClipboard(this); }); } function openDocs() { $("body").on("click", ".li-field-type", function() { - var type = $(this) - .closest(".acf-field-object") - .attr("data-type"); - var url = type.replace("_", "-"); - window.open( - "https://www.advancedcustomfields.com/resources/" + url, - "_blank" - ); + var type = $(this) + .closest(".acf-field-object") + .attr("data-type"); + var url = type.replace("_", "-"); + window.open( + "https://www.advancedcustomfields.com/resources/" + url, + "_blank" + ); }); } function fieldError() { alert( - "Unsupported field, submit an issue on: \nhttps://github.com/RostiMelk/ACF-Tools" + "Unsupported field, submit an issue on: \nhttps://github.com/RostiMelk/ACF-Tools" ); throw new Error( - "Unsupported field, submit an issue on: \nhttps://github.com/RostiMelk/ACF-Tools" + "Unsupported field, submit an issue on: \nhttps://github.com/RostiMelk/ACF-Tools" ); } function appendCopyCodeBtns() { // Append copy field code button function appendCopyCodeBtn() { - $( - '.acf-field-object:not([data-type="accordion"], [data-type="message"], [data-type="tab"]) .row-options' - ).append( - 'Copy code' - ); + $( + '.acf-field-object:not([data-type="accordion"], [data-type="message"], [data-type="tab"]) .row-options' + ).append( + 'Copy code' + ); } appendCopyCodeBtn(); // Append buttons to new fields $("body").on("click", ".add-field", function() { - $(".copy-field-code").remove(); - setTimeout(function() { - appendCopyCodeBtn(); - }, 10); + $(".copy-field-code").remove(); + setTimeout(function() { + appendCopyCodeBtn(); + }, 10); }); // Append copy all fields code button function appendCopyAllCodeBtn() { - $( - '.acf-field-object[data-type="repeater"], .acf-field-object[data-type="group"]' - ) - .children(".handle") - .find(".row-options") - .append( - 'Copy with sub fields' - ); + $( + '.acf-field-object[data-type="repeater"], .acf-field-object[data-type="group"]' + ) + .children(".handle") + .find(".row-options") + .append( + 'Copy with sub fields' + ); } appendCopyAllCodeBtn(); // Append copy all fields code button new fields $("body").on("click", ".add-field", function() { - $(".copy-field-code").remove(); - setTimeout(function() { - appendCopyAllCodeBtn(); - }, 10); + $(".copy-field-code").remove(); + setTimeout(function() { + appendCopyAllCodeBtn(); + }, 10); }); $("body").on("change", ".acf-field-setting-type .field-type", function() { - $(".copy-field-code.include-sub-fields").remove(); - setTimeout(function() { - appendCopyAllCodeBtn(); - }, 1000); + $(".copy-field-code.include-sub-fields").remove(); + setTimeout(function() { + appendCopyAllCodeBtn(); + }, 1000); }); } @@ -118,44 +118,44 @@ function getTypeOfField(thisField) { function getFieldName(thisField) { // Get field name return thisField - .closest(".handle") - .find(".li-field-name") - .text() - .trim(); + .closest(".handle") + .find(".li-field-name") + .text() + .trim(); } function getReturnType(thisField, typeOfField) { // Output type for supported fields (This is used if a field has multiple types of output methods, such as Array, ID or URL) if ( - typeOfField == "image" || - typeOfField == "gallery" || - typeOfField == "link" || - typeOfField == "taxonomy" || - typeOfField == "user" || - typeOfField == "file" + typeOfField == "image" || + typeOfField == "gallery" || + typeOfField == "link" || + typeOfField == "taxonomy" || + typeOfField == "user" || + typeOfField == "file" ) { - return thisField - .closest(".acf-field-object") - .find(".acf-field-setting-return_format li label.selected input") - .val(); + return thisField + .closest(".acf-field-object") + .find(".acf-field-setting-return_format li label.selected input") + .val(); } else { - return null; + return null; } } function getSeniority(thisField) { // Check if sub element or not if ( - thisField - .closest(".acf-field-object") - .parents(".acf-field-setting-fc_layout").length || - thisField - .closest(".acf-field-object") - .parents(".acf-field-setting-sub_fields").length + thisField + .closest(".acf-field-object") + .parents(".acf-field-setting-fc_layout").length || + thisField + .closest(".acf-field-object") + .parents(".acf-field-setting-sub_fields").length ) { - return "sub"; + return "sub"; } else { - return "parent"; + return "parent"; } } @@ -165,41 +165,41 @@ function getPlace() { } function copyFieldCode() { - // Copy field code - $("body").on("click", ".copy-field-code", function() { - var thisField = $(this), - fieldName = getFieldName(thisField), - typeOfField = getTypeOfField(thisField), - returnType = getReturnType(thisField, typeOfField), - seniority = getSeniority(thisField), - place = getPlace(thisField), - subFields = ""; - if ($(this).hasClass("include-sub-fields")) { - $(this) - .closest(".acf-field-object") - .children(".settings") - .children(".acf-table") - .children(".acf-field-settings") - .children(".acf-field-setting-sub_fields") - .children(".acf-input") - .children(".acf-field-list-wrap") - .children(".acf-field-list") - .children(".acf-field-object") - .children(".handle") - .find(".copy-field-code.exclude-sub-fields") - .each(function() { - var thisField = $(this), - fieldName = getFieldName(thisField), - typeOfField = getTypeOfField(thisField), - returnType = getReturnType(thisField, typeOfField), - seniority = getSeniority(thisField), - place = getPlace(thisField); + // Copy field code + $("body").on("click", ".copy-field-code", function() { + var thisField = $(this), + fieldName = getFieldName(thisField), + typeOfField = getTypeOfField(thisField), + returnType = getReturnType(thisField, typeOfField), + seniority = getSeniority(thisField), + place = getPlace(thisField), + subFields = ""; + if (thisField.hasClass("include-sub-fields")) { + thisField + .closest(".acf-field-object") + .children(".settings") + .children(".acf-table") + .children(".acf-field-settings") + .children(".acf-field-setting-sub_fields") + .children(".acf-input") + .children(".acf-field-list-wrap") + .children(".acf-field-list") + .children(".acf-field-object") + .children(".handle") + .find(".copy-field-code.exclude-sub-fields") + .each(function() { + var thisField = $(this), + fieldName = getFieldName(thisField), + typeOfField = getTypeOfField(thisField), + returnType = getReturnType(thisField, typeOfField), + seniority = getSeniority(thisField), + place = getPlace(thisField); - acf_field(fieldName, typeOfField, returnType, seniority, place, ''); + acf_field(copyCode = false, fieldName, typeOfField, returnType, seniority, place, ""); - subFields += sessionStorage.getItem("fieldcode"); - }); - } - acf_field(fieldName, typeOfField, returnType, seniority, place, subFields); - }); + subFields += sessionStorage.getItem("fieldcode"); + }); + } + acf_field(copyCode = true, fieldName, typeOfField, returnType, seniority, place, subFields); + }); } From b7a18d5a91002ee11319a6a5f92eb09623283344 Mon Sep 17 00:00:00 2001 From: RostiMelk Date: Mon, 30 Mar 2020 17:20:05 +0200 Subject: [PATCH 3/9] Clear sessionStorage after copying code --- scripts/scripts.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/scripts/scripts.js b/scripts/scripts.js index 2d63777..cdc4a55 100755 --- a/scripts/scripts.js +++ b/scripts/scripts.js @@ -201,5 +201,8 @@ function copyFieldCode() { }); } acf_field(copyCode = true, fieldName, typeOfField, returnType, seniority, place, subFields); + + // Clear session storage + sessionStorage.removeItem("fieldcode"); }); } From a8746ba2267faf9484cced62e352e27eb793b02c Mon Sep 17 00:00:00 2001 From: RostiMelk Date: Mon, 30 Mar 2020 17:23:16 +0200 Subject: [PATCH 4/9] Updated readme from master --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 2cef374..3aa49f9 100755 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ This is a Chrome extension for Advanced Custom Fields to help with developing si ### Features -- Copy code for field with the matching selected setting and field name +- Copy code for field with the matching selected setting and field name ([video demo here](https://youtu.be/_gyqUem8jKo)) - Quickly copy an ACF field name - Quickly open documentation for field @@ -26,4 +26,4 @@ You can also [find the extension on the Chrome web store](https://chrome.google. --- -_View more of my work on [rosti.no](https://rosti.no)_ +_View more of my work on [rosti.no](https://rosti.no)_ \ No newline at end of file From 0be0ac165a77ecaa043c45e15962968feb9fd70d Mon Sep 17 00:00:00 2001 From: RostiMelk Date: Mon, 30 Mar 2020 19:29:46 +0200 Subject: [PATCH 5/9] Fixed tab adjustment for appeded sub fields --- scripts/fields.js | 11 ++++++++--- scripts/scripts.js | 4 ++-- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/scripts/fields.js b/scripts/fields.js index 270ab79..5db1eaa 100644 --- a/scripts/fields.js +++ b/scripts/fields.js @@ -1,4 +1,4 @@ -function acf_field(copyCode, fieldName, typeOfField, returnType, seniority, place, subFields) { +function acf_field(appendCode, fieldName, typeOfField, returnType, seniority, place, subFields) { switch (typeOfField) { // Basic case "text": @@ -217,12 +217,17 @@ function acf_field(copyCode, fieldName, typeOfField, returnType, seniority, plac var fieldNameRe = new RegExp("'" + fieldName + "'", 'g'); fieldCode = fieldCode.replace(fieldNameRe, "'" + fieldName + "', 'options'"); } + + // Adjust tabs for appended sub fields + if(appendCode) { + fieldCode = fieldCode.replace(/\n/g, '\n\t\t') + } // Copy to clipboard sessionStorage.removeItem('fieldcode'); - sessionStorage.setItem('fieldcode', '\n' + fieldCode + '\n'); + sessionStorage.setItem('fieldcode', '\n\t\t' + fieldCode + '\n'); - if(copyCode) { + if(!appendCode) { copyCodeToClipboard(fieldCode); } } diff --git a/scripts/scripts.js b/scripts/scripts.js index cdc4a55..bb27546 100755 --- a/scripts/scripts.js +++ b/scripts/scripts.js @@ -195,12 +195,12 @@ function copyFieldCode() { seniority = getSeniority(thisField), place = getPlace(thisField); - acf_field(copyCode = false, fieldName, typeOfField, returnType, seniority, place, ""); + acf_field(appendCode = true, fieldName, typeOfField, returnType, seniority, place, ""); subFields += sessionStorage.getItem("fieldcode"); }); } - acf_field(copyCode = true, fieldName, typeOfField, returnType, seniority, place, subFields); + acf_field(appendCode = false, fieldName, typeOfField, returnType, seniority, place, subFields); // Clear session storage sessionStorage.removeItem("fieldcode"); From f243726403edae328a8939592129d661853b6266 Mon Sep 17 00:00:00 2001 From: RostiMelk Date: Mon, 30 Mar 2020 22:06:50 +0200 Subject: [PATCH 6/9] cleanup --- scripts/scripts.js | 221 +++++++++++++++++++++------------------------ styles/style.css | 7 ++ 2 files changed, 111 insertions(+), 117 deletions(-) diff --git a/scripts/scripts.js b/scripts/scripts.js index bb27546..6c21d88 100755 --- a/scripts/scripts.js +++ b/scripts/scripts.js @@ -1,17 +1,13 @@ $(document).ready(function() { - copyFieldName(); - openDocs(); - copyFieldCode(); - appendCopyCodeBtns(); + copyFieldName(); + openDocs(); + copyFieldCode(); + appendCopyCodeBtns(); }); function copyMessage() { - $("body").append( - '
Copied to clipboard!
' - ); - setTimeout(function() { $(".acftools-message").remove(); - }, 3000); + $("body").append('
Copied to clipboard!
'); } function copyToClipboard(element) { @@ -19,9 +15,9 @@ function copyToClipboard(element) { $("body").append(temp); temp .val( - $(element) - .text() - .trim() + $(element) + .text() + .trim() ) .select(); document.execCommand("copy"); @@ -30,138 +26,127 @@ function copyToClipboard(element) { } function copyCodeToClipboard(element) { - var temp = $("") - .val(element) - .appendTo("body") - .select(); - document.execCommand("copy"); - copyMessage(); + var temp = $("") + .val(element) + .appendTo("body") + .select(); + document.execCommand("copy"); + temp.remove(); + copyMessage(); } function copyFieldName() { - $("body").on("click", ".li-field-name", function() { - copyToClipboard(this); - }); + $("body").on("click", ".li-field-name", function() { + copyToClipboard(this); + }); } function openDocs() { - $("body").on("click", ".li-field-type", function() { - var type = $(this) - .closest(".acf-field-object") - .attr("data-type"); - var url = type.replace("_", "-"); - window.open( - "https://www.advancedcustomfields.com/resources/" + url, - "_blank" - ); - }); + $("body").on("click", ".li-field-type", function() { + var type = $(this) + .closest(".acf-field-object") + .attr("data-type"); + var url = type.replace("_", "-"); + window.open("https://www.advancedcustomfields.com/resources/" + url, "_blank"); + }); } function fieldError() { - alert( - "Unsupported field, submit an issue on: \nhttps://github.com/RostiMelk/ACF-Tools" - ); - throw new Error( - "Unsupported field, submit an issue on: \nhttps://github.com/RostiMelk/ACF-Tools" - ); + alert("Unsupported field, submit an issue on: \nhttps://github.com/RostiMelk/ACF-Tools"); + throw new Error("Unsupported field, submit an issue on: \nhttps://github.com/RostiMelk/ACF-Tools"); } function appendCopyCodeBtns() { - // Append copy field code button - function appendCopyCodeBtn() { - $( - '.acf-field-object:not([data-type="accordion"], [data-type="message"], [data-type="tab"]) .row-options' - ).append( - 'Copy code' - ); - } - appendCopyCodeBtn(); - // Append buttons to new fields - $("body").on("click", ".add-field", function() { - $(".copy-field-code").remove(); - setTimeout(function() { - appendCopyCodeBtn(); - }, 10); - }); - // Append copy all fields code button - function appendCopyAllCodeBtn() { - $( - '.acf-field-object[data-type="repeater"], .acf-field-object[data-type="group"]' - ) - .children(".handle") - .find(".row-options") - .append( - 'Copy with sub fields' - ); - } - appendCopyAllCodeBtn(); - // Append copy all fields code button new fields - $("body").on("click", ".add-field", function() { - $(".copy-field-code").remove(); - setTimeout(function() { - appendCopyAllCodeBtn(); - }, 10); - }); - $("body").on("change", ".acf-field-setting-type .field-type", function() { - $(".copy-field-code.include-sub-fields").remove(); - setTimeout(function() { - appendCopyAllCodeBtn(); - }, 1000); - }); + // Append copy field code button + function appendCopyCodeBtn() { + $('.acf-field-object:not([data-type="accordion"], [data-type="message"], [data-type="tab"]) .row-options') + .append('Copy code'); + } + appendCopyCodeBtn(); + // Append buttons to new fields + $("body").on("click", ".add-field", function() { + $(".copy-field-code").remove(); + setTimeout(function() { + appendCopyCodeBtn(); + }, 10); + }); + + // Append copy all fields code button + function appendCopyAllCodeBtn() { + $('.acf-field-object[data-type="repeater"], .acf-field-object[data-type="group"]') + .children(".handle") + .find(".row-options") + .append('Copy code with sub fields'); + } + appendCopyAllCodeBtn(); + + // Append copy all fields code button new fields + $("body").on("click", ".add-field", function() { + $(".copy-field-code").remove(); + setTimeout(function() { + appendCopyAllCodeBtn(); + }, 10); + }); + $("body").on("change", ".acf-field-setting-type .field-type", function() { + $(".copy-field-code.include-sub-fields").remove(); + setTimeout(function() { + appendCopyAllCodeBtn(); + }, 1000); + }); } function getTypeOfField(thisField) { - // Get type of field - return thisField.closest(".acf-field-object").attr("data-type"); + // Get type of field + return thisField.closest(".acf-field-object").attr("data-type"); } function getFieldName(thisField) { - // Get field name - return thisField - .closest(".handle") - .find(".li-field-name") - .text() - .trim(); + // Get field name + return thisField + .closest(".handle") + .find(".li-field-name") + .text() + .trim(); } function getReturnType(thisField, typeOfField) { - // Output type for supported fields (This is used if a field has multiple types of output methods, such as Array, ID or URL) - if ( - typeOfField == "image" || - typeOfField == "gallery" || - typeOfField == "link" || - typeOfField == "taxonomy" || - typeOfField == "user" || - typeOfField == "file" - ) { - return thisField - .closest(".acf-field-object") - .find(".acf-field-setting-return_format li label.selected input") - .val(); - } else { - return null; - } + // Output type for supported fields (This is used if a field has multiple types of output methods, such as Array, ID or URL) + if ( + typeOfField == "image" || + typeOfField == "gallery" || + typeOfField == "link" || + typeOfField == "taxonomy" || + typeOfField == "user" || + typeOfField == "file" + ) { + return thisField + .closest(".acf-field-object") + .find(".acf-field-setting-return_format li label.selected input") + .val(); + } else { + return null; + } } function getSeniority(thisField) { - // Check if sub element or not - if ( - thisField - .closest(".acf-field-object") - .parents(".acf-field-setting-fc_layout").length || - thisField - .closest(".acf-field-object") - .parents(".acf-field-setting-sub_fields").length - ) { - return "sub"; - } else { - return "parent"; - } + // Check if sub element or not + if ( + thisField + .closest(".acf-field-object") + .parents(".acf-field-setting-fc_layout").length || + thisField + .closest(".acf-field-object") + .parents(".acf-field-setting-sub_fields").length + ) { + return "sub"; + } else { + return "parent"; + } } function getPlace() { - // Get field group place (basically checks if field is in options page or not) - return $('.refresh-location-rule option[selected="selected"]').val(); + // Get field group place (basically checks if field is in options page or not) + return $('.refresh-location-rule option[selected="selected"]').val(); } function copyFieldCode() { @@ -174,6 +159,8 @@ function copyFieldCode() { seniority = getSeniority(thisField), place = getPlace(thisField), subFields = ""; + + // If user clicked copy code with sub fields. This will get the first level of child items. if (thisField.hasClass("include-sub-fields")) { thisField .closest(".acf-field-object") diff --git a/styles/style.css b/styles/style.css index 292bb73..981c68a 100755 --- a/styles/style.css +++ b/styles/style.css @@ -6,6 +6,13 @@ pointer-events: none; opacity: 0; animation: fade-in-slide 2s ease-in-out; + display: block; + font-size: 14.4px; + background-color: #C7E8CA; + padding: 8px; + text-align: center; + max-width: 100%; + width: 900px; } @keyframes fade-in-slide { From 2619212eded72cab4c447bd73d809f39d1ecf40e Mon Sep 17 00:00:00 2001 From: RostiMelk Date: Mon, 30 Mar 2020 22:20:18 +0200 Subject: [PATCH 7/9] cleanup --- scripts/fields.js | 4 +++- scripts/scripts.js | 31 ++++++++++++++++++------------- styles/style.css | 2 +- 3 files changed, 22 insertions(+), 15 deletions(-) diff --git a/scripts/fields.js b/scripts/fields.js index 5db1eaa..bf9800c 100644 --- a/scripts/fields.js +++ b/scripts/fields.js @@ -227,7 +227,9 @@ function acf_field(appendCode, fieldName, typeOfField, returnType, seniority, pl sessionStorage.removeItem('fieldcode'); sessionStorage.setItem('fieldcode', '\n\t\t' + fieldCode + '\n'); + + if(!appendCode) { - copyCodeToClipboard(fieldCode); + copyCodeToClipboard(fieldCode, subFields); } } diff --git a/scripts/scripts.js b/scripts/scripts.js index 6c21d88..839b356 100755 --- a/scripts/scripts.js +++ b/scripts/scripts.js @@ -5,12 +5,12 @@ $(document).ready(function() { appendCopyCodeBtns(); }); -function copyMessage() { +function copyMessage(message) { $(".acftools-message").remove(); - $("body").append('
Copied to clipboard!
'); + $("body").append('
'+message+'
'); } -function copyToClipboard(element) { +function copyStringToClipboard(element) { var temp = $(""); $("body").append(temp); temp @@ -22,23 +22,28 @@ function copyToClipboard(element) { .select(); document.execCommand("copy"); temp.remove(); - copyMessage(); } -function copyCodeToClipboard(element) { +function copyFieldName() { + $("body").on("click", ".li-field-name", function() { + copyStringToClipboard(this); + copyMessage('Copied field name to clipboard!'); + }); +} + +function copyCodeToClipboard(fieldCode, subFields) { var temp = $("") - .val(element) + .val(fieldCode) .appendTo("body") .select(); document.execCommand("copy"); temp.remove(); - copyMessage(); -} - -function copyFieldName() { - $("body").on("click", ".li-field-name", function() { - copyToClipboard(this); - }); + + if(subFields.length) { + copyMessage('Copied code with sub fields to clipboard!'); + } else { + copyMessage('Copied code to clipboard!'); + } } function openDocs() { diff --git a/styles/style.css b/styles/style.css index 981c68a..878a5e3 100755 --- a/styles/style.css +++ b/styles/style.css @@ -5,7 +5,7 @@ transform: translate(-50%, 0px); pointer-events: none; opacity: 0; - animation: fade-in-slide 2s ease-in-out; + animation: fade-in-slide 2.5s ease-in-out; display: block; font-size: 14.4px; background-color: #C7E8CA; From 3a49af5c6bfca2664b8dcc42b57125ef8f311388 Mon Sep 17 00:00:00 2001 From: RostiMelk Date: Mon, 30 Mar 2020 22:28:54 +0200 Subject: [PATCH 8/9] Update readme --- README.md | 4 ++-- scripts/scripts.js | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 3aa49f9..df3e769 100755 --- a/README.md +++ b/README.md @@ -9,8 +9,8 @@ This is a Chrome extension for Advanced Custom Fields to help with developing si ### Features - Copy code for field with the matching selected setting and field name ([video demo here](https://youtu.be/_gyqUem8jKo)) -- Quickly copy an ACF field name -- Quickly open documentation for field +- Quickly copy an ACF field name by just clicking the field name +- Quickly open documentation for field by just clicking the field type ### Installation diff --git a/scripts/scripts.js b/scripts/scripts.js index 839b356..f7c43de 100755 --- a/scripts/scripts.js +++ b/scripts/scripts.js @@ -81,7 +81,7 @@ function appendCopyCodeBtns() { $('.acf-field-object[data-type="repeater"], .acf-field-object[data-type="group"]') .children(".handle") .find(".row-options") - .append('Copy code with sub fields'); + .append('Copy code with sub fields'); } appendCopyAllCodeBtn(); From 8114393f614071ad7085979514bafdabe66420d6 Mon Sep 17 00:00:00 2001 From: RostiMelk Date: Wed, 1 Apr 2020 00:18:45 +0200 Subject: [PATCH 9/9] Fixed typo/syntax error in gallery field --- scripts/fields.js | 10 +++++----- scripts/scripts.js | 2 +- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/scripts/fields.js b/scripts/fields.js index bf9800c..d41b53f 100644 --- a/scripts/fields.js +++ b/scripts/fields.js @@ -49,13 +49,13 @@ function acf_field(appendCode, fieldName, typeOfField, returnType, seniority, pl case "file": switch (returnType) { case "array": - var fieldCode = ";\n" + "\t\">\n" + ""; + var fieldCode = ";\n" + "\t\">\n" + ""; break; case "url": var fieldCode = "\n" + "\t'>Download File\n" + "\n"; break; case "id": - var fieldCode = "\n" + "\t\">Download File\n" + ""; + var fieldCode = "\n" + "\t\">Download File\n" + ""; break; default: fieldError(); @@ -73,7 +73,7 @@ function acf_field(appendCode, fieldName, typeOfField, returnType, seniority, pl case "gallery": switch (returnType) { case "array": - var fieldCode = "\n" + "\t\n" + "\t\t\">\n" + "\t\t\t\" alt=\"\"/>\n" + "\t\t\n" + "\t\n" + ""; + var fieldCode = "\n" + "\t\n" + "\t\t\">\n" + "\t\t\t\" alt=\"\"/>\n" + "\t\t\n" + "\t\n" + ""; break; case "url": var fieldCode = ""; @@ -126,7 +126,7 @@ function acf_field(appendCode, fieldName, typeOfField, returnType, seniority, pl break; case "page_link": - var fieldCode = "\n" + "\t\n" + "\t\t\" >\n" + "\t\n" + ""; + var fieldCode = "\n" + "\t\n" + "\t\t\" >\n" + "\t\n" + ""; break; case "relationship": @@ -232,4 +232,4 @@ function acf_field(appendCode, fieldName, typeOfField, returnType, seniority, pl if(!appendCode) { copyCodeToClipboard(fieldCode, subFields); } -} +} \ No newline at end of file diff --git a/scripts/scripts.js b/scripts/scripts.js index f7c43de..5421e98 100755 --- a/scripts/scripts.js +++ b/scripts/scripts.js @@ -197,4 +197,4 @@ function copyFieldCode() { // Clear session storage sessionStorage.removeItem("fieldcode"); }); -} +} \ No newline at end of file