Skip to content

Commit

Permalink
Merge pull request #757 from haiwen/add_editor_view
Browse files Browse the repository at this point in the history
add editor module in programmer
  • Loading branch information
freeplant authored Sep 15, 2018
2 parents 3abc2e8 + a30e18f commit fd759f5
Show file tree
Hide file tree
Showing 70 changed files with 1,795 additions and 37 deletions.
14 changes: 11 additions & 3 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ android {
applicationId 'com.seafile.seadroid2'
minSdkVersion rootProject.ext.minSdkVersion
targetSdkVersion rootProject.ext.targetSdkVersion
versionCode 81
versionName "2.2.6"
versionCode 82
versionName "2.2.7"
multiDexEnabled true
resValue "string", "authorities", applicationId + '.cameraupload.provider'
resValue "string", "account_type", "com.seafile.seadroid2.account.api2"
Expand Down Expand Up @@ -89,6 +89,11 @@ android {
}
}

allprojects {
repositories {
maven { url 'https://jitpack.io' }
}
}
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation "com.android.support:appcompat-v7:${rootProject.ext.supportLibVersion}"
Expand All @@ -99,7 +104,6 @@ android {
implementation 'com.google.guava:guava:18.0'
implementation 'com.nostra13.universalimageloader:universal-image-loader:1.9.3'
implementation 'com.cocosw:bottomsheet:1.3.1'
implementation 'us.feras.mdv:markdownview:1.1.0'
implementation 'com.commit451:PhotoView:1.2.4'
implementation 'com.joanzapata.iconify:android-iconify-material-community:2.2.1'
testImplementation 'junit:junit:4.12'
Expand All @@ -109,6 +113,10 @@ android {
implementation 'com.shuyu:gsyVideoPlayer-java:2.1.0'
implementation 'com.shuyu:gsyVideoPlayer-ex_so:2.1.0'
implementation 'com.squareup.okhttp3:okhttp:3.9.1'

implementation 'com.yydcdut:markdown-processor:0.1.3'
implementation 'ren.qinc.edit:lib:0.0.5'//editor undo redo
implementation 'com.github.tiagohm.MarkdownView:library:0.19.0'
}
}

6 changes: 6 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,12 @@
android:theme="@style/Theme.Fullscreen"
android:label="@string/app_name">
</activity>
<activity
android:name=".editor.EditorActivity"
android:screenOrientation="portrait"
android:theme="@style/AppTheme.Editor"
android:label="@string/app_name">
</activity>

<provider
android:name="com.seafile.seadroid2.provider.SeafileProvider"
Expand Down
189 changes: 189 additions & 0 deletions app/src/main/java/com/seafile/seadroid2/editor/EditorActivity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
package com.seafile.seadroid2.editor;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;

import com.seafile.seadroid2.R;
import com.seafile.seadroid2.SeafException;
import com.seafile.seadroid2.editor.widget.HorizontalEditScrollView;
import com.seafile.seadroid2.ui.activity.BaseActivity;
import com.seafile.seadroid2.ui.dialog.FileSaveTaskDialog;
import com.seafile.seadroid2.ui.dialog.TaskDialog;
import com.yydcdut.markdown.MarkdownConfiguration;
import com.yydcdut.markdown.MarkdownEditText;
import com.yydcdut.markdown.MarkdownProcessor;
import com.yydcdut.markdown.syntax.edit.EditFactory;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;

import ren.qinc.edit.PerformEdit;

public class EditorActivity extends BaseActivity implements Toolbar.OnMenuItemClickListener {

private MarkdownEditText mMarkdownEditText;
private HorizontalEditScrollView mHorizontalEditScrollView;
private MarkdownProcessor mMarkdownProcessor;
private String path;
private PerformEdit mPerformEdit;
private boolean isSave = true;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_editor);
Toolbar toolbar = getActionBarToolbar();
toolbar.setOnMenuItemClickListener(this);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);


mMarkdownEditText = findViewById(R.id.edit_md);
mHorizontalEditScrollView = findViewById(R.id.scroll_edit);
findViewById(R.id.view_toolbar_bottom_line).setVisibility(View.GONE);

Intent intent = getIntent();
path = intent.getStringExtra("path");
markdown();
getSupportActionBar().setTitle(new File(path).getName());
mMarkdownEditText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {

}

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {

}

@Override
public void afterTextChanged(Editable s) {
isSave = false;
}
});
}


private void markdown() {
MarkdownConfiguration markdownConfiguration = new MarkdownConfiguration.Builder(this)
.setDefaultImageSize(50, 50)
.setBlockQuotesLineColor(0xffdddddd)
.setHeader1RelativeSize(1.6f)
.setHeader2RelativeSize(1.5f)
.setHeader3RelativeSize(1.4f)
.setHeader4RelativeSize(1.3f)
.setHeader5RelativeSize(1.2f)
.setHeader6RelativeSize(1.1f)
.setHorizontalRulesColor(0xffdce1e7)
.setCodeBgColor(0xff222B38)
.setTodoColor(0xFF2196F3)
.setTodoDoneColor(0xffb8b8b8)
.setUnOrderListColor(0xFF2196F3)
.build();

mPerformEdit = new PerformEdit(mMarkdownEditText);
mPerformEdit.setDefaultText(readToString(new File(path)));
mHorizontalEditScrollView.setEditTextAndConfig(mMarkdownEditText, markdownConfiguration);
mMarkdownProcessor = new MarkdownProcessor(this);
mMarkdownProcessor.config(markdownConfiguration);
mMarkdownProcessor.factory(EditFactory.create());
mMarkdownProcessor.live(mMarkdownEditText);
}


