| @@ -200,8 +200,27 @@ | ||
| 200 | 200 | value, |
| 201 | 201 | }); |
| 202 | 202 | } |
| 203 | 203 | |
| 204 | + // If the block's saved value references a legacy form that | |
| 205 | + // isn't in field.values (legacy forms are not offered as | |
| 206 | + // new selection choices), append it here so the dropdown | |
| 207 | + // still reflects the saved selection. | |
| 208 | + if ( | |
| 209 | + fieldProperties.value && | |
| 210 | + field.legacy_values && | |
| 211 | + typeof field.legacy_values[fieldProperties.value] !== | |
| 212 | + 'undefined' && | |
| 213 | + !fieldOptions.some( | |
| 214 | + (option) => option.value === fieldProperties.value | |
| 215 | + ) | |
| 216 | + ) { | |
| 217 | + fieldOptions.push({ | |
| 218 | + label: field.legacy_values[fieldProperties.value], | |
| 219 | + value: fieldProperties.value, | |
| 220 | + }); | |
| 221 | + } | |
| 222 | + | |
| 204 | 223 | // Sort field's options alphabetically by label. |
| 205 | 224 | fieldOptions.sort(function (x, y) { |
| 206 | 225 | const a = x.label.toUpperCase(), |
| 207 | 226 | b = y.label.toUpperCase(); |
| @@ -226,8 +245,27 @@ | ||
| 226 | 245 | value, |
| 227 | 246 | }); |
| 228 | 247 | } |
| 229 | 248 | |
| 249 | + // If the block's saved value references a legacy form that | |
| 250 | + // isn't in field.values (because legacy forms are not | |
| 251 | + // offered as new selection choices), append it here so the | |
| 252 | + // dropdown still reflects the saved selection. | |
| 253 | + if ( | |
| 254 | + fieldProperties.value && | |
| 255 | + field.legacy_values && | |
| 256 | + typeof field.legacy_values[fieldProperties.value] !== | |
| 257 | + 'undefined' && | |
| 258 | + !fieldOptions.some( | |
| 259 | + (option) => option.value === fieldProperties.value | |
| 260 | + ) | |
| 261 | + ) { | |
| 262 | + fieldOptions.push({ | |
| 263 | + label: field.legacy_values[fieldProperties.value], | |
| 264 | + value: fieldProperties.value, | |
| 265 | + }); | |
| 266 | + } | |
| 267 | + | |
| 230 | 268 | // Sort field's options alphabetically by label. |
| 231 | 269 | fieldOptions.sort(function (x, y) { |
| 232 | 270 | const a = x.label.toUpperCase(), |
| 233 | 271 | b = y.label.toUpperCase(); |
| @@ -1010,16 +1048,37 @@ | ||
| 1010 | 1048 | field = Object.assign({}, field, { |
| 1011 | 1049 | values: fieldValues[key] || field.values, |
| 1012 | 1050 | }); |
| 1013 | 1051 | |
| 1052 | + // Build the help element. Supports HTML in the description by | |
| 1053 | + // rendering each line as a paragraph via element.RawHTML, which | |
| 1054 | + // allows inline tags like <code> and <a> to render correctly. | |
| 1055 | + let helpElement; | |
| 1056 | + if (Array.isArray(field.description)) { | |
| 1057 | + helpElement = el( | |
| 1058 | + 'span', | |
| 1059 | + {}, | |
| 1060 | + field.description.map(function (line, index) { | |
| 1061 | + return el( | |
| 1062 | + 'p', | |
| 1063 | + { | |
| 1064 | + key: 'help-' + index, | |
| 1065 | + style: { margin: '0 0 0.5em 0' }, | |
| 1066 | + }, | |
| 1067 | + el(element.RawHTML, {}, line) | |
| 1068 | + ); | |
| 1069 | + }) | |
| 1070 | + ); | |
| 1071 | + } else if (field.description) { | |
| 1072 | + helpElement = el(element.RawHTML, {}, field.description); | |
| 1073 | + } | |
| 1074 | + | |
| 1014 | 1075 | // Define some field properties shared across all field types. |
| 1015 | 1076 | const fieldProperties = { |
| 1016 | 1077 | key: 'convertkit_plugin_sidebar_' + key, |
| 1017 | 1078 | id: 'convertkit_plugin_sidebar_' + key, |
| 1018 | 1079 | label: field.label, |
| 1019 | - help: Array.isArray(field.description) | |
| 1020 | - ? field.description.join('\n\n') | |
| 1021 | - : field.description, | |
| 1080 | + help: helpElement, | |
| 1022 | 1081 | value: settings[key] || field.default_value || '', |
| 1023 | 1082 | |
| 1024 | 1083 | // Add __next40pxDefaultSize and __nextHasNoMarginBottom properties, |
| 1025 | 1084 | // preventing deprecation notices in the block editor and opt in to the new styles |
| @@ -1125,8 +1184,9 @@ | ||
| 1125 | 1184 | ); |
| 1126 | 1185 | |
| 1127 | 1186 | if (hasOptgroups) { |
| 1128 | 1187 | const children = []; |
| 1188 | + const seenValues = new Set(); | |
| 1129 | 1189 | |
| 1130 | 1190 | for (const value of Object.keys(field.values)) { |
| 1131 | 1191 | if ( |
| 1132 | 1192 | typeof field.values[value] === 'object' && |
| @@ -1137,8 +1197,9 @@ | ||
| 1137 | 1197 | const groupChildren = []; |
| 1138 | 1198 | for (const groupValue of Object.keys( |
| 1139 | 1199 | field.values[value].values |
| 1140 | 1200 | )) { |
| 1201 | + seenValues.add(groupValue); | |
| 1141 | 1202 | groupChildren.push( |
| 1142 | 1203 | el( |
| 1143 | 1204 | 'option', |
| 1144 | 1205 | { |
| @@ -1160,8 +1221,9 @@ | ||
| 1160 | 1221 | ) |
| 1161 | 1222 | ); |
| 1162 | 1223 | } else { |
| 1163 | 1224 | // Option within optgroup. |
| 1225 | + seenValues.add(value); | |
| 1164 | 1226 | children.push( |
| 1165 | 1227 | el( |
| 1166 | 1228 | 'option', |
| 1167 | 1229 | { value, key: value }, |
| @@ -1170,8 +1232,30 @@ | ||
| 1170 | 1232 | ); |
| 1171 | 1233 | } |
| 1172 | 1234 | } |
| 1173 | 1235 | |
| 1236 | + // If the saved value references a legacy form that isn't | |
| 1237 | + // otherwise in the dropdown, append it so the current | |
| 1238 | + // selection remains visible. | |
| 1239 | + if ( | |
| 1240 | + fieldProperties.value && | |
| 1241 | + field.legacy_values && | |
| 1242 | + typeof field.legacy_values[fieldProperties.value] !== | |
| 1243 | + 'undefined' && | |
| 1244 | + !seenValues.has(fieldProperties.value) | |
| 1245 | + ) { | |
| 1246 | + children.push( | |
| 1247 | + el( | |
| 1248 | + 'option', | |
| 1249 | + { | |
| 1250 | + value: fieldProperties.value, | |
| 1251 | + key: fieldProperties.value, | |
| 1252 | + }, | |
| 1253 | + field.legacy_values[fieldProperties.value] | |
| 1254 | + ) | |
| 1255 | + ); | |
| 1256 | + } | |
| 1257 | + | |
| 1174 | 1258 | return el(SelectControl, fieldProperties, ...children); |
| 1175 | 1259 | } |
| 1176 | 1260 | |
| 1177 | 1261 | // Options only, no optgroups. |
| @@ -1179,8 +1263,26 @@ | ||
| 1179 | 1263 | for (const value of Object.keys(field.values)) { |
| 1180 | 1264 | fieldOptions.push({ |
| 1181 | 1265 | label: field.values[value], |
| 1182 | 1266 | value, |
| 1267 | + }); | |
| 1268 | + } | |
| 1269 | + | |
| 1270 | + // If the saved value references a legacy form that isn't in | |
| 1271 | + // field.values, append it so the current selection remains | |
| 1272 | + // visible in the dropdown. | |
| 1273 | + if ( | |
| 1274 | + fieldProperties.value && | |
| 1275 | + field.legacy_values && | |
| 1276 | + typeof field.legacy_values[fieldProperties.value] !== | |
| 1277 | + 'undefined' && | |
| 1278 | + !fieldOptions.some( | |
| 1279 | + (option) => option.value === fieldProperties.value | |
| 1280 | + ) | |
| 1281 | + ) { | |
| 1282 | + fieldOptions.push({ | |
| 1283 | + label: field.legacy_values[fieldProperties.value], | |
| 1284 | + value: fieldProperties.value, | |
| 1183 | 1285 | }); |
| 1184 | 1286 | } |
| 1185 | 1287 | |
| 1186 | 1288 | // Sort options alphabetically by label. |