| ︙ | | | ︙ | |
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
|
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>",
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
>
<
|
<
<
|
<
<
<
<
<
|
>
>
|
<
|
<
<
<
<
<
<
<
<
|
<
<
<
<
<
<
<
<
<
<
<
|
|
|
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
|
zValue ? zValue : "", isChecked ? " checked" : "");
CX("<span>%h</span></span>", zLabel);
}
enum fileedit_render_preview_flags {
FE_PREVIEW_LINE_NUMBERS = 1
};
enum fileedit_render_modes {
FE_RENDER_PLAIN_TEXT = 0,
FE_RENDER_HTML,
FE_RENDER_WIKI
};
static int fileedit_render_mode_for_mimetype(const char * zMimetype){
int rc = FE_RENDER_PLAIN_TEXT;
if( zMimetype ){
if( fossil_strcmp(zMimetype, "text/html")==0 ){
rc = FE_RENDER_HTML;
}else if( fossil_strcmp(zMimetype, "text/x-fossil-wiki")==0
|| fossil_strcmp(zMimetype, "text/x-markdown")==0 ){
rc = FE_RENDER_WIKI;
}
}
return rc;
}
/*
** Performs the PREVIEW mode for /filepage.
*/
static void fileedit_render_preview(Blob * pContent,
const char *zFilename,
int flags,
int nIframeHeightEm){
const char * zMime;
int renderMode = FE_RENDER_PLAIN_TEXT;
zMime = mimetype_from_name(zFilename);
renderMode = fileedit_render_mode_for_mimetype(zMime);
CX("<div class='fileedit-preview'>");
CX("<div>Preview</div>");
switch(renderMode){
case FE_RENDER_HTML:{
char * z64 = encode64(blob_str(pContent), blob_size(pContent));
CX("<iframe width='100%%' frameborder='0' marginwidth='0' "
"style='height:%dem' "
"marginheight='0' sandbox='allow-same-origin' id='ifm1' "
"src='data:text/html;base64,%z'"
"></iframe>", nIframeHeightEm ? nIframeHeightEm : 40,
z64);
break;
}
case FE_RENDER_WIKI:
wiki_render_by_mimetype(pContent, zMime);
break;
case FE_RENDER_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>",
|
| ︙ | | | ︙ | |
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
|
** 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
|
>
>
>
>
>
|
>
|
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
|
** 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(){
enum submit_modes {
SUBMIT_NONE = 0, SUBMIT_SAVE = 1, SUBMIT_PREVIEW = 2,
SUBMIT_DIFF = 3
};
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 */
const char * zFileMime = 0; /* File mime type guess */
CheckinMiniInfo cimi; /* Checkin state */
int submitMode = SUBMIT_NONE; /* See mapping below */
int vid, newVid = 0; /* checkin rid */
int frid = 0; /* File content rid */
int previewLn = P("preview_ln")!=0; /* Line number mode */
int previewHtmlHeight = 0; /* iframe height (EMs) */
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
|
| ︙ | | | ︙ | |
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
|
login_check_credentials();
if( !g.perm.Write ){
login_needed(g.anon.Write);
return;
}
db_begin_transaction();
CheckinMiniInfo_init(&cimi);
submitMode = atoi(PD("submit","0"))
/* Submit modes: 0=initial request,
** 1=submit (save), 2=preview, 3=diff */;
zFlagCheck = P("comment_mimetype");
if(zFlagCheck){
cimi.zMimetype = mprintf("%s",zFlagCheck);
zFlagCheck = 0;
}
cimi.zUser = mprintf("%s",g.zLogin);
|
|
|
|
>
|
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
|
login_check_credentials();
if( !g.perm.Write ){
login_needed(g.anon.Write);
return;
}
db_begin_transaction();
CheckinMiniInfo_init(&cimi);
submitMode = atoi(PD("submit","0"));
if(submitMode < SUBMIT_NONE || submitMode > SUBMIT_DIFF){
submitMode = 0;
}
zFlagCheck = P("comment_mimetype");
if(zFlagCheck){
cimi.zMimetype = mprintf("%s",zFlagCheck);
zFlagCheck = 0;
}
cimi.zUser = mprintf("%s",g.zLogin);
|
| ︙ | | | ︙ | |
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
|
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)){
|
>
|
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
|
zFilename));
}
vid = symbolic_name_to_rid(zRev, "ci");
if(0==vid){
fail((&err,"Could not resolve checkin version."));
}
cimi.zFilename = mprintf("%s",zFilename);
zFileMime = mimetype_from_name(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)){
|
| ︙ | | | ︙ | |
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
|
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);
|
|
|
<
|
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
|
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#options' 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);
|
| ︙ | | | ︙ | |
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
|
/* 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].
*/
if(0==submitMode || P("dry_run")!=0){
cimi.flags |= CIMINI_DRY_RUN;
}
style_labeled_checkbox("dry_run", "Dry-run?", "1",
"In dry-run mode, the Save button performs "
"all work needed for saving but then rolls "
"back the transaction, and thus does not "
"really save.",
|
|
|
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
|
/* 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].
*/
if(SUBMIT_NONE==submitMode || P("dry_run")!=0){
cimi.flags |= CIMINI_DRY_RUN;
}
style_labeled_checkbox("dry_run", "Dry-run?", "1",
"In dry-run mode, the Save button performs "
"all work needed for saving but then rolls "
"back the transaction, and thus does not "
"really save.",
|
| ︙ | | | ︙ | |
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
|
style_labeled_checkbox("prefer_delta",
"Prefer delta manifest?", "1",
"Will create a delta manifest, instead of "
"baseline, if conditions are favorable to do "
"so. This option is only a suggestion.",
cimi.flags & CIMINI_PREFER_DELTA);
{/* EOL conversion policy... */
const int eolMode = submitMode==0 ? 0 : atoi(PD("eol","0"));
switch(eolMode){
case 1: cimi.flags |= CIMINI_CONVERT_EOL_UNIX; break;
case 2: cimi.flags |= CIMINI_CONVERT_EOL_WINDOWS; break;
default: cimi.flags |= CIMINI_CONVERT_EOL_INHERIT; break;
}
CX("<select name='eol' "
"title='EOL conversion policy, noting that form-processing "
|
|
>
|
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
|
style_labeled_checkbox("prefer_delta",
"Prefer delta manifest?", "1",
"Will create a delta manifest, instead of "
"baseline, if conditions are favorable to do "
"so. This option is only a suggestion.",
cimi.flags & CIMINI_PREFER_DELTA);
{/* EOL conversion policy... */
const int eolMode = submitMode==SUBMIT_NONE
? 0 : atoi(PD("eol","0"));
switch(eolMode){
case 1: cimi.flags |= CIMINI_CONVERT_EOL_UNIX; break;
case 2: cimi.flags |= CIMINI_CONVERT_EOL_WINDOWS; break;
default: cimi.flags |= CIMINI_CONVERT_EOL_INHERIT; break;
}
CX("<select name='eol' "
"title='EOL conversion policy, noting that form-processing "
|
| ︙ | | | ︙ | |
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
|
"<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."));
|
>
>
>
>
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
|
>
|
|
|
>
>
|
|
|
|
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
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
|
"<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>");
{
/* Options which depend on the current submitMode or
the file's preview rendering mode. */
const int renderMode = fileedit_render_mode_for_mimetype(zFileMime);
CX("<br>");
if(FE_RENDER_HTML==renderMode){
/* HTML preview mode iframe height... */
int i;
if(submitMode==SUBMIT_PREVIEW){
previewHtmlHeight = atoi(PD("preview_html_ems","0"));
}else{
previewHtmlHeight = 40;
}
/* Allow selection of HTML preview iframe height */
CX("<select name='preview_html_ems' "
"title='Height (in EMs) of the iframe used for HTML "
"preview.'>\n");
CX("<option disabled value='40'>HTML Preview Height (EMs)"
"</option>\n");
for( i = 20; i <= 100; i+=20 ){
CX("<option value='%d'%s>%d</option>\n",
i, (previewHtmlHeight==i) ? " selected" : "", i);
}
CX("</select>\n");
}
else if(FE_RENDER_PLAIN_TEXT == renderMode){
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>SUBMIT_NONE)){
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==SUBMIT_NONE);
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(SUBMIT_SAVE==submitMode){
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."));
|
| ︙ | | | ︙ | |
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
|
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;
}
end_footer:
|
|
|
>
|
|
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
|
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(SUBMIT_PREVIEW==submitMode){
int pflags = 0;
if(previewLn) pflags |= FE_PREVIEW_LINE_NUMBERS;
fileedit_render_preview(&cimi.fileContent, cimi.zFilename, pflags,
previewHtmlHeight);
}else if(SUBMIT_DIFF==submitMode/*diff*/){
fail((&err,"Diff mode is still TODO."));
}else{
/* Ignore invalid submitMode value */
goto end_footer;
}
end_footer:
|
| ︙ | | | ︙ | |