Fossil

Check-in [079030faaf]
Login

Check-in [079030faaf]

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Implemented preview mode for plain text and wiki/embedded doc, but how to populate the necessary iframe for HTML preview, without a URL back to the preview content, eludes me.
Downloads: Tarball | ZIP archive
Timelines: family | ancestors | descendants | both | checkin-without-checkout
Files: files | file ages | folders
SHA3-256: 079030faaf762c218c7ffa04f8e617aaa07aebbce9c740483c0030fb0f48cf19
User & Date: stephan 2020-05-04 12:10:03.732
Context
2020-05-04
12:46
Implemented HTML-in-iframe preview. Related internal cleanups. ... (check-in: ac309281e6 user: stephan tags: checkin-without-checkout)
12:10
Implemented preview mode for plain text and wiki/embedded doc, but how to populate the necessary iframe for HTML preview, without a URL back to the preview content, eludes me. ... (check-in: 079030faaf user: stephan tags: checkin-without-checkout)
2020-05-03
14:57
Moved /fileedit and friends to fileedit.c. ... (check-in: 6cdb091adb user: stephan tags: checkin-without-checkout)
Changes
Unified Diff Ignore Whitespace Patch
Changes to src/default_css.txt.
860
861
862
863
864
865
866

867
868
869
870
871
872
873
// }
// #setup_skinedit_css_defaults > tbody > tr > td:nth-of-type(2) > div {
//   max-width: 30em;
//   overflow: auto;
// }
// .fileedit-XXX => /fileedit page
.fileedit-form textarea {

  width: 100%;
}
.fileedit-form fieldset {
  border-radius: 0.5em;
}
.fileedit-form .fileedit-options {
  padding: 0.5em;







>







860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
// }
// #setup_skinedit_css_defaults > tbody > tr > td:nth-of-type(2) > div {
//   max-width: 30em;
//   overflow: auto;
// }
// .fileedit-XXX => /fileedit page
.fileedit-form textarea {
  font-family: monospace;
  width: 100%;
}
.fileedit-form fieldset {
  border-radius: 0.5em;
}
.fileedit-form .fileedit-options {
  padding: 0.5em;
891
892
893
894
895
896
897







898
899
900
901
902
903
904
  border-radius: 0.5em;
}
code.fileedit-manifest {
  display: block;
  height: 16em;
  overflow: auto;
}







.input-with-label {
  border: 1px inset #808080;
  border-radius: 0.5em;
  padding: 0.25em 0.4em;
  margin: 0 0.5em;
  display: inline-block;
  cursor: pointer;







>
>
>
>
>
>
>







892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
  border-radius: 0.5em;
}
code.fileedit-manifest {
  display: block;
  height: 16em;
  overflow: auto;
}
div.fileedit-preview {
  margin: 0;
  padding: 0;
}
.fileedit-preview > div:first-child {
  border-bottom: 1px dashed;
}
.input-with-label {
  border: 1px inset #808080;
  border-radius: 0.5em;
  padding: 0.25em 0.4em;
  margin: 0 0.5em;
  display: inline-block;
  cursor: pointer;
Changes to src/fileedit.c.
903
904
905
906
907
908
909
910
911

912
913
914



























915
916
917
918
919
920
921
  if(0==phase){
    CX("<script nonce='%s'>", style_nonce());
  }else{
    CX("</script>\n");
  }
}

#if 0
/*

** This function is for potential TODO features for /fileedit.
** It's been tested with that code but is not currently used
** by it.



























*/
static void fileedit_emit_script_fetch(){
  fileedit_emit_script(0);
  CX("window.fossilFetch = function(path,opt){\n");
  CX("  if('function'===typeof opt){\n");
  CX("    opt={onload:opt};\n");
  CX("  }else{\n");







<

>
|
|
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







903
904
905
906
907
908
909

910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
  if(0==phase){
    CX("<script nonce='%s'>", style_nonce());
  }else{
    CX("</script>\n");
  }
}


/*
** Emits a script tag which defines window.fossilFetch(), which works
** similarly (not identically) to the not-quite-ubiquitous global
** fetch().
**
** JS usages:
**
** fossilFetch( URI, onLoadCallback );
**
** fossilFetch( URI, optionsObject );
**
** Where the optionsObject may be an object with any of these
** properties:
**
** - onload: callback(responseData) (default = output response to
**   console).
**
** - onerror: callback(XHR onload event) (default = no-op)
**
** - method: 'POST' | 'GET' (default = 'GET')
**
** Noting that URI must be relative to the top of the repository and
** must not start with a slash. It gets %R/ prepended to it.
**
** TODOs, if needed, include:
**
** optionsObject.params: object map of key/value pairs to append to the
** URI.
**
** optionsObject.payload: string or JSON-able object to POST as the
** payload.
**
*/
static void fileedit_emit_script_fetch(){
  fileedit_emit_script(0);
  CX("window.fossilFetch = function(path,opt){\n");
  CX("  if('function'===typeof opt){\n");
  CX("    opt={onload:opt};\n");
  CX("  }else{\n");
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
  CX("      opt.onload(this.response);\n");
  CX("    }\n");
  CX("  }\n");
  CX("  x.send();");
  CX("};\n");
  fileedit_emit_script(1);
};
#endif /* fileedit_emit_script_fetch() */

/*
** Outputs a labeled checkbox element:
**
** <span class='input-with-label' title={{zTip}}>
**   <input type='checkbox' name={{zFieldName}} value={{zValue}}
**          {{isChecked ? " checked : ""}}/>







<







960
961
962
963
964
965
966

967
968
969
970
971
972
973
  CX("      opt.onload(this.response);\n");
  CX("    }\n");
  CX("  }\n");
  CX("  x.send();");
  CX("};\n");
  fileedit_emit_script(1);
};


/*
** Outputs a labeled checkbox element:
**
** <span class='input-with-label' title={{zTip}}>
**   <input type='checkbox' name={{zFieldName}} value={{zValue}}
**          {{isChecked ? " checked : ""}}/>
960
961
962
963
964
965
966










































































967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985



986
987
988
989
990
991
992
993


994
995
996
997
998
999
1000
1001
1002




















1003
1004

1005
1006
1007
1008
1009
1010
1011
    CX(" title='%h'", zTip);
  }
  CX("><input type='checkbox' name='%s' value='%T'%s/>",
     zFieldName,
     zValue ? zValue : "", isChecked ? " checked" : "");
  CX("<span>%h</span></span>", zLabel);
}











































































/*
** WEBPAGE: fileedit
**
** EXPERIMENTAL and subject to change and removal at any time. The goal
** is to allow online edits of files.
**
** Query parameters:
**
**    file=FILENAME    Repo-relative path to the file.
**    r=VERSION        Checkin version, using any unambiguous
**                     supported symbolic version name.
**
** All other parameters are for internal use only, submitted via the
** form-submission process, and may change with any given revision of
** this code.
*/
void fileedit_page(){
  const char * zFilename = PD("file",P("name")); /* filename */



  const char * zRev = P("r");           /* checkin version */
  const char * zContent = P("content"); /* file content */
  const char * zComment = P("comment"); /* checkin comment */
  CheckinMiniInfo cimi;                 /* Checkin state */
  int submitMode = 0;                   /* See mapping below */
  int vid, newVid = 0;                  /* checkin rid */
  char * zFileUuid = 0;                 /* File content UUID */
  int frid = 0;                         /* File content rid */


  Blob err = empty_blob;                /* Error report */
  const char * zFlagCheck = 0;          /* Temp url flag holder */
  Blob endScript = empty_blob;          /* Script code to run at the
                                           end. This content will be
                                           combined into a single JS
                                           function call, thus each
                                           entry must end with a
                                           semicolon. */
  Stmt stmt = empty_Stmt;




















#define fail(EXPR) blob_appendf EXPR; goto end_footer


  login_check_credentials();
  if( !g.perm.Write ){
    login_needed(g.anon.Write);
    return;
  }
  db_begin_transaction();
  CheckinMiniInfo_init(&cimi);







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>


















|
>
>
>






<

>
>









>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>


>







986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094

1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
    CX(" title='%h'", zTip);
  }
  CX("><input type='checkbox' name='%s' value='%T'%s/>",
     zFieldName,
     zValue ? zValue : "", isChecked ? " checked" : "");
  CX("<span>%h</span></span>", zLabel);
}

