Skip to content

Commit c21dfee

Browse files
authored
Merge pull request #468 from wordpress-mobile/feature/add-headless-factory-method
Aztec constructor without source editor
2 parents e56783e + e064bce commit c21dfee

File tree

3 files changed

+31
-13
lines changed

3 files changed

+31
-13
lines changed

app/src/main/kotlin/org/wordpress/aztec/demo/MainActivity.kt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -341,11 +341,11 @@ class MainActivity : AppCompatActivity(),
341341

342342
// initialize the text & HTML
343343
if (!isRunningTest) {
344-
aztec.sourceEditor.displayStyledAndFormattedHtml(EXAMPLE)
344+
aztec.sourceEditor?.displayStyledAndFormattedHtml(EXAMPLE)
345345
}
346346

347347
if (savedInstanceState == null) {
348-
aztec.visualEditor.fromHtml(aztec.sourceEditor.getPureHtml())
348+
aztec.visualEditor.fromHtml(aztec.sourceEditor?.getPureHtml()!!)
349349
aztec.initHistory()
350350
}
351351

@@ -485,13 +485,13 @@ class MainActivity : AppCompatActivity(),
485485
if (aztec.visualEditor.visibility == View.VISIBLE) {
486486
aztec.visualEditor.undo()
487487
} else {
488-
aztec.sourceEditor.undo()
488+
aztec.sourceEditor?.undo()
489489
}
490490
R.id.redo ->
491491
if (aztec.visualEditor.visibility == View.VISIBLE) {
492492
aztec.visualEditor.redo()
493493
} else {
494-
aztec.sourceEditor.redo()
494+
aztec.sourceEditor?.redo()
495495
}
496496
else -> {
497497
}
@@ -694,7 +694,7 @@ class MainActivity : AppCompatActivity(),
694694
if (mediaPending) {
695695
ToastUtils.showToast(this, R.string.media_upload_dialog_message)
696696
} else {
697-
aztec.toolbar.toggleEditorMode()
697+
aztec.toolbar?.toggleEditorMode()
698698
}
699699
}
700700

aztec/src/main/kotlin/org/wordpress/aztec/Aztec.kt

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ import org.wordpress.aztec.toolbar.AztecToolbar
1010
import org.wordpress.aztec.toolbar.IAztecToolbarClickListener
1111
import java.util.*
1212

13-
open class Aztec private constructor(val visualEditor: AztecText, val sourceEditor: SourceViewEditText,
14-
val toolbar: AztecToolbar, val toolbarClickListener: IAztecToolbarClickListener) {
13+
open class Aztec private constructor(val visualEditor: AztecText, val toolbar: AztecToolbar,
14+
private val toolbarClickListener: IAztecToolbarClickListener) {
1515

1616
private var imageGetter: Html.ImageGetter? = null
1717
private var videoThumbnailGetter: Html.VideoThumbnailGetter? = null
@@ -23,16 +23,27 @@ open class Aztec private constructor(val visualEditor: AztecText, val sourceEdit
2323
private var onAudioTappedListener: AztecText.OnAudioTappedListener? = null
2424
private var onMediaDeletedListener: AztecText.OnMediaDeletedListener? = null
2525
private var plugins: ArrayList<IAztecPlugin> = visualEditor.plugins
26+
var sourceEditor: SourceViewEditText? = null
2627

2728
init {
2829
initHistory()
2930
initToolbar()
3031
}
31-
32-
constructor(activity: Activity, @IdRes aztecTextId: Int,
32+
33+
private constructor(activity: Activity, @IdRes aztecTextId: Int,
3334
@IdRes sourceTextId: Int, @IdRes toolbarId: Int,
34-
toolbarClickListener: IAztecToolbarClickListener) : this(activity.findViewById(aztecTextId),
35-
activity.findViewById(sourceTextId), activity.findViewById(toolbarId), toolbarClickListener)
35+
toolbarClickListener: IAztecToolbarClickListener) : this(activity.findViewById<AztecText>(aztecTextId),
36+
activity.findViewById<SourceViewEditText>(sourceTextId), activity.findViewById<AztecToolbar>(toolbarId), toolbarClickListener)
37+
38+
private constructor(activity: Activity, @IdRes aztecTextId: Int,
39+
@IdRes toolbarId: Int,
40+
toolbarClickListener: IAztecToolbarClickListener) : this(activity.findViewById<AztecText>(aztecTextId),
41+
activity.findViewById<AztecToolbar>(toolbarId), toolbarClickListener)
42+
43+
private constructor(visualEditor: AztecText, sourceEditor: SourceViewEditText,
44+
toolbar: AztecToolbar, toolbarClickListener: IAztecToolbarClickListener) : this(visualEditor, toolbar, toolbarClickListener) {
45+
this.sourceEditor = sourceEditor
46+
}
3647

3748
companion object Factory {
3849
@JvmStatic
@@ -46,6 +57,10 @@ open class Aztec private constructor(val visualEditor: AztecText, val sourceEdit
4657
toolbar: AztecToolbar, toolbarClickListener: IAztecToolbarClickListener) : Aztec {
4758
return Aztec(visualEditor, sourceEditor, toolbar, toolbarClickListener)
4859
}
60+
61+
fun with(visualEditor: AztecText, toolbar: AztecToolbar, toolbarClickListener: IAztecToolbarClickListener) : Aztec {
62+
return Aztec(visualEditor, toolbar, toolbarClickListener)
63+
}
4964
}
5065

5166
fun setImageGetter(imageGetter: Html.ImageGetter) : Aztec {
@@ -113,7 +128,7 @@ open class Aztec private constructor(val visualEditor: AztecText, val sourceEdit
113128
}
114129

115130
fun initHistory() {
116-
sourceEditor.history = visualEditor.history
131+
sourceEditor?.history = visualEditor.history
117132
}
118133

119134
private fun initToolbar() {

aztec/src/main/kotlin/org/wordpress/aztec/toolbar/AztecToolbar.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ class AztecToolbar : FrameLayout, OnMenuItemClickListener {
312312
return editor != null && editor is AztecText
313313
}
314314

315-
fun setEditor(editor: AztecText, sourceEditor: SourceViewEditText) {
315+
fun setEditor(editor: AztecText, sourceEditor: SourceViewEditText?) {
316316
this.sourceEditor = sourceEditor
317317
this.editor = editor
318318

@@ -465,6 +465,9 @@ class AztecToolbar : FrameLayout, OnMenuItemClickListener {
465465
}
466466

467467
fun toggleEditorMode() {
468+
// only allow toggling if sourceEditor is present
469+
if (sourceEditor == null) return;
470+
468471
if (editor!!.visibility == View.VISIBLE) {
469472
sourceEditor!!.displayStyledAndFormattedHtml(editor!!.toPlainHtml(true))
470473
editor!!.visibility = View.GONE

0 commit comments

Comments
 (0)