index: release v1.18 with only altgr index linked
[sheet.git] / word / edit.plp
index 9af73cdb3737e3cfbb6bed374f447f0ddf4809d0..55fbeac9923b1bfa1f098e87cdab9ed7830e0bc9 100644 (file)
@@ -15,6 +15,7 @@ EOT
 
 use List::Util qw( pairs pairkeys );
 use Shiar_Sheet::FormRow;
+use SQL::Abstract '1.83';
 use JSON;
 
 my $db = eval {
@@ -111,7 +112,7 @@ my %wordcol = (
        convert => {-label => 'Convert options', -json => 'image', -multiple => 1, -src => sub {
                return "data/word/32/$_[0]->{id}.jpg";
        }},
-       crop32  => {-label => 'Crop 3:2', -json => 'image'},
+       crop32  => {-json => 'image', type => 'hidden'}, # set by javascript interface
        story   => {-label => 'Story', type => 'textarea', hidden => 'hidden'},
 );
 
@@ -152,15 +153,21 @@ elsif (defined $post{form}) {{
                my @val = map { parseinput($_) } $post{'@'.$col}->@*;
                my $val = $colinfo->{-multiple} && @val ? \@val : $val[-1];
                if (my $jsoncol = $colinfo->{-json}) {
-                       defined $val and
                        $row->{$jsoncol}->{$col} = $val;  # hash will be encoded
+                       ref $_ eq 'HASH' or $_ = decode_json($_) for $replace->{$jsoncol} // ();
                }
                else {
                        $row->{$col} = $val;
                }
        }
        my $imagecol = $row->{image};  # backup image subcolumns
-       ref $_ eq 'HASH' and $_ = encode_json($_) for values %{$row};
+       while (my ($col, $val) = each %{$row}) {
+               # convert json subcolumns to database string
+               ref $val eq 'HASH' or next;
+               $val = { %{$_}, %{$val} } for $replace->{$col} // ();  # preserve unknown
+               defined $val->{$_} or delete $val->{$_} for keys %{$val};  # delete emptied
+               $row->{$col} = encode_json($val);
+       }
 
        if (!$row->{form} and $row->{lang}) {
                if ($row->{ref} ne 'delete') {
@@ -209,13 +216,13 @@ elsif (defined $post{form}) {{
        require Shiar_Sheet::ImagePrep;
        my $image = Shiar_Sheet::ImagePrep->new($wordcol{source}->{-src}->($row));
        my $reimage = eval {
-               ($imagecol->{source} // '') ne ($replace->{source} // '') or return;
+               ($imagecol->{source} // '') ne ($replace->{image}->{source} // '') or return;
                $image->download($imagecol->{source});
        };
        !$@ or Alert(["Source image not found", $@]);
 
-       $reimage ||= $row->{image} ~~ $replace->{image};  # different source
-       $reimage ||= $row->{cover} ~~ $replace->{cover};  # resize
+       $reimage ||= $row->{convert} ~~ $replace->{image}->{convert};  # different
+       $reimage ||= $row->{cover}   ~~ $replace->{image}->{cover};  # resize
        $reimage++ if $fields{rethumb};  # force refresh
        if ($reimage) {
                eval {
@@ -256,17 +263,20 @@ for my $col (@wordcols) {
        my ($attr, @span) = ref $info eq 'ARRAY' ? @{$info} : $info;
        next if delete $attr->{hidden} and not $row->{$col};
        my $title = ref $attr ? delete $attr->{-label} : $attr;
-       printf '<li><label for="%s">%s</label><p>', $col, $title;
+       printf '<li><label for="%s">%s</label><div>', $col, $title;
                printf '<span class=inline>';
                print $row->input($col => $attr);
                if (my $imgsrc = $attr->{-src}) {
-                       printf('<img id="%spreview" src="/%s" alt="%s"%s />',
-                               $col, $_, $row->{form}, $col eq 'source' && ' hidden'
+                       my $hide = $col eq 'source';
+                       printf '<figure id="%spreview">', $col unless $hide;
+                       printf('<img src="/%s" alt="%s"%s />',
+                               $_, $row->{form}, $hide && qq( id="${col}preview" hidden)
                        ) for grep { -e } $imgsrc->($row);
+                       printf '</figure>' unless $hide;
                }
                print $row->input($_ => delete $wordcol{$_}) for @span;
                print '</span>';
-       say '</p></li>';
+       say '</div></li>';
 }
 
 if (not $row->{ref}) {
@@ -326,6 +336,10 @@ while (my $ref = $children->hash) {
        <input type="submit" value="Add" />
 </form></li>
 </ul>
+
+<form id="search">
+       <input type="search" name="q" value="" placeholder="search" /><button type="submit">🔍</button>
+</form>
 </section>
 <:
 }