enum fileedit_render_preview_flags {
FE_PREVIEW_LINE_NUMBERS = 1
};

/*
** Performs the PREVIEW mode for /filepage.
*/
static void fileedit_render_preview(Blob * pContent,
                                    const char *zFilename,
                                    int flags){
  const char * zMime;
  enum render_modes {PLAIN_TEXT = 0, HTML, WIKI};
  int renderMode = PLAIN_TEXT;
  zMime = mimetype_from_name(zFilename);
  if( zMime ){
    if( fossil_strcmp(zMime, "text/html")==0 ){
      renderMode = HTML;
    }else if( fossil_strcmp(zMime, "text/x-fossil-wiki")==0
              || fossil_strcmp(zMime, "text/x-markdown")==0 ){
      renderMode = WIKI;
    }
  }
  CX("<div class='fileedit-preview'>");
  CX("<div>Preview</div>");
  switch(renderMode){
    case HTML:{
      CX("<iframe width='100%%' frameborder='0' marginwidth='0' "
         "marginheight='0' sandbox='allow-same-origin' id='ifm1' "
         "srcdoc='Not yet working: not sure how to "
         "populate the iframe.'"
         "></iframe>");
#if 0
      fileedit_emit_script(0);
      CX("document.getElementById('ifm1').addEventListener('load',"
         "function(){\n"
         "console.debug('iframe=',this);\n"
         "this.height=this.contentDocument.documentElement."
         "scrollHeight + 75;\n"
         "this.contentDocument.body.innerHTML=`%h`;\n"
         "});\n",
         blob_str(pContent));
      /* Potential TODO: use iframe.srcdoc:
      **
      ** https://caniuse.com/#search=srcdoc
      ** https://stackoverflow.com/questions/22381216/escape-quotes-in-an-iframe-srcdoc-value
      **
      ** Doing so would require escaping the quote characters which match
      ** the srcdoc='xyz' quotes.
      */
      fileedit_emit_script(1);
#endif
      break;
    }
    case WIKI:
      wiki_render_by_mimetype(pContent, zMime);
      break;
    case PLAIN_TEXT:
    default:{
      const char *zExt = strrchr(zFilename,'.');
      const char *zContent = blob_str(pContent);
      if(FE_PREVIEW_LINE_NUMBERS & flags){
        output_text_with_line_numbers(zContent, "on");
      }else if(zExt && zExt[1]){
        CX("<pre><code class='language-%s'>%h</code></pre>",
           zExt+1, zContent);
      }else{
        CX("<pre>%h</pre>", zExt+1, zContent);
      }
      break;
    }
  }
  CX("</div><!--.fileedit-preview-->\n");
}

