Skip to content

Commit 6fa747e

Browse files
committed
implement feature to disable veiw source code without rights
1 parent f063bfc commit 6fa747e

File tree

3 files changed

+39
-15
lines changed

3 files changed

+39
-15
lines changed

public/js/index.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,8 @@ Visibility.change(function (e, state) {
423423

424424
// when page ready
425425
$(document).ready(function () {
426+
if (ui.toolbar.edit.data('blockSource')) { replaceUrl(window.location.href) }
427+
426428
idle.checkAway()
427429
checkResponsive()
428430
// if in smaller screen, we don't need advanced scrollbar
@@ -478,7 +480,7 @@ $(document).ready(function () {
478480
changeMode(modeType.view)
479481
})
480482
key('ctrl+alt+b', function (e) {
481-
if (ui.toolbar.edit.data('blockSource')) return
483+
if (ui.toolbar.both.data('blockSource')) return
482484
changeMode(modeType.both)
483485
})
484486
// toggle-dropdown
@@ -502,6 +504,19 @@ $(window).on('error', function () {
502504
// setNeedRefresh();
503505
})
504506

507+
// replace url if user have not rights to veiw source code
508+
function replaceUrl(url) {
509+
const urlHasEdit = /\?edit/;
510+
const urlHasBoth = /\?both/;
511+
if (urlHasEdit.test(url)) {
512+
let newUrl = url.toString().replace(urlHasEdit, '?view');
513+
window.location.replace(newUrl);
514+
} else if (urlHasBoth.test(url)) {
515+
let newUrl = url.toString().replace(urlHasBoth, '?view');
516+
window.location.replace(newUrl);
517+
}
518+
}
519+
505520
setupSyncAreas(ui.area.codemirrorScroll, ui.area.view, ui.area.markdown, editor)
506521

507522
function autoSyncscroll () {
@@ -1570,10 +1585,12 @@ function importFromUrl (url) {
15701585

15711586
// mode
15721587
ui.toolbar.mode.click(function () {
1588+
if (ui.toolbar.mode.data('blockSource')) return
15731589
toggleMode()
15741590
})
15751591
// edit
15761592
ui.toolbar.edit.click(function () {
1593+
if (ui.toolbar.edit.data('blockSource')) return
15771594
changeMode(modeType.edit)
15781595
})
15791596
// view
@@ -1582,6 +1599,7 @@ ui.toolbar.view.click(function () {
15821599
})
15831600
// both
15841601
ui.toolbar.both.click(function () {
1602+
if (ui.toolbar.both.data('blockSource')) return
15851603
changeMode(modeType.both)
15861604
})
15871605

public/views/codimd/header.ejs

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -76,28 +76,34 @@
7676
<li role="presentation"><a role="menuitem" class="ui-help" href="#" data-toggle="modal" data-target=".help-modal"><i class="fa fa-question-circle fa-fw"></i> Help</a>
7777
</li>
7878
</ul>
79-
<a class="btn btn-link ui-mode">
79+
<% if ((permission === "limited" || "editable" || "freely") && !allowVisibleSource) { %>
80+
<a class="btn btn-link ui-mode" data-block-source="<%= !allowVisibleSource %>">
8081
<i class="fa fa-pencil"></i>
8182
</a>
83+
<% } else { %>
84+
<a class="btn btn-link ui-mode" data-block-source="<%= !allowVisibleSource %>">
85+
<i class="fa fa-pencil"></i>
86+
</a>
87+
<% } %>
8288
</div>
8389
</div>
8490
<div class="collapse navbar-collapse">
8591
<ul class="nav navbar-nav navbar-form navbar-left" style="padding:0;">
8692
<div class="btn-group" data-toggle="buttons">
8793
<% if ((permission === "limited" || "editable" || "freely") && !allowVisibleSource) { %>
88-
<label class="btn btn-default ui-edit" data-block-source="true" title="<%= __('You have no rights to edit this note')%>" disabled>
89-
<input type="radio" autocomplete="off"><i class="fa fa-pencil"></i>
90-
</label>
91-
<label class="btn btn-default ui-both" data-block-source="true" title="<%= __('You have no rights to edit this note')%>" disabled>
92-
<input type="radio" autocomplete="off"><i class="fa fa-columns"></i>
93-
</label>
94+
<label class="btn btn-default ui-edit" data-block-source="<%= !allowVisibleSource %>" title="<%= __('You have no rights to edit this note') %>" disabled>
95+
<input type="radio" autocomplete="off"><i class="fa fa-pencil"></i>
96+
</label>
97+
<label class="btn btn-default ui-both" data-block-source="<%= !allowVisibleSource %>" title="<%= __('You have no rights to edit this note') %>" disabled>
98+
<input type="radio" autocomplete="off"><i class="fa fa-columns"></i>
99+
</label>
94100
<% } else { %>
95-
<label class="btn btn-default ui-edit" title="<%= __('Edit') %> (Ctrl+Alt+E)">
96-
<input type="radio" name="mode" autocomplete="off"><i class="fa fa-pencil"></i>
97-
</label>
98-
<label class="btn btn-default ui-both" title="<%= __('Both') %> (Ctrl+Alt+B)">
99-
<input type="radio" name="mode" autocomplete="off"><i class="fa fa-columns"></i>
100-
</label>
101+
<label class="btn btn-default ui-edit" data-block-source="<%= !allowVisibleSource %>" title="<%= __('Edit') %> (Ctrl+Alt+E)">
102+
<input type="radio" name="mode" autocomplete="off"><i class="fa fa-pencil"></i>
103+
</label>
104+
<label class="btn btn-default ui-both" data-block-source="<%= !allowVisibleSource %>" title="<%= __('Both') %> (Ctrl+Alt+B)">
105+
<input type="radio" name="mode" autocomplete="off"><i class="fa fa-columns"></i>
106+
</label>
101107
<% } %>
102108
<label class="btn btn-default ui-view" title="<%= __('View') %> (Ctrl+Alt+V)">
103109
<input type="radio" name="mode" autocomplete="off"><i class="fa fa-eye"></i>

public/views/index/body.ejs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
<div class="masthead clearfix">
66
<div class="inner">
7-
<h3 class="masthead-brand"></h3>allowAnonymousViews
7+
<h3 class="masthead-brand"></h3>
88
<nav>
99
<ul class="nav masthead-nav">
1010
<li class="ui-home<% if(!signin) { %> active<% } %>"><a href="#"><%= __('Intro') %></a>

0 commit comments

Comments
 (0)