fix custom-fields

This commit is contained in:
joyqi 2024-10-24 22:36:51 +08:00
parent 4224adce49
commit 2a4bc21af2
4 changed files with 63 additions and 57 deletions

File diff suppressed because one or more lines are too long

View File

@ -5,7 +5,7 @@ $(document).ready(function () {
function attachDeleteEvent (el) {
$('button.btn-xs', el).click(function () {
if (confirm('<?php _e('确认要删除此字段吗?'); ?>')) {
$(this).parents('tr').fadeOut(function () {
$(this).parents('li').fadeOut(function () {
$(this).remove();
});
@ -14,20 +14,21 @@ $(document).ready(function () {
});
}
$('#custom-field table tbody tr').each(function () {
$('#custom-field .fields .field').each(function () {
attachDeleteEvent(this);
});
$('#custom-field button.operate-add').click(function () {
var html = '<tr><td><input type="text" name="fieldNames[]" placeholder="<?php _e('字段名称'); ?>" pattern="^[_a-zA-Z][_a-zA-Z0-9]*$" oninput="this.reportValidity()" class="text-s w-100"></td>'
+ '<td><select name="fieldTypes[]" id="">'
var html = '<li class="field"><div class="field-name"><input type="text" name="fieldNames[]" placeholder="<?php _e('字段名称'); ?>" pattern="^[_a-zA-Z][_a-zA-Z0-9]*$" oninput="this.reportValidity()" class="text-s w-100">'
+ '<select name="fieldTypes[]" id="">'
+ '<option value="str"><?php _e('字符'); ?></option>'
+ '<option value="int"><?php _e('整数'); ?></option>'
+ '<option value="float"><?php _e('小数'); ?></option>'
+ '</select></td>'
+ '<td><textarea name="fieldValues[]" placeholder="<?php _e('字段值'); ?>" class="text-s w-100" rows="2"></textarea></td>'
+ '<td><button type="button" class="btn btn-xs"><?php _e('删除'); ?></button></td></tr>',
el = $(html).hide().appendTo('#custom-field table tbody').fadeIn();
+ '<option value="json"><?php _e('JSON 结构'); ?></option>'
+ '</select></div>'
+ '<div class="field-value"><textarea name="fieldValues[]" placeholder="<?php _e('字段值'); ?>" class="text-s w-100" rows="2"></textarea>'
+ '<button type="button" class="btn btn-xs"><?php _e('删除'); ?></button></div></li>',
el = $(html).hide().appendTo('#custom-field .fields').fadeIn();
attachDeleteEvent(el);
});

View File

@ -6,28 +6,20 @@ $defaultFields = isset($post) ? $post->getDefaultFieldItems() : $page->getDefaul
<details id="custom-field"
class="typecho-post-option" <?php if (!empty($defaultFields) || !empty($fields)): ?>open<?php endif; ?>>
<summary><?php _e('自定义字段'); ?></summary>
<table class="typecho-list-table mono">
<colgroup>
<col width="20%"/>
<col width="15%"/>
<col width="55%"/>
<col width="10%"/>
</colgroup>
<ul class="fields mono">
<?php foreach ($defaultFields as $field): ?>
<?php [$label, $input] = $field; ?>
<tr>
<td><?php $label->render(); ?></td>
<td colspan="3"><?php $input->render(); ?></td>
</tr>
<li class="field">
<div class="field-name"><?php $label->render(); ?></div>
<div class="field-value"><?php $input->render(); ?></div>
</li>
<?php endforeach; ?>
<?php foreach ($fields as $field): ?>
<tr>
<td>
<li class="field">
<div class="field-name">
<label for="fieldname" class="sr-only"><?php _e('字段名称'); ?></label>
<input type="text" name="fieldNames[]" value="<?php echo htmlspecialchars($field['name']); ?>"
id="fieldname" pattern="^[_a-zA-Z][_a-zA-Z0-9]*$" oninput="this.reportValidity()" class="text-s w-100">
</td>
<td>
<label for="fieldtype" class="sr-only"><?php _e('字段类型'); ?></label>
<select name="fieldTypes[]" id="fieldtype">
<option
@ -39,43 +31,38 @@ $defaultFields = isset($post) ? $post->getDefaultFieldItems() : $page->getDefaul
<option
value="json"<?php if ('json' == $field['type']): ?> selected<?php endif; ?>><?php _e('JSON 结构'); ?></option>
</select>
</td>
<td>
</div>
<div class="field-value">
<label for="fieldvalue" class="sr-only"><?php _e('字段值'); ?></label>
<textarea name="fieldValues[]" id="fieldvalue" class="text-s w-100"
rows="2"><?php echo htmlspecialchars($field[($field['type'] == 'json' ? 'str' : $field['type']) . '_value']); ?></textarea>
</td>
<td>
<button type="button" class="btn btn-xs"><?php _e('删除'); ?></button>
</td>
</tr>
</div>
</li>
<?php endforeach; ?>
<?php if (empty($defaultFields) && empty($fields)): ?>
<tr>
<td>
<li class="field">
<div class="field-name">
<label for="fieldname" class="sr-only"><?php _e('字段名称'); ?></label>
<input type="text" name="fieldNames[]" placeholder="<?php _e('字段名称'); ?>" id="fieldname"
class="text-s w-100" pattern="^[_a-zA-Z][_a-zA-Z0-9]*$" oninput="this.reportValidity()">
</td>
<td>
<label for="fieldtype" class="sr-only"><?php _e('字段类型'); ?></label>
<select name="fieldTypes[]" id="fieldtype">
<option value="str"><?php _e('字符'); ?></option>
<option value="int"><?php _e('整数'); ?></option>
<option value="float"><?php _e('小数'); ?></option>
<option value="json"><?php _e('JSON 结构'); ?></option>
</select>
</td>
<td>
</div>
<div class="field-value">
<label for="fieldvalue" class="sr-only"><?php _e('字段值'); ?></label>
<textarea name="fieldValues[]" placeholder="<?php _e('字段值'); ?>" id="fieldvalue"
class="text-s w-100" rows="2"></textarea>
</td>
<td>
<button type="button" class="btn btn-xs"><?php _e('删除'); ?></button>
</td>
</tr>
</div>
</li>
<?php endif; ?>
</table>
</ul>
<div class="add">
<button type="button" class="btn btn-xs operate-add"><?php _e('+添加字段'); ?></button>
<div class="description kit-hidden-mb">

View File

@ -97,10 +97,6 @@
}
}
&.fold {
table, .description { display: none; }
}
.add {
display: flex;
justify-content: space-between;
@ -130,20 +126,42 @@
}
}
table {
margin-top: 10px;
border: none;
}
.fields {
list-style: none;
margin: 10px 0 0 0;
padding: 0;
td {
padding: 10px 5px;
font-size: .92857em;
border-bottom: 1px solid #F0F0EC;
vertical-align: top;
.field {
display: flex;
padding-bottom: 10px;
border-bottom: 1px solid #F0F0EC;
margin-bottom: 10px;
label {
font-size: 1em;
font-weight: normal;
&:last-child {
margin-bottom: 0;
}
.field-name {
display: flex;
input[name="fieldNames[]"] {
height: 27px;
width: 150px;
margin-right: 10px;
}
}
.field-value {
margin-left: 10px;
flex: 1;
display: flex;
textarea[name="fieldValues[]"] {
flex: 1;
margin-right: 10px;
resize: none;
}
}
}
}