/*
** WEBPAGE: fileedit
**
** EXPERIMENTAL and subject to change and removal at any time. The goal
** is to allow online edits of files.
**
** Query parameters:
**
**    file=FILENAME    Repo-relative path to the file.
**    r=VERSION        Checkin version, using any unambiguous
**                     supported symbolic version name.
**
** All other parameters are for internal use only, submitted via the
** form-submission process, and may change with any given revision of
** this code.
*/
void fileedit_page(){
  const char * zFilename = PD("file",P("name"));
                                        /* filename. We'll accept 'name'
                                           because that param is handled
                                           specially by the core. */
  const char * zRev = P("r");           /* checkin version */
  const char * zContent = P("content"); /* file content */
  const char * zComment = P("comment"); /* checkin comment */
  CheckinMiniInfo cimi;                 /* Checkin state */
  int submitMode = 0;                   /* See mapping below */
  int vid, newVid = 0;                  /* checkin rid */

  int frid = 0;                         /* File content rid */
  int previewLn = P("preview_ln")!=0;   /* Line number mode */
  char * zFileUuid = 0;                 /* File content UUID */
  Blob err = empty_blob;                /* Error report */
  const char * zFlagCheck = 0;          /* Temp url flag holder */
  Blob endScript = empty_blob;          /* Script code to run at the
                                           end. This content will be
                                           combined into a single JS
                                           function call, thus each
                                           entry must end with a
                                           semicolon. */
  Stmt stmt = empty_Stmt;
  const int loadMode = 0;               /* See next comment block */
  /* loadMode: How to populate the TEXTAREA:
  **
  ** 0: HTML encode: despite my personal reservations regarding HTML
  ** escaping, this seems to be the only reliable approach
  ** until/unless we completely AJAXify this page.
  **
  ** 1: JSON mode: JSON-izes the file content and injects it, via JS,
  ** into the editor TEXTAREA. This works wonderfully until the input
  ** file contains an raw <SCRIPT> tag, at which points the HTML
  ** parser chokes on it.
  **
  ** 2: AJAX mode: can only load content from the db, not preview/dry-run
  ** content. Unless this page is refactored to work solely over AJAX
  ** (which is a potential TODO), this method is only useful on the
  ** initial hit to this page, where the file is loaded.
  **
  ** loadMode is not generally configurable: change it only for
  ** testing/development purposes.
  */
#define fail(EXPR) blob_appendf EXPR; goto end_footer

  assert(loadMode==0 || loadMode==1 || loadMode==2);
  login_check_credentials();
  if( !g.perm.Write ){
    login_needed(g.anon.Write);
    return;
  }
  db_begin_transaction();
  CheckinMiniInfo_init(&cimi);
1021
1022
1023
1024
1025
1026
1027
1028

1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039

1040
1041
1042
1043
1044
1045
1046

  style_header("File Editor");
  /* As of this point, don't use return or fossil_fatal(), use
  ** fail((&err,...))  instead so that we can be sure to do any
  ** cleanup and end the transaction cleanly.
  */
  if(!zRev || !*zRev || !zFilename || !*zFilename){
    fail((&err,"Missing required URL parameters."));

  }
  if(0==fileedit_is_editable(zFilename)){
    fail((&err,"Filename <code>%h</code> is disallowed "
          "by the <code>fileedit-glob</code> repository "
          "setting.",
          zFilename));
  }
  vid = symbolic_name_to_rid(zRev, "ci");
  if(0==vid){
    fail((&err,"Could not resolve checkin version."));
  }


  /* Find the repo-side file entry or fail... */
  cimi.zParentUuid = rid_to_uuid(vid);
  db_prepare(&stmt, "SELECT uuid, perm FROM files_of_checkin "
             "WHERE filename=%Q %s AND checkinID=%d",
             zFilename, filename_collation(), vid);
  if(SQLITE_ROW==db_step(&stmt)){







|
>











>







1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173

  style_header("File Editor");
  /* As of this point, don't use return or fossil_fatal(), use
  ** fail((&err,...))  instead so that we can be sure to do any
  ** cleanup and end the transaction cleanly.
  */
  if(!zRev || !*zRev || !zFilename || !*zFilename){
    fail((&err,"Missing required URL parameters: "
          "file=FILE and r=CHECKIN"));
  }
  if(0==fileedit_is_editable(zFilename)){
    fail((&err,"Filename <code>%h</code> is disallowed "
          "by the <code>fileedit-glob</code> repository "
          "setting.",
          zFilename));
  }
  vid = symbolic_name_to_rid(zRev, "ci");
  if(0==vid){
    fail((&err,"Could not resolve checkin version."));
  }
  cimi.zFilename = mprintf("%s",zFilename);

  /* Find the repo-side file entry or fail... */
  cimi.zParentUuid = rid_to_uuid(vid);
  db_prepare(&stmt, "SELECT uuid, perm FROM files_of_checkin "
             "WHERE filename=%Q %s AND checkinID=%d",
             zFilename, filename_collation(), vid);
  if(SQLITE_ROW==db_step(&stmt)){
1073
1074
1075
1076
1077
1078
1079


1080



1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096

1097
1098
1099
1100
1101
1102
1103
          "<code>%h</code>",zFilename));
  }

  /* All set. Here we go... */

  CX("<h1>Editing:</h1>");
  CX("<p class='fileedit-hint'>");


  CX("File: <code>%h</code><br>"



     "Checkin Version: <code id='r-label'>%s</code><br>",
     zFilename, cimi.zParentUuid);
  CX("Permalink: <code>"
     "<a id='permalink' href='%R/fileedit?file=%T&r=%!S'>"
     "/fileedit?file=%T&r=%!S</a></code><br>"
     "(Clicking the permalink will reload the page and discard "
     "all edits!)",
     zFilename, cimi.zParentUuid,
     zFilename, cimi.zParentUuid);
  CX("</p>");
  CX("<p>This page is <em>far from complete</em> and may still have "
     "significant bugs. USE AT YOUR OWN RISK, preferably on a test "
     "repo.</p>\n");
  
  CX("<form action='%R/fileedit' method='POST' "
     "class='fileedit-form'>\n");


  /******* Hidden fields *******/
  CX("<input type='hidden' name='r' value='%s'>",
     cimi.zParentUuid);
  CX("<input type='hidden' name='file' value='%T'>",
     zFilename);








