1"use strict"; 2 3Object.defineProperty(exports, "__esModule", { 4 value: true 5}); 6exports.assignStylesValue = assignStylesValue; 7exports.getAppThemeLightNoActionBarGroup = getAppThemeLightNoActionBarGroup; 8exports.getProjectStylesXMLPathAsync = getProjectStylesXMLPathAsync; 9exports.getStyleParent = getStyleParent; 10exports.getStylesGroupAsObject = getStylesGroupAsObject; 11exports.getStylesItem = getStylesItem; 12exports.readStylesXMLAsync = readStylesXMLAsync; 13exports.removeStylesItem = removeStylesItem; 14exports.setStylesItem = setStylesItem; 15function _Paths() { 16 const data = require("./Paths"); 17 _Paths = function () { 18 return data; 19 }; 20 return data; 21} 22function _Resources() { 23 const data = require("./Resources"); 24 _Resources = function () { 25 return data; 26 }; 27 return data; 28} 29// Adds support for `tools:x` 30const fallbackResourceString = `<?xml version="1.0" encoding="utf-8"?><resources xmlns:tools="http://schemas.android.com/tools"></resources>`; 31async function readStylesXMLAsync({ 32 path, 33 fallback = fallbackResourceString 34}) { 35 return (0, _Resources().readResourcesXMLAsync)({ 36 path, 37 fallback 38 }); 39} 40async function getProjectStylesXMLPathAsync(projectRoot, { 41 kind 42} = {}) { 43 return (0, _Paths().getResourceXMLPathAsync)(projectRoot, { 44 kind, 45 name: 'styles' 46 }); 47} 48function ensureDefaultStyleResourceXML(xml) { 49 var _xml, _xml$resources; 50 xml = (0, _Resources().ensureDefaultResourceXML)(xml); 51 if (!Array.isArray((_xml = xml) === null || _xml === void 0 ? void 0 : (_xml$resources = _xml.resources) === null || _xml$resources === void 0 ? void 0 : _xml$resources.style)) { 52 xml.resources.style = []; 53 } 54 return xml; 55} 56function getStyleParent(xml, group) { 57 return (0, _Resources().findResourceGroup)(xml.resources.style, group); 58} 59function getStylesItem({ 60 name, 61 xml, 62 parent 63}) { 64 xml = ensureDefaultStyleResourceXML(xml); 65 const appTheme = getStyleParent(xml, parent); 66 if (!appTheme) { 67 return null; 68 } 69 if (appTheme.item) { 70 const existingItem = appTheme.item.filter(({ 71 $: head 72 }) => head.name === name)[0]; 73 74 // Don't want to 2 of the same item, so if one exists, we overwrite it 75 if (existingItem) { 76 return existingItem; 77 } 78 } 79 return null; 80} 81function setStylesItem({ 82 item, 83 xml, 84 parent 85}) { 86 xml = ensureDefaultStyleResourceXML(xml); 87 let appTheme = getStyleParent(xml, parent); 88 if (!appTheme) { 89 appTheme = (0, _Resources().buildResourceGroup)(parent); 90 xml.resources.style.push(appTheme); 91 } 92 if (appTheme.item) { 93 const existingItem = appTheme.item.filter(({ 94 $: head 95 }) => head.name === item.$.name)[0]; 96 97 // Don't want to 2 of the same item, so if one exists, we overwrite it 98 if (existingItem) { 99 existingItem._ = item._; 100 existingItem.$ = item.$; 101 } else { 102 appTheme.item.push(item); 103 } 104 } else { 105 appTheme.item = [item]; 106 } 107 return xml; 108} 109function removeStylesItem({ 110 name, 111 xml, 112 parent 113}) { 114 xml = ensureDefaultStyleResourceXML(xml); 115 const appTheme = getStyleParent(xml, parent); 116 if (appTheme !== null && appTheme !== void 0 && appTheme.item) { 117 const index = appTheme.item.findIndex(({ 118 $: head 119 }) => head.name === name); 120 if (index > -1) { 121 appTheme.item.splice(index, 1); 122 } 123 } 124 return xml; 125} 126 127// This is a very common theme so make it reusable. 128function getAppThemeLightNoActionBarGroup() { 129 return { 130 name: 'AppTheme', 131 parent: 'Theme.AppCompat.Light.NoActionBar' 132 }; 133} 134function assignStylesValue(xml, { 135 add, 136 value, 137 targetApi, 138 name, 139 parent 140}) { 141 if (add) { 142 return setStylesItem({ 143 xml, 144 parent, 145 item: (0, _Resources().buildResourceItem)({ 146 name, 147 targetApi, 148 value 149 }) 150 }); 151 } 152 return removeStylesItem({ 153 xml, 154 parent, 155 name 156 }); 157} 158 159/** 160 * Helper to convert a styles.xml parent's children into a simple k/v pair. 161 * Added for testing purposes. 162 * 163 * @param xml 164 * @returns 165 */ 166function getStylesGroupAsObject(xml, group) { 167 const xmlGroup = getStyleParent(xml, group); 168 return xmlGroup !== null && xmlGroup !== void 0 && xmlGroup.item ? (0, _Resources().getResourceItemsAsObject)(xmlGroup.item) : null; 169} 170//# sourceMappingURL=Styles.js.map