public String readToString(File file) {
try {
if (file == null || !file.exists()) {
return null;
}
FileReader reader = new FileReader(file);
StringBuilder out = new StringBuilder();
char[] buffer = new char[1024 * 4];
int numRead = 0;
while ((numRead = reader.read(buffer)) > -1) {
out.append(String.valueOf(buffer, 0, numRead));
}
reader.close();
return out.toString();
} catch (IOException e) {
e.printStackTrace();
}
return "";
}

@Override
public void onDetachedFromWindow() {
mPerformEdit.clearHistory();
super.onDetachedFromWindow();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getActionBarToolbar().inflateMenu(R.menu.editor_view_menu);
return true;
}


@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
break;
case R.id.edit_undo:
mPerformEdit.undo();
break;
case R.id.edit_redo:
mPerformEdit.redo();
break;
case R.id.edit_save:
showSaveDialog();
break;
}
return super.onOptionsItemSelected(item);
}

@Override
public boolean onMenuItemClick(MenuItem item) {
return super.onOptionsItemSelected(item);
}

@Override
public void onBackPressed() {
if (isSave) {
super.onBackPressed();
} else {
showSaveDialog();
}
}


private void showSaveDialog() {
FileSaveTaskDialog dialog = new FileSaveTaskDialog();
dialog.init(path, mMarkdownEditText);
dialog.setTaskDialogLisenter(new TaskDialog.TaskDialogListener() {
@Override
public void onTaskSuccess() {
// save file success
isSave = true;
Toast.makeText(EditorActivity.this, getString(R.string.editor_file_save_success), Toast.LENGTH_SHORT).show();
finish();
}

@Override
public void onTaskFailed(SeafException e) {
Toast.makeText(EditorActivity.this, getString(R.string.editor_file_save_failed), Toast.LENGTH_SHORT).show();
}
});
dialog.show(getSupportFragmentManager(), "FileSaveTaskDialog");
}
}
54 changes: 54 additions & 0 deletions app/src/main/java/com/seafile/seadroid2/editor/LinkDialogView.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.seafile.seadroid2.editor;

import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;

import com.seafile.seadroid2.R;

public class LinkDialogView extends LinearLayout {
private EditText mDescriptionEditText;
private EditText mLinkEditText;

public LinkDialogView(Context context) {
super(context);
init(context);
}

public LinkDialogView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public LinkDialogView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context);
}

private void init(Context context) {
View v = LayoutInflater.from(context).inflate(R.layout.dialog_link, this, true);
mDescriptionEditText = (EditText) v.findViewById(R.id.edit_description_link);
mLinkEditText = (EditText) v.findViewById(R.id.edit_link);
}

public void clear() {
mDescriptionEditText.setText("");
mLinkEditText.setText("http://");
}

public String getDescription() {
return mDescriptionEditText.getText().toString();
}

public String getLink() {
return mLinkEditText.getText().toString();
}


}
63 changes: 63 additions & 0 deletions app/src/main/java/com/seafile/seadroid2/editor/Utils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package com.seafile.seadroid2.editor;

import android.support.annotation.Nullable;
import android.text.style.AlignmentSpan;
import android.widget.EditText;

public class Utils {
/**
* find '\n' from "start" position
*
* @param s text
* @param start start position
* @return the '\n' position
*/
public static int findNextNewLineChar(CharSequence s, int start) {
for (int i = start; i < s.length(); i++) {
if (s.charAt(i) == '\n') {
return i;
}
}
return -1;
}

/**
* find '\n' before "start" position
*
* @param s text
* @param start start position
* @return the '\n' position
*/
public static int findBeforeNewLineChar(CharSequence s, int start) {
for (int i = start - 1; i > 0; i--) {
if (s.charAt(i) == '\n') {
return i;
}
}
return -1;
}

@Nullable
public static <T> T getSpans(EditText editText, int start, int end, Class<T> clazz) {
T[] ts = editText.getText().getSpans(start, end, clazz);
if (ts != null && ts.length > 0) {
return ts[0];
}
return null;
}

public static boolean hasCenterSpan(EditText editText, int start, int end) {
AlignmentSpan.Standard centerSpan = Utils.getSpans(editText, start, end, AlignmentSpan.Standard.class);
if (centerSpan == null) {
return false;
} else {
return true;
}
}


public static int safePosition(int position, CharSequence s) {
return position > s.length() ? s.length() : (position < 0 ? 0 : position);
}

}
Loading

0 comments on commit fd759f5

Please sign in to comment.