>
>
|
>
>
>
|
|












|
|
>







1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
          "<code>%h</code>",zFilename));
  }

  /* All set. Here we go... */

  CX("<h1>Editing:</h1>");
  CX("<p class='fileedit-hint'>");
  CX("File: "
     "[<a id='finfo-link' href='%R/finfo?name=%T&m=%!S'>info</a>] "
     "<code>%h</code><br>",
     zFilename, zFileUuid, zFilename);
  CX("Checkin Version: "
     "[<a id='r-link' href='%R/info/%!S'>info</a>] "
     "<code id='r-label'>%s</code><br>",
     cimi.zParentUuid, cimi.zParentUuid);
  CX("Permalink: <code>"
     "<a id='permalink' href='%R/fileedit?file=%T&r=%!S'>"
     "/fileedit?file=%T&r=%!S</a></code><br>"
     "(Clicking the permalink will reload the page and discard "
     "all edits!)",
     zFilename, cimi.zParentUuid,
     zFilename, cimi.zParentUuid);
  CX("</p>");
  CX("<p>This page is <em>far from complete</em> and may still have "
     "significant bugs. USE AT YOUR OWN RISK, preferably on a test "
     "repo.</p>\n");
  
  CX("<form action='%R/fileedit%s' method='POST' "
     "class='fileedit-form'>\n",
     submitMode>0 ? "#options" : "");

  /******* Hidden fields *******/
  CX("<input type='hidden' name='r' value='%s'>",
     cimi.zParentUuid);
  CX("<input type='hidden' name='file' value='%T'>",
     zFilename);

