Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

show a checkbox to word wrap text #137

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
23 changes: 20 additions & 3 deletions fluffy/static/js/paste-inline.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,23 @@ function updateSelectedHash(selected) {
}

$(document).ready(function() {
var numbers = $('.line-numbers > a');

$("#do_word_wrap").click(function () {
let text_to_wrap = $(".highlight").find('pre');
if (text_to_wrap.css('white-space')==="pre") {
text_to_wrap.css('white-space', 'break-spaces');
} else {
text_to_wrap.css('white-space', '');
}
});

var numbers = $('.highlight > pre > span');
numbers.each(function (i) {
var data = $("<td class='line-data'></td>").text($(this).text());
var line_num = $("<td class='line-number'></td>").text(i);
$(this).html("");
$(this).append(line_num, data);
});
var setState = -1;
$(document).mouseup(function() {
setState = -1;
Expand Down Expand Up @@ -121,12 +137,13 @@ $(document).ready(function() {
}

var selected = selectedLines();
var line = parseInt(el.text());
var line = parseInt(el.get(0).id.replace("line-", ""));
var idx = selected.indexOf(line);

if (idx === -1 && setState === 1) {
updateLineClasses(line, true);
selected.push(line);
console.log("##"+ selected);
updateSelectedHash(selected);
} else if (idx !== -1 && setState === 0) {
selected.splice(idx, 1);
Expand All @@ -138,7 +155,7 @@ $(document).ready(function() {

numbers.on('mousedown', function(e) {
var selected = selectedLines();
var line = parseInt($(this).text());
var line = parseInt(this.id.replace("line-", ""));
setState = selected.indexOf(line) === -1 ? 1 : 0;
return maybeChangeState($(this));
});
Expand Down
4 changes: 4 additions & 0 deletions fluffy/static/scss/app.scss
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,7 @@ a.report {
.footer a:hover {
color: #89C2C7;
}

.page-paste #paste .text .highlight > pre > span::before {
content: attr(line-number);
}
5 changes: 5 additions & 0 deletions fluffy/templates/home.html
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@
<p>
<textarea id="text" name="text" placeholder="paste text here" required autofocus>{{text}}</textarea>
</p>

<p>
<input type="checkbox" id="format_text" name="format_text" value="format_text">
<label for="word_wrap">Format Output</label>
</p>
<p>
<select name="language">
<option selected="selected" value="">Guess the language for me</option>
Expand Down
3 changes: 3 additions & 0 deletions fluffy/templates/layouts/text.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
<a class="button" href="{{raw_url}}">
Raw Text
</a>
<a class="button" id="do_word_wrap" href="#">
Word Wrap
</a>

<form method="POST" action="{{home_url}}">
<input type="hidden" name="text" value="{{text}}" />
Expand Down
10 changes: 5 additions & 5 deletions fluffy/templates/paste.html
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@
{% endblock %}

{% block text %}
<div class="line-numbers">
{% for i in range(1, num_lines(text) + 1) %}
<a id="LL{{i}}">{{i}}</a>
{% endfor %}
</div>
{# <div class="line-numbers">#}
{# {% for i in range(1, num_lines(text) + 1) %}#}
{# <a id="LL{{i}}">{{i}}</a>#}
{# {% endfor %}#}
{# </div>#}
<div class="text" contenteditable="true" spellcheck="false">
{{highlighter.highlight(text)|safe}}
</div>
Expand Down
13 changes: 13 additions & 0 deletions fluffy/utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import os
import random
from json import dumps
from json import loads

from fluffy.app import app

Expand Down Expand Up @@ -98,3 +100,14 @@ def get_result():
return result

return get_result()


def iff_json_then_pretty_json(text):
try:
text_no_whitespace = text.strip()
if text_no_whitespace[0] in ['[', '{'] and text_no_whitespace[-1] in [']', '}']:
text = dumps(loads(text), separators=(',', ':'), indent=4)
return text
except ValueError:
pass
return None
7 changes: 6 additions & 1 deletion fluffy/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from fluffy.models import UploadedFile
from fluffy.utils import human_size
from fluffy.utils import ICON_EXTENSIONS
from fluffy.utils import iff_json_then_pretty_json
from fluffy.utils import ONE_MB


Expand Down Expand Up @@ -153,8 +154,12 @@ def paste():
), 413
objects.append(uf)

if request.form.get('format_text'):
if iff_json_then_pretty_json(text):
text = iff_json_then_pretty_json(text)
lang = 'json'
# HTML view (Markdown or paste)
lang = request.form['language']
lang = lang or request.form['language']
if lang != 'rendered-markdown':
highlighter = get_highlighter(text, lang, None)
lang_title = highlighter.name
Expand Down