1111
1112
1113
1114
1115
1116
1117



1118


1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
  CX("<div class='fileedit-hint'>Comments use the Fossil wiki markup "
     "syntax.</div>"/*TODO: radiobuttons for fossil/me/plain text*/);

  /******* Content *******/
  CX("<h3>File Content</h3>\n");
  CX("<textarea name='content' id='fileedit-content' "
     "rows='20' cols='80'>");



  CX("Loading...");


  CX("</textarea>\n");
  /******* Flags/options *******/
  CX("<fieldset class='fileedit-options'>"
     "<legend>Options</legend><div>"
     /* Chrome does not sanely lay out multiple
     ** fieldset children after the <legend>, so
     ** a containing div is necessary. */);
  /*
  ** TODO?: date-override date selection field. Maybe use
  ** an input[type=datetime-local].







>
>
>
|
>
>


|







1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
  CX("<div class='fileedit-hint'>Comments use the Fossil wiki markup "
     "syntax.</div>"/*TODO: radiobuttons for fossil/me/plain text*/);

  /******* Content *******/
  CX("<h3>File Content</h3>\n");
  CX("<textarea name='content' id='fileedit-content' "
     "rows='20' cols='80'>");
  if(0==loadMode){
    CX("%h",blob_str(&cimi.fileContent));
  }else{
    CX("Loading...");
    /* Performed via JS later on */
  }
  CX("</textarea>\n");
  /******* Flags/options *******/
  CX("<fieldset class='fileedit-options' id='options'>"
     "<legend>Options</legend><div>"
     /* Chrome does not sanely lay out multiple
     ** fieldset children after the <legend>, so
     ** a containing div is necessary. */);
  /*
  ** TODO?: date-override date selection field. Maybe use
  ** an input[type=datetime-local].
1190
1191
1192
1193
1194
1195
1196
1197

1198
1199
1200
1201
1202
1203
1204
1205






1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222

1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235











1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
    CX("<option value='2'%s>Windows EOLs</option>",
       eolMode==2 ? " selected" : "");
    CX("</select>");
  }

  CX("</div></fieldset>") /* end of checkboxes */;

  /******* Buttons *******/  

  CX("<fieldset class='fileedit-options'>"
     "<legend>Tell the server to...</legend><div>");
  CX("<button type='submit' name='submit' value='1'>"
     "Save</button>");
  CX("<button type='submit' name='submit' value='2'>"
     "Preview (TODO)</button>");
  CX("<button type='submit' name='submit' value='3'>"
     "Diff (TODO)</button>");






  CX("</div></fieldset>");

  /******* End of form *******/    
  CX("</form>\n");

  {
    /* Populate the editor...
    **
    ** To avoid all escaping-related issues, we have to do this one of
    ** two ways:
    **
    ** 1) Fetch the content via AJAX. That only works if the content
    **    is already in the db, but not for edited versions.
    **
    ** 2) Store the content as JSON and feed it into the textarea
    **    using JavaScript.
    */

    char const * zQuoted = 0;
    if(blob_size(&cimi.fileContent)>0){
      db_prepare(&stmt, "SELECT json_quote(%B)", &cimi.fileContent);
      db_step(&stmt);
      zQuoted = db_column_text(&stmt,0);
    }
    blob_appendf(&endScript,
                 "/* populate editor form */\n"
                 "document.getElementById('fileedit-content')"
                 ".value=%s;", zQuoted ? zQuoted : "'';\n");
    if(stmt.pStmt){
      db_finalize(&stmt);
    }











  }

  if(1==submitMode/*save*/){
    Blob manifest = empty_blob;
    char * zNewUuid = 0;
    /*cimi.flags |= CIMINI_STRONGLY_PREFER_DELTA;*/
    if(zComment && *zComment){
      blob_append(&cimi.comment, zComment, -1);
    }else{
      fail((&err,"Empty comment is not permitted."));
    }
    /*cimi.pParent = manifest_get(vid, CFTYPE_MANIFEST, 0);
      assert(cimi.pParent && "We know vid is valid.");*/
    cimi.zFilename = mprintf("%s",zFilename);
    cimi.pMfOut = &manifest;
    checkin_mini(&cimi, &newVid, &err);
    if(newVid!=0){
      zNewUuid = rid_to_uuid(newVid);
      CX("<h3>Manifest%s: %S</h3><pre>"
         "<code class='fileedit-manifest'>%h</code>"
         "</pre>",







|
>





|


>
>
>
>
>
>





<
|
<
<
<
<
<
<
<
<
<
<
>













>
>
>
>
>
>
>
>
>
>
>













<







1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355

1356










1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394

1395
1396
1397
1398
1399
1400
1401
    CX("<option value='2'%s>Windows EOLs</option>",
       eolMode==2 ? " selected" : "");
    CX("</select>");
  }

  CX("</div></fieldset>") /* end of checkboxes */;

  /******* Buttons *******/
  CX("<a id='buttons'></a>");
  CX("<fieldset class='fileedit-options'>"
     "<legend>Tell the server to...</legend><div>");
  CX("<button type='submit' name='submit' value='1'>"
     "Save</button>");
  CX("<button type='submit' name='submit' value='2'>"
     "Preview</button>");
  CX("<button type='submit' name='submit' value='3'>"
     "Diff (TODO)</button>");
  CX("<br>");
  style_labeled_checkbox("preview_ln",
                         "Add line numbers to plain-text previews?", "1",
                         "If on, plain-text files (only) will get "
                         "line numbers added to the preview.",
                         previewLn);
  CX("</div></fieldset>");

  /******* End of form *******/    
  CX("</form>\n");


  /* Dynamically populate the editor... */










  if(1==loadMode || (2==loadMode && submitMode>0)){
    char const * zQuoted = 0;
    if(blob_size(&cimi.fileContent)>0){
      db_prepare(&stmt, "SELECT json_quote(%B)", &cimi.fileContent);
      db_step(&stmt);
      zQuoted = db_column_text(&stmt,0);
    }
    blob_appendf(&endScript,
                 "/* populate editor form */\n"
                 "document.getElementById('fileedit-content')"
                 ".value=%s;", zQuoted ? zQuoted : "'';\n");
    if(stmt.pStmt){
      db_finalize(&stmt);
    }
  }else if(2==loadMode){
    assert(submitMode==0);
    fileedit_emit_script_fetch();
    blob_appendf(&endScript,
                 "window.fossilFetch('raw/%s',{"
                 "onload: (r)=>document.getElementById('fileedit-content')"
                 ".value=r,"
                 "onerror:()=>document.getElementById('fileedit-content')"
                 ".value="
                 "'Error loading content'"
                 "});\n", zFileUuid);
  }

  if(1==submitMode/*save*/){
    Blob manifest = empty_blob;
    char * zNewUuid = 0;
    /*cimi.flags |= CIMINI_STRONGLY_PREFER_DELTA;*/
    if(zComment && *zComment){
      blob_append(&cimi.comment, zComment, -1);
    }else{
      fail((&err,"Empty comment is not permitted."));
    }
    /*cimi.pParent = manifest_get(vid, CFTYPE_MANIFEST, 0);
      assert(cimi.pParent && "We know vid is valid.");*/

    cimi.pMfOut = &manifest;
    checkin_mini(&cimi, &newVid, &err);
    if(newVid!=0){
      zNewUuid = rid_to_uuid(newVid);
      CX("<h3>Manifest%s: %S</h3><pre>"
         "<code class='fileedit-manifest'>%h</code>"
         "</pre>",
1267
1268
1269
1270
1271
1272
1273
1274





1275




1276
1277
1278
1279
1280
1281
1282

1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294

1295
1296
1297
1298
1299
1300
1301
                     "document.querySelector('input[type=checkbox]"
                     "[name=dry_run]').checked=true;\n");
        blob_appendf(&endScript,
                     "/* Update version number */\n"
                     "document.querySelector('input[name=r]')"
                     ".value=%Q;\n"
                     "document.querySelector('#r-label')"
                     ".innerText=%Q;\n",





                     zNewUuid, zNewUuid);




        blob_appendf(&endScript,
                     "/* Update permalink */\n"
                     "const urlFull='%R/fileedit?file=%T&r=%!S';\n"
                     "const urlShort='/fileedit?file=%T&r=%!S';\n"
                     "let link=document.querySelector('#permalink');\n"
                     "link.innerText=urlShort;\n"
                     "link.setAttribute('href',urlFull);\n",

                     zFilename, zNewUuid, zFilename, zNewUuid);
      }
      fossil_free(zNewUuid);
      zNewUuid = 0;
    }
    /* On error, the error message is in the err blob and will
    ** be emitted below. */
    cimi.pMfOut = 0;
    blob_reset(&manifest);
  }else if(2==submitMode/*preview*/){
    /* TODO */
    fail((&err,"Preview mode is still TODO."));

  }else if(3==submitMode/*diff*/){
    fail((&err,"Diff mode is still TODO."));
  }else{
    /* Ignore invalid submitMode value */
    goto end_footer;
  }








|
>
>
>
>
>
|
>
>
>
>







>
|









|
|
>







1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
                     "document.querySelector('input[type=checkbox]"
                     "[name=dry_run]').checked=true;\n");
        blob_appendf(&endScript,
                     "/* Update version number */\n"
                     "document.querySelector('input[name=r]')"
                     ".value=%Q;\n"
                     "document.querySelector('#r-label')"
                     ".innerText=%Q;\n"
                     "document.querySelector('#r-link')"
                     ".setAttribute('href', '%R/info/%!S');\n"
                     "document.querySelector('#finfo-link')"
                     ".setAttribute('href','%R/finfo?name=%T&m=%!S');\n",
                     /*input[name=r]:*/zNewUuid, /*#r-label:*/ zNewUuid,
                     /*#r-link:*/ zNewUuid,
                     /*#finfo-link:*/zFilename, zNewUuid);
        blob_appendf(&endScript,
                     "/* Updated finfo link */"
                     );
        blob_appendf(&endScript,
                     "/* Update permalink */\n"
                     "const urlFull='%R/fileedit?file=%T&r=%!S';\n"
                     "const urlShort='/fileedit?file=%T&r=%!S';\n"
                     "let link=document.querySelector('#permalink');\n"
                     "link.innerText=urlShort;\n"
                     "link.setAttribute('href',urlFull);\n",
                     cimi.zFilename, zNewUuid,
                     cimi.zFilename, zNewUuid);
      }
      fossil_free(zNewUuid);
      zNewUuid = 0;
    }
    /* On error, the error message is in the err blob and will
    ** be emitted below. */
    cimi.pMfOut = 0;
    blob_reset(&manifest);
  }else if(2==submitMode/*preview*/){
    int pflags = 0;
    if(previewLn) pflags |= FE_PREVIEW_LINE_NUMBERS;
    fileedit_render_preview(&cimi.fileContent, cimi.zFilename, pflags);
  }else if(3==submitMode/*diff*/){
    fail((&err,"Diff mode is still TODO."));
  }else{
    /* Ignore invalid submitMode value */
    goto end_footer;
  }